Rev 3386 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 3386 | Rev 4153 | ||
|---|---|---|---|
| Line 57... | Line 57... | ||
| 57 | { "more", no_argument, 0, 'm' }, |
57 | { "more", no_argument, 0, 'm' }, |
| 58 | { 0, 0, 0, 0 } |
58 | { 0, 0, 0, 0 } |
| 59 | }; |
59 | }; |
| 60 | 60 | ||
| 61 | /* Dispays help for cat in various levels */ |
61 | /* Dispays help for cat in various levels */ |
| 62 | void * help_cmd_cat(unsigned int level) |
62 | void help_cmd_cat(unsigned int level) |
| 63 | { |
63 | { |
| 64 | if (level == HELP_SHORT) { |
64 | if (level == HELP_SHORT) { |
| 65 | printf("`%s' shows the contents of files\n", cmdname); |
65 | printf("`%s' shows the contents of files\n", cmdname); |
| 66 | } else { |
66 | } else { |
| 67 | help_cmd_cat(HELP_SHORT); |
67 | help_cmd_cat(HELP_SHORT); |
| Line 76... | Line 76... | ||
| 76 | " -m, --more Pause after each screen full\n" |
76 | " -m, --more Pause after each screen full\n" |
| 77 | "Currently, %s is under development, some options don't work.\n", |
77 | "Currently, %s is under development, some options don't work.\n", |
| 78 | cmdname, cmdname); |
78 | cmdname, cmdname); |
| 79 | } |
79 | } |
| 80 | 80 | ||
| 81 | return CMD_VOID; |
81 | return; |
| 82 | } |
82 | } |
| 83 | 83 | ||
| 84 | static unsigned int cat_file(const char *fname, size_t blen) |
84 | static unsigned int cat_file(const char *fname, size_t blen) |
| 85 | { |
85 | { |
| 86 | int fd, bytes = 0, count = 0, reads = 0; |
86 | int fd, bytes = 0, count = 0, reads = 0; |
| Line 101... | Line 101... | ||
| 101 | fname); |
101 | fname); |
| 102 | return 1; |
102 | return 1; |
| 103 | } |
103 | } |
| 104 | 104 | ||
| 105 | do { |
105 | do { |
| 106 | memset(buff, 0, sizeof(buff)); |
- | |
| 107 | bytes = read(fd, buff, blen); |
106 | bytes = read(fd, buff, blen); |
| 108 | if (bytes > 0) { |
107 | if (bytes > 0) { |
| 109 | count += bytes; |
108 | count += bytes; |
| 110 | if (bytes < blen) |
- | |
| 111 | buff[bytes] = '\0'; |
109 | buff[bytes] = '\0'; |
| 112 | printf(buff); |
110 | printf("%s", buff); |
| 113 | reads++; |
111 | reads++; |
| 114 | } |
112 | } |
| 115 | } while (bytes > 0); |
113 | } while (bytes > 0); |
| 116 | 114 | ||
| 117 | close(fd); |
115 | close(fd); |
| Line 119... | Line 117... | ||
| 119 | printf("Error reading %s\n", fname); |
117 | printf("Error reading %s\n", fname); |
| 120 | free(buff); |
118 | free(buff); |
| 121 | return 1; |
119 | return 1; |
| 122 | } |
120 | } |
| 123 | 121 | ||
| 124 | /* Debug stuff, newline not added purposefully */ |
- | |
| 125 | printf("** %s is a file with the size of %ld bytes\n", |
- | |
| 126 | fname, total); |
- | |
| 127 | printf( "** %d bytes were read in a buffer of %d bytes in %d reads\n", |
- | |
| 128 | count, blen, reads); |
- | |
| 129 | printf("** Read %s\n", count == total ? "Succeeded" : "Failed"); |
- | |
| 130 | free(buff); |
122 | free(buff); |
| 131 | 123 | ||
| 132 | return 0; |
124 | return 0; |
| 133 | } |
125 | } |
| 134 | 126 | ||
| 135 | /* Main entry point for cat, accepts an array of arguments */ |
127 | /* Main entry point for cat, accepts an array of arguments */ |
| 136 | int * cmd_cat(char **argv) |
128 | int cmd_cat(char **argv) |
| 137 | { |
129 | { |
| 138 | unsigned int argc, i, ret = 0, buffer = 0; |
130 | unsigned int argc, i, ret = 0, buffer = 0; |
| 139 | int c, opt_ind; |
131 | int c, opt_ind; |
| 140 | 132 | ||
| 141 | argc = cli_count_args(argv); |
133 | argc = cli_count_args(argv); |