diff options
author | Eric Andersen <andersen@codepoet.org> | 2000-08-02 19:57:18 +0000 |
---|---|---|
committer | Eric Andersen <andersen@codepoet.org> | 2000-08-02 19:57:18 +0000 |
commit | 46ade979839e6ec400d4b6f2c5bbd209b8940738 (patch) | |
tree | bac983a62a35c8bb35b64e4296b766a0d0df193a | |
parent | 0c8e2a6580cb83dbca842751fdf672c4f5d2f9bd (diff) | |
download | busybox-w32-46ade979839e6ec400d4b6f2c5bbd209b8940738.tar.gz busybox-w32-46ade979839e6ec400d4b6f2c5bbd209b8940738.tar.bz2 busybox-w32-46ade979839e6ec400d4b6f2c5bbd209b8940738.zip |
Cleaner way to handle -NUM and +NUM, reduces the mem usage as well.
-Erik
-rw-r--r-- | coreutils/tail.c | 49 | ||||
-rw-r--r-- | tail.c | 49 |
2 files changed, 50 insertions, 48 deletions
diff --git a/coreutils/tail.c b/coreutils/tail.c index 156f6368b..627373bfd 100644 --- a/coreutils/tail.c +++ b/coreutils/tail.c | |||
@@ -156,39 +156,38 @@ void add_file(char *name) | |||
156 | strcpy(files[n_files - 1], name); | 156 | strcpy(files[n_files - 1], name); |
157 | } | 157 | } |
158 | 158 | ||
159 | void checknumbers(const char* name) | ||
160 | { | ||
161 | int test=atoi(name); | ||
162 | if(test){ | ||
163 | units=test; | ||
164 | if(units<0) | ||
165 | units=units-1; | ||
166 | } else { | ||
167 | fatalError("Unrecognised number '%s'\n", name); | ||
168 | } | ||
169 | } | ||
159 | 170 | ||
160 | int tail_main(int argc, char **argv) | 171 | int tail_main(int argc, char **argv) |
161 | { | 172 | { |
162 | int show_headers = 1; | 173 | int show_headers = 1; |
163 | int test; | 174 | int test; |
164 | int opt; | 175 | int opt; |
165 | int optc=0; | ||
166 | char **optv=NULL; | ||
167 | char follow=0; | 176 | char follow=0; |
168 | int sleep_int=1; | 177 | int sleep_int=1; |
169 | int *fd; | 178 | int *fd; |
170 | 179 | ||
171 | opterr = 0; | 180 | opterr = 0; |
172 | 181 | ||
173 | for(opt=0;opt<argc;opt++){ | 182 | while ((opt=getopt(argc,argv,"c:fhn:s:q:v123456789+")) >0) { |
174 | test=atoi(argv[opt]); | ||
175 | if(test){ | ||
176 | units=test; | ||
177 | if(units<0) | ||
178 | units=units-1; | ||
179 | }else{ | ||
180 | optc++; | ||
181 | optv = realloc(optv, optc); | ||
182 | optv[optc - 1] = (char *) malloc(strlen(argv[opt]) + 1); | ||
183 | strcpy(optv[optc - 1], argv[opt]); | ||
184 | } | ||
185 | } | ||
186 | while ((opt=getopt(optc,optv,"c:fhn:s:q:v")) >0) { | ||
187 | 183 | ||
188 | switch (opt) { | 184 | switch (opt) { |
185 | case '1':case '2':case '3':case '4':case '5': | ||
186 | case '6':case '7':case '8':case '9':case '0': | ||
187 | checknumbers(argv[optind-1]); | ||
188 | break; | ||
189 | 189 | ||
190 | #ifndef BB_FEATURE_SIMPLE_TAIL | 190 | #ifndef BB_FEATURE_SIMPLE_TAIL |
191 | |||
192 | case 'c': | 191 | case 'c': |
193 | unit_type = BYTES; | 192 | unit_type = BYTES; |
194 | test = atoi(optarg); | 193 | test = atoi(optarg); |
@@ -248,17 +247,21 @@ int tail_main(int argc, char **argv) | |||
248 | usage(tail_usage); | 247 | usage(tail_usage); |
249 | } | 248 | } |
250 | } | 249 | } |
251 | while (optind <= optc) { | 250 | while (optind <= argc) { |
252 | if(optind==optc) { | 251 | if(optind==argc) { |
253 | if (n_files==0) | 252 | if (n_files==0) |
254 | add_file(STDIN); | 253 | add_file(STDIN); |
255 | else | 254 | else |
256 | break; | 255 | break; |
257 | }else { | 256 | }else { |
258 | if (!strcmp(optv[optind], "-")) | 257 | if (*argv[optind] == '+') { |
258 | checknumbers(argv[optind]); | ||
259 | } | ||
260 | else if (!strcmp(argv[optind], "-")) { | ||
259 | add_file(STDIN); | 261 | add_file(STDIN); |
260 | else | 262 | } else { |
261 | add_file(optv[optind]); | 263 | add_file(argv[optind]); |
264 | } | ||
262 | optind++; | 265 | optind++; |
263 | } | 266 | } |
264 | } | 267 | } |
@@ -325,8 +328,6 @@ int tail_main(int argc, char **argv) | |||
325 | free(buffer); | 328 | free(buffer); |
326 | if(files) | 329 | if(files) |
327 | free(files); | 330 | free(files); |
328 | if(optv) | ||
329 | free(optv); | ||
330 | return 0; | 331 | return 0; |
331 | } | 332 | } |
332 | 333 | ||
@@ -156,39 +156,38 @@ void add_file(char *name) | |||
156 | strcpy(files[n_files - 1], name); | 156 | strcpy(files[n_files - 1], name); |
157 | } | 157 | } |
158 | 158 | ||
159 | void checknumbers(const char* name) | ||
160 | { | ||
161 | int test=atoi(name); | ||
162 | if(test){ | ||
163 | units=test; | ||
164 | if(units<0) | ||
165 | units=units-1; | ||
166 | } else { | ||
167 | fatalError("Unrecognised number '%s'\n", name); | ||
168 | } | ||
169 | } | ||
159 | 170 | ||
160 | int tail_main(int argc, char **argv) | 171 | int tail_main(int argc, char **argv) |
161 | { | 172 | { |
162 | int show_headers = 1; | 173 | int show_headers = 1; |
163 | int test; | 174 | int test; |
164 | int opt; | 175 | int opt; |
165 | int optc=0; | ||
166 | char **optv=NULL; | ||
167 | char follow=0; | 176 | char follow=0; |
168 | int sleep_int=1; | 177 | int sleep_int=1; |
169 | int *fd; | 178 | int *fd; |
170 | 179 | ||
171 | opterr = 0; | 180 | opterr = 0; |
172 | 181 | ||
173 | for(opt=0;opt<argc;opt++){ | 182 | while ((opt=getopt(argc,argv,"c:fhn:s:q:v123456789+")) >0) { |
174 | test=atoi(argv[opt]); | ||
175 | if(test){ | ||
176 | units=test; | ||
177 | if(units<0) | ||
178 | units=units-1; | ||
179 | }else{ | ||
180 | optc++; | ||
181 | optv = realloc(optv, optc); | ||
182 | optv[optc - 1] = (char *) malloc(strlen(argv[opt]) + 1); | ||
183 | strcpy(optv[optc - 1], argv[opt]); | ||
184 | } | ||
185 | } | ||
186 | while ((opt=getopt(optc,optv,"c:fhn:s:q:v")) >0) { | ||
187 | 183 | ||
188 | switch (opt) { | 184 | switch (opt) { |
185 | case '1':case '2':case '3':case '4':case '5': | ||
186 | case '6':case '7':case '8':case '9':case '0': | ||
187 | checknumbers(argv[optind-1]); | ||
188 | break; | ||
189 | 189 | ||
190 | #ifndef BB_FEATURE_SIMPLE_TAIL | 190 | #ifndef BB_FEATURE_SIMPLE_TAIL |
191 | |||
192 | case 'c': | 191 | case 'c': |
193 | unit_type = BYTES; | 192 | unit_type = BYTES; |
194 | test = atoi(optarg); | 193 | test = atoi(optarg); |
@@ -248,17 +247,21 @@ int tail_main(int argc, char **argv) | |||
248 | usage(tail_usage); | 247 | usage(tail_usage); |
249 | } | 248 | } |
250 | } | 249 | } |
251 | while (optind <= optc) { | 250 | while (optind <= argc) { |
252 | if(optind==optc) { | 251 | if(optind==argc) { |
253 | if (n_files==0) | 252 | if (n_files==0) |
254 | add_file(STDIN); | 253 | add_file(STDIN); |
255 | else | 254 | else |
256 | break; | 255 | break; |
257 | }else { | 256 | }else { |
258 | if (!strcmp(optv[optind], "-")) | 257 | if (*argv[optind] == '+') { |
258 | checknumbers(argv[optind]); | ||
259 | } | ||
260 | else if (!strcmp(argv[optind], "-")) { | ||
259 | add_file(STDIN); | 261 | add_file(STDIN); |
260 | else | 262 | } else { |
261 | add_file(optv[optind]); | 263 | add_file(argv[optind]); |
264 | } | ||
262 | optind++; | 265 | optind++; |
263 | } | 266 | } |
264 | } | 267 | } |
@@ -325,8 +328,6 @@ int tail_main(int argc, char **argv) | |||
325 | free(buffer); | 328 | free(buffer); |
326 | if(files) | 329 | if(files) |
327 | free(files); | 330 | free(files); |
328 | if(optv) | ||
329 | free(optv); | ||
330 | return 0; | 331 | return 0; |
331 | } | 332 | } |
332 | 333 | ||