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 /coreutils | |
| 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
Diffstat (limited to 'coreutils')
| -rw-r--r-- | coreutils/tail.c | 167 |
1 files changed, 93 insertions, 74 deletions
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 | ||
