Rev 4153 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3356 | post | 1 | /* Copyright (c) 2008, Tim Post <tinkertim@gmail.com> |
2 | * All rights reserved. |
||
3277 | post | 3 | * |
3356 | post | 4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions are met: |
||
6 | * |
||
7 | * Redistributions of source code must retain the above copyright notice, this |
||
8 | * list of conditions and the following disclaimer. |
||
9 | * |
||
10 | * Redistributions in binary form must reproduce the above copyright notice, |
||
11 | * this list of conditions and the following disclaimer in the documentation |
||
12 | * and/or other materials provided with the distribution. |
||
13 | * |
||
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 |
||
16 | * software without specific prior written permission. |
||
17 | * |
||
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 |
||
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
||
22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
||
23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
||
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 |
||
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 |
||
28 | * POSSIBILITY OF SUCH DAMAGE. |
||
29 | */ |
||
3277 | post | 30 | |
31 | #include <stdio.h> |
||
32 | #include <stdlib.h> |
||
3356 | post | 33 | #include <unistd.h> |
3319 | post | 34 | #include <getopt.h> |
3356 | post | 35 | #include <string.h> |
36 | #include <fcntl.h> |
||
3319 | post | 37 | |
3311 | post | 38 | #include "config.h" |
39 | #include "util.h" |
||
40 | #include "errors.h" |
||
3277 | post | 41 | #include "entry.h" |
42 | #include "cat.h" |
||
43 | #include "cmds.h" |
||
44 | |||
45 | static char *cmdname = "cat"; |
||
3356 | post | 46 | #define CAT_VERSION "0.0.1" |
47 | #define CAT_DEFAULT_BUFLEN 1024 |
||
3277 | post | 48 | |
3356 | post | 49 | static char *cat_oops = "That option is not yet supported\n"; |
50 | |||
3375 | post | 51 | static struct option const long_options[] = { |
3356 | post | 52 | { "help", no_argument, 0, 'h' }, |
53 | { "version", no_argument, 0, 'v' }, |
||
54 | { "head", required_argument, 0, 'H' }, |
||
55 | { "tail", required_argument, 0, 't' }, |
||
56 | { "buffer", required_argument, 0, 'b' }, |
||
57 | { "more", no_argument, 0, 'm' }, |
||
3319 | post | 58 | { 0, 0, 0, 0 } |
59 | }; |
||
60 | |||
3277 | post | 61 | /* Dispays help for cat in various levels */ |
4153 | mejdrech | 62 | void help_cmd_cat(unsigned int level) |
3277 | post | 63 | { |
3356 | post | 64 | if (level == HELP_SHORT) { |
65 | printf("`%s' shows the contents of files\n", cmdname); |
||
66 | } else { |
||
67 | help_cmd_cat(HELP_SHORT); |
||
68 | printf( |
||
69 | "Usage: %s [options] <file1> [file2] [...]\n" |
||
70 | "Options:\n" |
||
71 | " -h, --help A short option summary\n" |
||
72 | " -v, --version Print version information and exit\n" |
||
73 | " -H, --head ## Print only the first ## bytes\n" |
||
74 | " -t, --tail ## Print only the last ## bytes\n" |
||
75 | " -b, --buffer ## Set the read buffer size to ##\n" |
||
76 | " -m, --more Pause after each screen full\n" |
||
77 | "Currently, %s is under development, some options don't work.\n", |
||
78 | cmdname, cmdname); |
||
79 | } |
||
80 | |||
4153 | mejdrech | 81 | return; |
3277 | post | 82 | } |
83 | |||
3364 | post | 84 | static unsigned int cat_file(const char *fname, size_t blen) |
3356 | post | 85 | { |
86 | int fd, bytes = 0, count = 0, reads = 0; |
||
87 | off_t total = 0; |
||
88 | char *buff = NULL; |
||
89 | |||
4581 | mejdrech | 90 | fd = open(fname, O_RDONLY); |
91 | if (fd < 0) { |
||
3356 | post | 92 | printf("Unable to open %s\n", fname); |
93 | return 1; |
||
94 | } |
||
95 | |||
96 | total = lseek(fd, 0, SEEK_END); |
||
97 | lseek(fd, 0, SEEK_SET); |
||
3358 | post | 98 | |
3356 | post | 99 | if (NULL == (buff = (char *) malloc(blen + 1))) { |
100 | close(fd); |
||
101 | printf("Unable to allocate enough memory to read %s\n", |
||
102 | fname); |
||
103 | return 1; |
||
104 | } |
||
105 | |||
106 | do { |
||
107 | bytes = read(fd, buff, blen); |
||
108 | if (bytes > 0) { |
||
109 | count += bytes; |
||
4153 | mejdrech | 110 | buff[bytes] = '\0'; |
111 | printf("%s", buff); |
||
3356 | post | 112 | reads++; |
113 | } |
||
114 | } while (bytes > 0); |
||
115 | |||
116 | close(fd); |
||
117 | if (bytes == -1) { |
||
118 | printf("Error reading %s\n", fname); |
||
119 | free(buff); |
||
120 | return 1; |
||
121 | } |
||
122 | |||
123 | free(buff); |
||
124 | |||
125 | return 0; |
||
126 | } |
||
127 | |||
3277 | post | 128 | /* Main entry point for cat, accepts an array of arguments */ |
4153 | mejdrech | 129 | int cmd_cat(char **argv) |
3277 | post | 130 | { |
3356 | post | 131 | unsigned int argc, i, ret = 0, buffer = 0; |
3319 | post | 132 | int c, opt_ind; |
3277 | post | 133 | |
3376 | post | 134 | argc = cli_count_args(argv); |
3277 | post | 135 | |
3319 | post | 136 | for (c = 0, optind = 0, opt_ind = 0; c != -1;) { |
3356 | post | 137 | c = getopt_long(argc, argv, "hvmH:t:b:", long_options, &opt_ind); |
3319 | post | 138 | switch (c) { |
139 | case 'h': |
||
3356 | post | 140 | help_cmd_cat(HELP_LONG); |
141 | return CMD_SUCCESS; |
||
3319 | post | 142 | case 'v': |
3356 | post | 143 | printf("%s\n", CAT_VERSION); |
144 | return CMD_SUCCESS; |
||
145 | case 'H': |
||
146 | printf(cat_oops); |
||
147 | return CMD_FAILURE; |
||
148 | case 't': |
||
149 | printf(cat_oops); |
||
150 | return CMD_FAILURE; |
||
151 | case 'b': |
||
152 | printf(cat_oops); |
||
3319 | post | 153 | break; |
3356 | post | 154 | case 'm': |
155 | printf(cat_oops); |
||
156 | return CMD_FAILURE; |
||
3319 | post | 157 | } |
158 | } |
||
159 | |||
3356 | post | 160 | argc -= optind; |
3319 | post | 161 | |
3356 | post | 162 | if (argc < 1) { |
163 | printf("%s - incorrect number of arguments. Try `%s --help'\n", |
||
164 | cmdname, cmdname); |
||
165 | return CMD_FAILURE; |
||
166 | } |
||
167 | |||
168 | if (buffer <= 0) |
||
169 | buffer = CAT_DEFAULT_BUFLEN; |
||
170 | |||
171 | for (i = optind; argv[i] != NULL; i++) |
||
172 | ret += cat_file(argv[i], buffer); |
||
173 | |||
174 | if (ret) |
||
175 | return CMD_FAILURE; |
||
176 | else |
||
177 | return CMD_SUCCESS; |
||
3277 | post | 178 | } |
179 |