diff options
author | Eric Andersen <andersen@codepoet.org> | 2000-08-02 16:42:58 +0000 |
---|---|---|
committer | Eric Andersen <andersen@codepoet.org> | 2000-08-02 16:42:58 +0000 |
commit | d5fa3e3e9a8a1548b2e405ca9684bebbf946fd4f (patch) | |
tree | f1552eda438dc59df253e2b1c85bc6b23648e96e | |
parent | 080d51bf455201ea4f815371c556effca6298dc2 (diff) | |
download | busybox-w32-d5fa3e3e9a8a1548b2e405ca9684bebbf946fd4f.tar.gz busybox-w32-d5fa3e3e9a8a1548b2e405ca9684bebbf946fd4f.tar.bz2 busybox-w32-d5fa3e3e9a8a1548b2e405ca9684bebbf946fd4f.zip |
Update to the tail rewrite by "Allen Soard" <esp-software@mail.hypermart.net>
-Erik
-rw-r--r-- | applets/usage.c | 42 | ||||
-rw-r--r-- | busybox.def.h | 2 | ||||
-rw-r--r-- | coreutils/tail.c | 167 | ||||
-rw-r--r-- | tail.c | 167 | ||||
-rw-r--r-- | usage.c | 42 |
5 files changed, 221 insertions, 199 deletions
diff --git a/applets/usage.c b/applets/usage.c index 3f367f9b1..1e2eb64cc 100644 --- a/applets/usage.c +++ b/applets/usage.c | |||
@@ -1017,7 +1017,6 @@ const char syslogd_usage[] = | |||
1017 | #endif | 1017 | #endif |
1018 | 1018 | ||
1019 | #if defined BB_TAIL | 1019 | #if defined BB_TAIL |
1020 | #if defined BB_FEATURE_SIMPLE_TAIL | ||
1021 | const char tail_usage[] = | 1020 | const char tail_usage[] = |
1022 | "tail [OPTION] [FILE]...\n" | 1021 | "tail [OPTION] [FILE]...\n" |
1023 | #ifndef BB_FEATURE_TRIVIAL_HELP | 1022 | #ifndef BB_FEATURE_TRIVIAL_HELP |
@@ -1025,32 +1024,25 @@ const char tail_usage[] = | |||
1025 | "With more than one FILE, precede each with a header giving the\n" | 1024 | "With more than one FILE, precede each with a header giving the\n" |
1026 | "file name. With no FILE, or when FILE is -, read standard input.\n\n" | 1025 | "file name. With no FILE, or when FILE is -, read standard input.\n\n" |
1027 | "Options:\n" | 1026 | "Options:\n" |
1028 | "\t-n NUM\t\tPrint last NUM lines instead of first 10\n" | 1027 | #ifndef BB_FEATURE_SIMPLE_TAIL |
1029 | 1028 | "\t-c=N[kbm]\toutput the last N bytes\n" | |
1030 | "\t-f\t\tOutput data as the file grows. This version\n" | ||
1031 | "\t\t\tof 'tail -f' supports only one file at a time.\n" | ||
1032 | #endif | 1029 | #endif |
1033 | ; | 1030 | "\t-n NUM\t\tPrint last NUM lines instead of first 10\n" |
1034 | #else /* ! defined BB_FEATURE_SIMPLE_TAIL */ | 1031 | "\t\t\tAlso can be -NUM or +NUM.\n" |
1035 | const char tail_usage[] = | 1032 | "\t-f\t\tOutput data as the file grows.\n" |
1036 | "tail [OPTION]... [FILE]...\n" | 1033 | #ifndef BB_FEATURE_SIMPLE_TAIL |
1037 | #ifndef BB_FEATURE_TRIVIAL_HELP | 1034 | "\t-q\t\tnever output headers giving file names\n" |
1038 | "\nPrint last 10 lines of each FILE to standard output.\n" | 1035 | "\t-s SEC\t\tWait SEC seconds between reads with -f\n" |
1039 | "With more than one FILE, precede each with a header giving the file name.\n" | 1036 | "\t-v\t\talways output headers giving file names\n\n" |
1040 | "With no FILE, or when FILE is -, read standard input.\n" | ||
1041 | "\n" | ||
1042 | " -c=N[kbm] output the last N bytes\n" | ||
1043 | " -f output appended data as the file grows\n" | ||
1044 | " -n=N output the last N lines, instead of last 10\n" | ||
1045 | " -q never output headers giving file names\n" | ||
1046 | " -v always output headers giving file names\n" | ||
1047 | "\n" | ||
1048 | "If the first character of N (bytes or lines) is a `+', output begins with \n" | 1037 | "If the first character of N (bytes or lines) is a `+', output begins with \n" |
1049 | "the Nth item from the start of each file, otherwise, print the last N items\n" | 1038 | "the Nth item from the start of each file, otherwise, print the last N items\n" |
1050 | "in the file. N bytes may be suffixed by k (x1024), b (x512), or m (1024^2).\n" | 1039 | "in the file. N bytes may be suffixed by k (x1024), b (x512), or m (1024^2).\n" |
1040 | //#else | ||
1041 | // "\nIf the first character of N (bytes or lines) is a `+', output begins with \n" | ||
1042 | // "the Nth item from the start of each file.\n" | ||
1051 | #endif | 1043 | #endif |
1052 | ; | ||
1053 | #endif | 1044 | #endif |
1045 | ; | ||
1054 | #endif | 1046 | #endif |
1055 | 1047 | ||
1056 | #if defined BB_TAR | 1048 | #if defined BB_TAR |
@@ -1061,9 +1053,9 @@ const char tar_usage[] = | |||
1061 | "tar -[xtvO] " | 1053 | "tar -[xtvO] " |
1062 | #endif | 1054 | #endif |
1063 | #if defined BB_FEATURE_TAR_EXCLUDE | 1055 | #if defined BB_FEATURE_TAR_EXCLUDE |
1064 | "[--exclude File] " | 1056 | "[-X File(s)] " |
1065 | #endif | 1057 | #endif |
1066 | "[-f tarFile] [FILE] ...\n" | 1058 | "[-f tarFile] [FILE(s)] ...\n" |
1067 | #ifndef BB_FEATURE_TRIVIAL_HELP | 1059 | #ifndef BB_FEATURE_TRIVIAL_HELP |
1068 | "\nCreate, extract, or list files from a tar file. Note that\n" | 1060 | "\nCreate, extract, or list files from a tar file. Note that\n" |
1069 | "this version of tar treats hard links as separate files.\n\n" | 1061 | "this version of tar treats hard links as separate files.\n\n" |
@@ -1077,7 +1069,7 @@ const char tar_usage[] = | |||
1077 | "\tf\t\tname of tarfile or \"-\" for stdin\n" | 1069 | "\tf\t\tname of tarfile or \"-\" for stdin\n" |
1078 | "\tO\t\textract to stdout\n" | 1070 | "\tO\t\textract to stdout\n" |
1079 | #if defined BB_FEATURE_TAR_EXCLUDE | 1071 | #if defined BB_FEATURE_TAR_EXCLUDE |
1080 | "\t--exclude\tfile to exclude\n" | 1072 | "\tX\t\tfile(s) to exclude\n" |
1081 | #endif | 1073 | #endif |
1082 | "\nInformative output:\n" | 1074 | "\nInformative output:\n" |
1083 | "\tv\t\tverbosely list files processed\n" | 1075 | "\tv\t\tverbosely list files processed\n" |
diff --git a/busybox.def.h b/busybox.def.h index 41ba0ce85..5adc3e57f 100644 --- a/busybox.def.h +++ b/busybox.def.h | |||
@@ -190,7 +190,7 @@ | |||
190 | // enable syslogd -R remotehost | 190 | // enable syslogd -R remotehost |
191 | #define BB_FEATURE_REMOTE_LOG | 191 | #define BB_FEATURE_REMOTE_LOG |
192 | // | 192 | // |
193 | //Simple tail implementation (2.25k vs 3k for the full one). | 193 | //Simple tail implementation (2.34k vs 3k for the full one). |
194 | //Both provide 'tail -f' support (only one file at a time.) | 194 | //Both provide 'tail -f' support (only one file at a time.) |
195 | #define BB_FEATURE_SIMPLE_TAIL | 195 | #define BB_FEATURE_SIMPLE_TAIL |
196 | // | 196 | // |
diff --git a/coreutils/tail.c b/coreutils/tail.c index 4a1aa8436..156f6368b 100644 --- a/coreutils/tail.c +++ b/coreutils/tail.c | |||
@@ -47,47 +47,34 @@ | |||
47 | 47 | ||
48 | static int n_files = 0; | 48 | static int n_files = 0; |
49 | static char **files = NULL; | 49 | static char **files = NULL; |
50 | 50 | static char * buffer; | |
51 | static char follow=0; | 51 | static ssize_t bytes_read=0; |
52 | static ssize_t bs; | ||
53 | static ssize_t filelocation=0; | ||
54 | static char pip; | ||
52 | 55 | ||
53 | #ifdef BB_FEATURE_SIMPLE_TAIL | 56 | #ifdef BB_FEATURE_SIMPLE_TAIL |
54 | static const char unit_type=LINES; | 57 | static const char unit_type=LINES; |
55 | static const char sleep_int=1; | ||
56 | #else | 58 | #else |
57 | static char unit_type=LINES; | 59 | static char unit_type=LINES; |
58 | static int sleep_int=1; | ||
59 | static char verbose = 0; | 60 | static char verbose = 0; |
60 | #endif | 61 | #endif |
61 | 62 | ||
62 | //static off_t units=-11; | ||
63 | static off_t units=0; | 63 | static off_t units=0; |
64 | 64 | ||
65 | int tail_stream(int file_id) | 65 | int tail_stream(int fd) |
66 | { | 66 | { |
67 | int fd; | 67 | ssize_t startpoint; |
68 | ssize_t bytes_read=0; | ||
69 | ssize_t bs=BUFSIZ; | ||
70 | ssize_t startpoint=bs; | ||
71 | ssize_t endpoint=0; | 68 | ssize_t endpoint=0; |
72 | ssize_t count=0; | 69 | ssize_t count=0; |
73 | ssize_t filesize=0; | 70 | ssize_t filesize=0; |
74 | ssize_t filelocation=0; | ||
75 | char direction=1; | 71 | char direction=1; |
76 | char * buffer; | ||
77 | char pipe; | ||
78 | |||
79 | 72 | ||
80 | if (!strcmp(files[file_id], STDIN)) | 73 | filelocation=0; |
81 | fd = 0; | 74 | startpoint=bs=BUFSIZ; |
82 | else | ||
83 | fd = open(files[file_id], O_RDONLY); | ||
84 | if (fd == -1) | ||
85 | fatalError("Unable to open file %s.\n", files[file_id]); | ||
86 | |||
87 | buffer=malloc(bs); | ||
88 | 75 | ||
89 | filesize=lseek(fd, -1, SEEK_END)+1; | 76 | filesize=lseek(fd, -1, SEEK_END)+1; |
90 | pipe=(filesize<=0); | 77 | pip=(filesize<=0); |
91 | 78 | ||
92 | if(units>=0) | 79 | if(units>=0) |
93 | lseek(fd,0,SEEK_SET); | 80 | lseek(fd,0,SEEK_SET); |
@@ -96,7 +83,7 @@ int tail_stream(int file_id) | |||
96 | count=1; | 83 | count=1; |
97 | } | 84 | } |
98 | while(units != 0) { | 85 | while(units != 0) { |
99 | if (pipe) { | 86 | if (pip) { |
100 | char * line; | 87 | char * line; |
101 | ssize_t f_size=0; | 88 | ssize_t f_size=0; |
102 | 89 | ||
@@ -140,12 +127,12 @@ int tail_stream(int file_id) | |||
140 | #endif | 127 | #endif |
141 | if(buffer[startpoint-1]=='\n') | 128 | if(buffer[startpoint-1]=='\n') |
142 | count++; | 129 | count++; |
143 | if (!pipe) | 130 | if (!pip) |
144 | filelocation=lseek(fd,0,SEEK_CUR); | 131 | filelocation=lseek(fd,0,SEEK_CUR); |
145 | if(count==abs(units)) | 132 | if(count==units*direction) |
146 | break; | 133 | break; |
147 | } | 134 | } |
148 | if((count==abs(units)) | pipe) | 135 | if((count==units*direction) | pip) |
149 | break; | 136 | break; |
150 | if(direction<0){ | 137 | if(direction<0){ |
151 | filelocation = lseek(fd, -bytes_read, SEEK_CUR); | 138 | filelocation = lseek(fd, -bytes_read, SEEK_CUR); |
@@ -153,29 +140,11 @@ int tail_stream(int file_id) | |||
153 | break; | 140 | break; |
154 | } | 141 | } |
155 | } | 142 | } |
156 | if(pipe && (direction<0)) | 143 | if(pip && (direction<0)) |
157 | bs++; | 144 | bs++; |
158 | bytes_read=bs-startpoint; | 145 | bytes_read=bs-startpoint; |
159 | memcpy(&buffer[0],&buffer[startpoint],bytes_read); | 146 | memcpy(&buffer[0],&buffer[startpoint],bytes_read); |
160 | 147 | ||
161 | bs=BUFSIZ; | ||
162 | while (1) { | ||
163 | if((filelocation>0 || pipe)){ | ||
164 | write(1,buffer,bytes_read); | ||
165 | } | ||
166 | bytes_read = read(fd, buffer, bs); | ||
167 | filelocation+=bytes_read; | ||
168 | if (bytes_read <= 0) { | ||
169 | if (!follow) { | ||
170 | close(fd); | ||
171 | break; | ||
172 | } | ||
173 | sleep(sleep_int); | ||
174 | } | ||
175 | usleep(sleep_int * 1000); | ||
176 | } | ||
177 | if (buffer) | ||
178 | free(buffer); | ||
179 | return 0; | 148 | return 0; |
180 | } | 149 | } |
181 | 150 | ||
@@ -192,33 +161,31 @@ int tail_main(int argc, char **argv) | |||
192 | { | 161 | { |
193 | int show_headers = 1; | 162 | int show_headers = 1; |
194 | int test; | 163 | int test; |
195 | int c; | 164 | int opt; |
196 | int nargs=0; | 165 | int optc=0; |
197 | char **argn=NULL; | 166 | char **optv=NULL; |
167 | char follow=0; | ||
168 | int sleep_int=1; | ||
169 | int *fd; | ||
198 | 170 | ||
199 | opterr = 0; | 171 | opterr = 0; |
200 | 172 | ||
201 | for(c=0;c<argc;c++){ | 173 | for(opt=0;opt<argc;opt++){ |
202 | test=atoi(argv[c]); | 174 | test=atoi(argv[opt]); |
203 | if(test){ | 175 | if(test){ |
204 | units=test; | 176 | units=test; |
205 | if(units<0) | 177 | if(units<0) |
206 | units=units-1; | 178 | units=units-1; |
207 | }else{ | 179 | }else{ |
208 | nargs++; | 180 | optc++; |
209 | argn = realloc(argn, nargs); | 181 | optv = realloc(optv, optc); |
210 | argn[nargs - 1] = (char *) malloc(strlen(argv[c]) + 1); | 182 | optv[optc - 1] = (char *) malloc(strlen(argv[opt]) + 1); |
211 | strcpy(argn[nargs - 1], argv[c]); | 183 | strcpy(optv[optc - 1], argv[opt]); |
212 | } | 184 | } |
213 | } | 185 | } |
214 | while (1) { | 186 | while ((opt=getopt(optc,optv,"c:fhn:s:q:v")) >0) { |
215 | int opt_index = 0; | ||
216 | 187 | ||
217 | c = getopt_long_only(nargs, argn, | 188 | switch (opt) { |
218 | "c:fhn:s:qv", NULL, &opt_index); | ||
219 | if (c == -1) | ||
220 | break; | ||
221 | switch (c) { | ||
222 | 189 | ||
223 | #ifndef BB_FEATURE_SIMPLE_TAIL | 190 | #ifndef BB_FEATURE_SIMPLE_TAIL |
224 | 191 | ||
@@ -277,37 +244,89 @@ int tail_main(int argc, char **argv) | |||
277 | usage(tail_usage); | 244 | usage(tail_usage); |
278 | break; | 245 | break; |
279 | default: | 246 | default: |
280 | errorMsg("\nUnknown arg: %c.\n\n",c); | 247 | errorMsg("\nUnknown arg: %c.\n\n",optopt); |
281 | usage(tail_usage); | 248 | usage(tail_usage); |
282 | } | 249 | } |
283 | } | 250 | } |
284 | while (optind < nargs) { | 251 | while (optind <= optc) { |
285 | if (!strcmp(argn[optind], "-")) | 252 | if(optind==optc) { |
286 | add_file(STDIN); | 253 | if (n_files==0) |
287 | else | 254 | add_file(STDIN); |
288 | add_file(argn[optind]); | 255 | else |
289 | optind++; | 256 | break; |
257 | }else { | ||
258 | if (!strcmp(optv[optind], "-")) | ||
259 | add_file(STDIN); | ||
260 | else | ||
261 | add_file(optv[optind]); | ||
262 | optind++; | ||
263 | } | ||
290 | } | 264 | } |
291 | if(units==0) | 265 | if(units==0) |
292 | units=-11; | 266 | units=-11; |
293 | if(units>0) | 267 | if(units>0) |
294 | units--; | 268 | units--; |
295 | if (n_files == 0) | 269 | fd=malloc(sizeof(int)*n_files); |
296 | add_file(STDIN); | ||
297 | if (n_files == 1) | 270 | if (n_files == 1) |
298 | #ifndef BB_FEATURE_SIMPLE_TAIL | 271 | #ifndef BB_FEATURE_SIMPLE_TAIL |
299 | if (!verbose) | 272 | if (!verbose) |
300 | #endif | 273 | #endif |
301 | show_headers = 0; | 274 | show_headers = 0; |
275 | buffer=malloc(BUFSIZ); | ||
302 | for (test = 0; test < n_files; test++) { | 276 | for (test = 0; test < n_files; test++) { |
303 | if (show_headers) | 277 | if (show_headers) |
304 | printf("==> %s <==\n", files[test]); | 278 | printf("==> %s <==\n", files[test]); |
305 | tail_stream(test); | 279 | if (!strcmp(files[test], STDIN)) |
280 | fd[test] = 0; | ||
281 | else | ||
282 | fd[test] = open(files[test], O_RDONLY); | ||
283 | if (fd[test] == -1) | ||
284 | fatalError("Unable to open file %s.\n", files[test]); | ||
285 | tail_stream(fd[test]); | ||
286 | |||
287 | bs=BUFSIZ; | ||
288 | while (1) { | ||
289 | if((filelocation>0 || pip)){ | ||
290 | write(1,buffer,bytes_read); | ||
291 | } | ||
292 | bytes_read = read(fd[test], buffer, bs); | ||
293 | filelocation+=bytes_read; | ||
294 | if (bytes_read <= 0) { | ||
295 | break; | ||
296 | } | ||
297 | usleep(sleep_int * 1000); | ||
298 | } | ||
299 | if(n_files>1) | ||
300 | printf("\n"); | ||
306 | } | 301 | } |
302 | while(1){ | ||
303 | for (test = 0; test < n_files; test++) { | ||
304 | if(!follow){ | ||
305 | close(fd[test]); | ||
306 | continue; | ||
307 | } else { | ||
308 | sleep(sleep_int); | ||
309 | bytes_read = read(fd[test], buffer, bs); | ||
310 | if(bytes_read>0) { | ||
311 | if (show_headers) | ||
312 | printf("==> %s <==\n", files[test]); | ||
313 | write(1,buffer,bytes_read); | ||
314 | if(n_files>1) | ||
315 | printf("\n"); | ||
316 | } | ||
317 | } | ||
318 | } | ||
319 | if(!follow) | ||
320 | break; | ||
321 | } | ||
322 | if (fd) | ||
323 | free(fd); | ||
324 | if (buffer) | ||
325 | free(buffer); | ||
307 | if(files) | 326 | if(files) |
308 | free(files); | 327 | free(files); |
309 | if(argn) | 328 | if(optv) |
310 | free(argn); | 329 | free(optv); |
311 | return 0; | 330 | return 0; |
312 | } | 331 | } |
313 | 332 | ||
@@ -47,47 +47,34 @@ | |||
47 | 47 | ||
48 | static int n_files = 0; | 48 | static int n_files = 0; |
49 | static char **files = NULL; | 49 | static char **files = NULL; |
50 | 50 | static char * buffer; | |
51 | static char follow=0; | 51 | static ssize_t bytes_read=0; |
52 | static ssize_t bs; | ||
53 | static ssize_t filelocation=0; | ||
54 | static char pip; | ||
52 | 55 | ||
53 | #ifdef BB_FEATURE_SIMPLE_TAIL | 56 | #ifdef BB_FEATURE_SIMPLE_TAIL |
54 | static const char unit_type=LINES; | 57 | static const char unit_type=LINES; |
55 | static const char sleep_int=1; | ||
56 | #else | 58 | #else |
57 | static char unit_type=LINES; | 59 | static char unit_type=LINES; |
58 | static int sleep_int=1; | ||
59 | static char verbose = 0; | 60 | static char verbose = 0; |
60 | #endif | 61 | #endif |
61 | 62 | ||
62 | //static off_t units=-11; | ||
63 | static off_t units=0; | 63 | static off_t units=0; |
64 | 64 | ||
65 | int tail_stream(int file_id) | 65 | int tail_stream(int fd) |
66 | { | 66 | { |
67 | int fd; | 67 | ssize_t startpoint; |
68 | ssize_t bytes_read=0; | ||
69 | ssize_t bs=BUFSIZ; | ||
70 | ssize_t startpoint=bs; | ||
71 | ssize_t endpoint=0; | 68 | ssize_t endpoint=0; |
72 | ssize_t count=0; | 69 | ssize_t count=0; |
73 | ssize_t filesize=0; | 70 | ssize_t filesize=0; |
74 | ssize_t filelocation=0; | ||
75 | char direction=1; | 71 | char direction=1; |
76 | char * buffer; | ||
77 | char pipe; | ||
78 | |||
79 | 72 | ||
80 | if (!strcmp(files[file_id], STDIN)) | 73 | filelocation=0; |
81 | fd = 0; | 74 | startpoint=bs=BUFSIZ; |
82 | else | ||
83 | fd = open(files[file_id], O_RDONLY); | ||
84 | if (fd == -1) | ||
85 | fatalError("Unable to open file %s.\n", files[file_id]); | ||
86 | |||
87 | buffer=malloc(bs); | ||
88 | 75 | ||
89 | filesize=lseek(fd, -1, SEEK_END)+1; | 76 | filesize=lseek(fd, -1, SEEK_END)+1; |
90 | pipe=(filesize<=0); | 77 | pip=(filesize<=0); |
91 | 78 | ||
92 | if(units>=0) | 79 | if(units>=0) |
93 | lseek(fd,0,SEEK_SET); | 80 | lseek(fd,0,SEEK_SET); |
@@ -96,7 +83,7 @@ int tail_stream(int file_id) | |||
96 | count=1; | 83 | count=1; |
97 | } | 84 | } |
98 | while(units != 0) { | 85 | while(units != 0) { |
99 | if (pipe) { | 86 | if (pip) { |
100 | char * line; | 87 | char * line; |
101 | ssize_t f_size=0; | 88 | ssize_t f_size=0; |
102 | 89 | ||
@@ -140,12 +127,12 @@ int tail_stream(int file_id) | |||
140 | #endif | 127 | #endif |
141 | if(buffer[startpoint-1]=='\n') | 128 | if(buffer[startpoint-1]=='\n') |
142 | count++; | 129 | count++; |
143 | if (!pipe) | 130 | if (!pip) |
144 | filelocation=lseek(fd,0,SEEK_CUR); | 131 | filelocation=lseek(fd,0,SEEK_CUR); |
145 | if(count==abs(units)) | 132 | if(count==units*direction) |
146 | break; | 133 | break; |
147 | } | 134 | } |
148 | if((count==abs(units)) | pipe) | 135 | if((count==units*direction) | pip) |
149 | break; | 136 | break; |
150 | if(direction<0){ | 137 | if(direction<0){ |
151 | filelocation = lseek(fd, -bytes_read, SEEK_CUR); | 138 | filelocation = lseek(fd, -bytes_read, SEEK_CUR); |
@@ -153,29 +140,11 @@ int tail_stream(int file_id) | |||
153 | break; | 140 | break; |
154 | } | 141 | } |
155 | } | 142 | } |
156 | if(pipe && (direction<0)) | 143 | if(pip && (direction<0)) |
157 | bs++; | 144 | bs++; |
158 | bytes_read=bs-startpoint; | 145 | bytes_read=bs-startpoint; |
159 | memcpy(&buffer[0],&buffer[startpoint],bytes_read); | 146 | memcpy(&buffer[0],&buffer[startpoint],bytes_read); |
160 | 147 | ||
161 | bs=BUFSIZ; | ||
162 | while (1) { | ||
163 | if((filelocation>0 || pipe)){ | ||
164 | write(1,buffer,bytes_read); | ||
165 | } | ||
166 | bytes_read = read(fd, buffer, bs); | ||
167 | filelocation+=bytes_read; | ||
168 | if (bytes_read <= 0) { | ||
169 | if (!follow) { | ||
170 | close(fd); | ||
171 | break; | ||
172 | } | ||
173 | sleep(sleep_int); | ||
174 | } | ||
175 | usleep(sleep_int * 1000); | ||
176 | } | ||
177 | if (buffer) | ||
178 | free(buffer); | ||
179 | return 0; | 148 | return 0; |
180 | } | 149 | } |
181 | 150 | ||
@@ -192,33 +161,31 @@ int tail_main(int argc, char **argv) | |||
192 | { | 161 | { |
193 | int show_headers = 1; | 162 | int show_headers = 1; |
194 | int test; | 163 | int test; |
195 | int c; | 164 | int opt; |
196 | int nargs=0; | 165 | int optc=0; |
197 | char **argn=NULL; | 166 | char **optv=NULL; |
167 | char follow=0; | ||
168 | int sleep_int=1; | ||
169 | int *fd; | ||
198 | 170 | ||
199 | opterr = 0; | 171 | opterr = 0; |
200 | 172 | ||
201 | for(c=0;c<argc;c++){ | 173 | for(opt=0;opt<argc;opt++){ |
202 | test=atoi(argv[c]); | 174 | test=atoi(argv[opt]); |
203 | if(test){ | 175 | if(test){ |
204 | units=test; | 176 | units=test; |
205 | if(units<0) | 177 | if(units<0) |
206 | units=units-1; | 178 | units=units-1; |
207 | }else{ | 179 | }else{ |
208 | nargs++; | 180 | optc++; |
209 | argn = realloc(argn, nargs); | 181 | optv = realloc(optv, optc); |
210 | argn[nargs - 1] = (char *) malloc(strlen(argv[c]) + 1); | 182 | optv[optc - 1] = (char *) malloc(strlen(argv[opt]) + 1); |
211 | strcpy(argn[nargs - 1], argv[c]); | 183 | strcpy(optv[optc - 1], argv[opt]); |
212 | } | 184 | } |
213 | } | 185 | } |
214 | while (1) { | 186 | while ((opt=getopt(optc,optv,"c:fhn:s:q:v")) >0) { |
215 | int opt_index = 0; | ||
216 | 187 | ||
217 | c = getopt_long_only(nargs, argn, | 188 | switch (opt) { |
218 | "c:fhn:s:qv", NULL, &opt_index); | ||
219 | if (c == -1) | ||
220 | break; | ||
221 | switch (c) { | ||
222 | 189 | ||
223 | #ifndef BB_FEATURE_SIMPLE_TAIL | 190 | #ifndef BB_FEATURE_SIMPLE_TAIL |
224 | 191 | ||
@@ -277,37 +244,89 @@ int tail_main(int argc, char **argv) | |||
277 | usage(tail_usage); | 244 | usage(tail_usage); |
278 | break; | 245 | break; |
279 | default: | 246 | default: |
280 | errorMsg("\nUnknown arg: %c.\n\n",c); | 247 | errorMsg("\nUnknown arg: %c.\n\n",optopt); |
281 | usage(tail_usage); | 248 | usage(tail_usage); |
282 | } | 249 | } |
283 | } | 250 | } |
284 | while (optind < nargs) { | 251 | while (optind <= optc) { |
285 | if (!strcmp(argn[optind], "-")) | 252 | if(optind==optc) { |
286 | add_file(STDIN); | 253 | if (n_files==0) |
287 | else | 254 | add_file(STDIN); |
288 | add_file(argn[optind]); | 255 | else |
289 | optind++; | 256 | break; |
257 | }else { | ||
258 | if (!strcmp(optv[optind], "-")) | ||
259 | add_file(STDIN); | ||
260 | else | ||
261 | add_file(optv[optind]); | ||
262 | optind++; | ||
263 | } | ||
290 | } | 264 | } |
291 | if(units==0) | 265 | if(units==0) |
292 | units=-11; | 266 | units=-11; |
293 | if(units>0) | 267 | if(units>0) |
294 | units--; | 268 | units--; |
295 | if (n_files == 0) | 269 | fd=malloc(sizeof(int)*n_files); |
296 | add_file(STDIN); | ||
297 | if (n_files == 1) | 270 | if (n_files == 1) |
298 | #ifndef BB_FEATURE_SIMPLE_TAIL | 271 | #ifndef BB_FEATURE_SIMPLE_TAIL |
299 | if (!verbose) | 272 | if (!verbose) |
300 | #endif | 273 | #endif |
301 | show_headers = 0; | 274 | show_headers = 0; |
275 | buffer=malloc(BUFSIZ); | ||
302 | for (test = 0; test < n_files; test++) { | 276 | for (test = 0; test < n_files; test++) { |
303 | if (show_headers) | 277 | if (show_headers) |
304 | printf("==> %s <==\n", files[test]); | 278 | printf("==> %s <==\n", files[test]); |
305 | tail_stream(test); | 279 | if (!strcmp(files[test], STDIN)) |
280 | fd[test] = 0; | ||
281 | else | ||
282 | fd[test] = open(files[test], O_RDONLY); | ||
283 | if (fd[test] == -1) | ||
284 | fatalError("Unable to open file %s.\n", files[test]); | ||
285 | tail_stream(fd[test]); | ||
286 | |||
287 | bs=BUFSIZ; | ||
288 | while (1) { | ||
289 | if((filelocation>0 || pip)){ | ||
290 | write(1,buffer,bytes_read); | ||
291 | } | ||
292 | bytes_read = read(fd[test], buffer, bs); | ||
293 | filelocation+=bytes_read; | ||
294 | if (bytes_read <= 0) { | ||
295 | break; | ||
296 | } | ||
297 | usleep(sleep_int * 1000); | ||
298 | } | ||
299 | if(n_files>1) | ||
300 | printf("\n"); | ||
306 | } | 301 | } |
302 | while(1){ | ||
303 | for (test = 0; test < n_files; test++) { | ||
304 | if(!follow){ | ||
305 | close(fd[test]); | ||
306 | continue; | ||
307 | } else { | ||
308 | sleep(sleep_int); | ||
309 | bytes_read = read(fd[test], buffer, bs); | ||
310 | if(bytes_read>0) { | ||
311 | if (show_headers) | ||
312 | printf("==> %s <==\n", files[test]); | ||
313 | write(1,buffer,bytes_read); | ||
314 | if(n_files>1) | ||
315 | printf("\n"); | ||
316 | } | ||
317 | } | ||
318 | } | ||
319 | if(!follow) | ||
320 | break; | ||
321 | } | ||
322 | if (fd) | ||
323 | free(fd); | ||
324 | if (buffer) | ||
325 | free(buffer); | ||
307 | if(files) | 326 | if(files) |
308 | free(files); | 327 | free(files); |
309 | if(argn) | 328 | if(optv) |
310 | free(argn); | 329 | free(optv); |
311 | return 0; | 330 | return 0; |
312 | } | 331 | } |
313 | 332 | ||
@@ -1017,7 +1017,6 @@ const char syslogd_usage[] = | |||
1017 | #endif | 1017 | #endif |
1018 | 1018 | ||
1019 | #if defined BB_TAIL | 1019 | #if defined BB_TAIL |
1020 | #if defined BB_FEATURE_SIMPLE_TAIL | ||
1021 | const char tail_usage[] = | 1020 | const char tail_usage[] = |
1022 | "tail [OPTION] [FILE]...\n" | 1021 | "tail [OPTION] [FILE]...\n" |
1023 | #ifndef BB_FEATURE_TRIVIAL_HELP | 1022 | #ifndef BB_FEATURE_TRIVIAL_HELP |
@@ -1025,32 +1024,25 @@ const char tail_usage[] = | |||
1025 | "With more than one FILE, precede each with a header giving the\n" | 1024 | "With more than one FILE, precede each with a header giving the\n" |
1026 | "file name. With no FILE, or when FILE is -, read standard input.\n\n" | 1025 | "file name. With no FILE, or when FILE is -, read standard input.\n\n" |
1027 | "Options:\n" | 1026 | "Options:\n" |
1028 | "\t-n NUM\t\tPrint last NUM lines instead of first 10\n" | 1027 | #ifndef BB_FEATURE_SIMPLE_TAIL |
1029 | 1028 | "\t-c=N[kbm]\toutput the last N bytes\n" | |
1030 | "\t-f\t\tOutput data as the file grows. This version\n" | ||
1031 | "\t\t\tof 'tail -f' supports only one file at a time.\n" | ||
1032 | #endif | 1029 | #endif |
1033 | ; | 1030 | "\t-n NUM\t\tPrint last NUM lines instead of first 10\n" |
1034 | #else /* ! defined BB_FEATURE_SIMPLE_TAIL */ | 1031 | "\t\t\tAlso can be -NUM or +NUM.\n" |
1035 | const char tail_usage[] = | 1032 | "\t-f\t\tOutput data as the file grows.\n" |
1036 | "tail [OPTION]... [FILE]...\n" | 1033 | #ifndef BB_FEATURE_SIMPLE_TAIL |
1037 | #ifndef BB_FEATURE_TRIVIAL_HELP | 1034 | "\t-q\t\tnever output headers giving file names\n" |
1038 | "\nPrint last 10 lines of each FILE to standard output.\n" | 1035 | "\t-s SEC\t\tWait SEC seconds between reads with -f\n" |
1039 | "With more than one FILE, precede each with a header giving the file name.\n" | 1036 | "\t-v\t\talways output headers giving file names\n\n" |
1040 | "With no FILE, or when FILE is -, read standard input.\n" | ||
1041 | "\n" | ||
1042 | " -c=N[kbm] output the last N bytes\n" | ||
1043 | " -f output appended data as the file grows\n" | ||
1044 | " -n=N output the last N lines, instead of last 10\n" | ||
1045 | " -q never output headers giving file names\n" | ||
1046 | " -v always output headers giving file names\n" | ||
1047 | "\n" | ||
1048 | "If the first character of N (bytes or lines) is a `+', output begins with \n" | 1037 | "If the first character of N (bytes or lines) is a `+', output begins with \n" |
1049 | "the Nth item from the start of each file, otherwise, print the last N items\n" | 1038 | "the Nth item from the start of each file, otherwise, print the last N items\n" |
1050 | "in the file. N bytes may be suffixed by k (x1024), b (x512), or m (1024^2).\n" | 1039 | "in the file. N bytes may be suffixed by k (x1024), b (x512), or m (1024^2).\n" |
1040 | //#else | ||
1041 | // "\nIf the first character of N (bytes or lines) is a `+', output begins with \n" | ||
1042 | // "the Nth item from the start of each file.\n" | ||
1051 | #endif | 1043 | #endif |
1052 | ; | ||
1053 | #endif | 1044 | #endif |
1045 | ; | ||
1054 | #endif | 1046 | #endif |
1055 | 1047 | ||
1056 | #if defined BB_TAR | 1048 | #if defined BB_TAR |
@@ -1061,9 +1053,9 @@ const char tar_usage[] = | |||
1061 | "tar -[xtvO] " | 1053 | "tar -[xtvO] " |
1062 | #endif | 1054 | #endif |
1063 | #if defined BB_FEATURE_TAR_EXCLUDE | 1055 | #if defined BB_FEATURE_TAR_EXCLUDE |
1064 | "[--exclude File] " | 1056 | "[-X File(s)] " |
1065 | #endif | 1057 | #endif |
1066 | "[-f tarFile] [FILE] ...\n" | 1058 | "[-f tarFile] [FILE(s)] ...\n" |
1067 | #ifndef BB_FEATURE_TRIVIAL_HELP | 1059 | #ifndef BB_FEATURE_TRIVIAL_HELP |
1068 | "\nCreate, extract, or list files from a tar file. Note that\n" | 1060 | "\nCreate, extract, or list files from a tar file. Note that\n" |
1069 | "this version of tar treats hard links as separate files.\n\n" | 1061 | "this version of tar treats hard links as separate files.\n\n" |
@@ -1077,7 +1069,7 @@ const char tar_usage[] = | |||
1077 | "\tf\t\tname of tarfile or \"-\" for stdin\n" | 1069 | "\tf\t\tname of tarfile or \"-\" for stdin\n" |
1078 | "\tO\t\textract to stdout\n" | 1070 | "\tO\t\textract to stdout\n" |
1079 | #if defined BB_FEATURE_TAR_EXCLUDE | 1071 | #if defined BB_FEATURE_TAR_EXCLUDE |
1080 | "\t--exclude\tfile to exclude\n" | 1072 | "\tX\t\tfile(s) to exclude\n" |
1081 | #endif | 1073 | #endif |
1082 | "\nInformative output:\n" | 1074 | "\nInformative output:\n" |
1083 | "\tv\t\tverbosely list files processed\n" | 1075 | "\tv\t\tverbosely list files processed\n" |