Rev 3492 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3492 | Rev 3593 | ||
---|---|---|---|
1 | /* Copyright (c) 2008, Tim Post <tinkertim@gmail.com> |
1 | /* Copyright (c) 2008, Tim Post <tinkertim@gmail.com> |
2 | * All rights reserved. |
2 | * All rights reserved. |
3 | * |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions are met: |
5 | * modification, are permitted provided that the following conditions are met: |
6 | * |
6 | * |
7 | * Redistributions of source code must retain the above copyright notice, this |
7 | * Redistributions of source code must retain the above copyright notice, this |
8 | * list of conditions and the following disclaimer. |
8 | * list of conditions and the following disclaimer. |
9 | * |
9 | * |
10 | * Redistributions in binary form must reproduce the above copyright notice, |
10 | * Redistributions in binary form must reproduce the above copyright notice, |
11 | * this list of conditions and the following disclaimer in the documentation |
11 | * this list of conditions and the following disclaimer in the documentation |
12 | * and/or other materials provided with the distribution. |
12 | * and/or other materials provided with the distribution. |
13 | * |
13 | * |
14 | * Neither the name of the original program's authors nor the names of its |
14 | * Neither the name of the original program's authors nor the names of its |
15 | * contributors may be used to endorse or promote products derived from this |
15 | * contributors may be used to endorse or promote products derived from this |
16 | * software without specific prior written permission. |
16 | * software without specific prior written permission. |
17 | * |
17 | * |
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
28 | * POSSIBILITY OF SUCH DAMAGE. |
28 | * POSSIBILITY OF SUCH DAMAGE. |
29 | */ |
29 | */ |
30 | 30 | ||
31 | #include <stdio.h> |
31 | #include <stdio.h> |
32 | #include <stdlib.h> |
32 | #include <stdlib.h> |
33 | #include <unistd.h> |
33 | #include <unistd.h> |
34 | #include <getopt.h> |
34 | #include <getopt.h> |
35 | #include <string.h> |
35 | #include <string.h> |
36 | #include <fcntl.h> |
36 | #include <fcntl.h> |
37 | 37 | ||
38 | #include "config.h" |
38 | #include "config.h" |
39 | #include "util.h" |
39 | #include "util.h" |
40 | #include "errors.h" |
40 | #include "errors.h" |
41 | #include "entry.h" |
41 | #include "entry.h" |
42 | #include "cat.h" |
42 | #include "cat.h" |
43 | #include "cmds.h" |
43 | #include "cmds.h" |
44 | 44 | ||
45 | static char *cmdname = "cat"; |
45 | static char *cmdname = "cat"; |
46 | #define CAT_VERSION "0.0.1" |
46 | #define CAT_VERSION "0.0.1" |
47 | #define CAT_DEFAULT_BUFLEN 1024 |
47 | #define CAT_DEFAULT_BUFLEN 1024 |
48 | 48 | ||
49 | static char *cat_oops = "That option is not yet supported\n"; |
49 | static char *cat_oops = "That option is not yet supported\n"; |
50 | 50 | ||
51 | static struct option const long_options[] = { |
51 | static struct option const long_options[] = { |
52 | { "help", no_argument, 0, 'h' }, |
52 | { "help", no_argument, 0, 'h' }, |
53 | { "version", no_argument, 0, 'v' }, |
53 | { "version", no_argument, 0, 'v' }, |
54 | { "head", required_argument, 0, 'H' }, |
54 | { "head", required_argument, 0, 'H' }, |
55 | { "tail", required_argument, 0, 't' }, |
55 | { "tail", required_argument, 0, 't' }, |
56 | { "buffer", required_argument, 0, 'b' }, |
56 | { "buffer", required_argument, 0, 'b' }, |
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); |
68 | printf( |
68 | printf( |
69 | "Usage: %s [options] <file1> [file2] [...]\n" |
69 | "Usage: %s [options] <file1> [file2] [...]\n" |
70 | "Options:\n" |
70 | "Options:\n" |
71 | " -h, --help A short option summary\n" |
71 | " -h, --help A short option summary\n" |
72 | " -v, --version Print version information and exit\n" |
72 | " -v, --version Print version information and exit\n" |
73 | " -H, --head ## Print only the first ## bytes\n" |
73 | " -H, --head ## Print only the first ## bytes\n" |
74 | " -t, --tail ## Print only the last ## bytes\n" |
74 | " -t, --tail ## Print only the last ## bytes\n" |
75 | " -b, --buffer ## Set the read buffer size to ##\n" |
75 | " -b, --buffer ## Set the read buffer size to ##\n" |
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; |
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; |
87 | off_t total = 0; |
87 | off_t total = 0; |
88 | char *buff = NULL; |
88 | char *buff = NULL; |
89 | 89 | ||
90 | if (-1 == (fd = open(fname, O_RDONLY))) { |
90 | if (-1 == (fd = open(fname, O_RDONLY))) { |
91 | printf("Unable to open %s\n", fname); |
91 | printf("Unable to open %s\n", fname); |
92 | return 1; |
92 | return 1; |
93 | } |
93 | } |
94 | 94 | ||
95 | total = lseek(fd, 0, SEEK_END); |
95 | total = lseek(fd, 0, SEEK_END); |
96 | lseek(fd, 0, SEEK_SET); |
96 | lseek(fd, 0, SEEK_SET); |
97 | 97 | ||
98 | if (NULL == (buff = (char *) malloc(blen + 1))) { |
98 | if (NULL == (buff = (char *) malloc(blen + 1))) { |
99 | close(fd); |
99 | close(fd); |
100 | printf("Unable to allocate enough memory to read %s\n", |
100 | printf("Unable to allocate enough memory to read %s\n", |
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); |
118 | if (bytes == -1) { |
116 | if (bytes == -1) { |
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 | free(buff); |
122 | free(buff); |
125 | 123 | ||
126 | return 0; |
124 | return 0; |
127 | } |
125 | } |
128 | 126 | ||
129 | /* Main entry point for cat, accepts an array of arguments */ |
127 | /* Main entry point for cat, accepts an array of arguments */ |
130 | int cmd_cat(char **argv) |
128 | int cmd_cat(char **argv) |
131 | { |
129 | { |
132 | unsigned int argc, i, ret = 0, buffer = 0; |
130 | unsigned int argc, i, ret = 0, buffer = 0; |
133 | int c, opt_ind; |
131 | int c, opt_ind; |
134 | 132 | ||
135 | argc = cli_count_args(argv); |
133 | argc = cli_count_args(argv); |
136 | 134 | ||
137 | for (c = 0, optind = 0, opt_ind = 0; c != -1;) { |
135 | for (c = 0, optind = 0, opt_ind = 0; c != -1;) { |
138 | c = getopt_long(argc, argv, "hvmH:t:b:", long_options, &opt_ind); |
136 | c = getopt_long(argc, argv, "hvmH:t:b:", long_options, &opt_ind); |
139 | switch (c) { |
137 | switch (c) { |
140 | case 'h': |
138 | case 'h': |
141 | help_cmd_cat(HELP_LONG); |
139 | help_cmd_cat(HELP_LONG); |
142 | return CMD_SUCCESS; |
140 | return CMD_SUCCESS; |
143 | case 'v': |
141 | case 'v': |
144 | printf("%s\n", CAT_VERSION); |
142 | printf("%s\n", CAT_VERSION); |
145 | return CMD_SUCCESS; |
143 | return CMD_SUCCESS; |
146 | case 'H': |
144 | case 'H': |
147 | printf(cat_oops); |
145 | printf(cat_oops); |
148 | return CMD_FAILURE; |
146 | return CMD_FAILURE; |
149 | case 't': |
147 | case 't': |
150 | printf(cat_oops); |
148 | printf(cat_oops); |
151 | return CMD_FAILURE; |
149 | return CMD_FAILURE; |
152 | case 'b': |
150 | case 'b': |
153 | printf(cat_oops); |
151 | printf(cat_oops); |
154 | break; |
152 | break; |
155 | case 'm': |
153 | case 'm': |
156 | printf(cat_oops); |
154 | printf(cat_oops); |
157 | return CMD_FAILURE; |
155 | return CMD_FAILURE; |
158 | } |
156 | } |
159 | } |
157 | } |
160 | 158 | ||
161 | argc -= optind; |
159 | argc -= optind; |
162 | 160 | ||
163 | if (argc < 1) { |
161 | if (argc < 1) { |
164 | printf("%s - incorrect number of arguments. Try `%s --help'\n", |
162 | printf("%s - incorrect number of arguments. Try `%s --help'\n", |
165 | cmdname, cmdname); |
163 | cmdname, cmdname); |
166 | return CMD_FAILURE; |
164 | return CMD_FAILURE; |
167 | } |
165 | } |
168 | 166 | ||
169 | if (buffer <= 0) |
167 | if (buffer <= 0) |
170 | buffer = CAT_DEFAULT_BUFLEN; |
168 | buffer = CAT_DEFAULT_BUFLEN; |
171 | 169 | ||
172 | for (i = optind; argv[i] != NULL; i++) |
170 | for (i = optind; argv[i] != NULL; i++) |
173 | ret += cat_file(argv[i], buffer); |
171 | ret += cat_file(argv[i], buffer); |
174 | 172 | ||
175 | if (ret) |
173 | if (ret) |
176 | return CMD_FAILURE; |
174 | return CMD_FAILURE; |
177 | else |
175 | else |
178 | return CMD_SUCCESS; |
176 | return CMD_SUCCESS; |
179 | } |
177 | } |
180 | 178 | ||
181 | 179 |