int tail_main(int argc, char **argv) { long count = 10; unsigned int sleep_period = 1; int from_top = 0; int follow = 0; int header_threshhold = 1; int count_bytes = 0; char *tailbuf; size_t tailbufsize; int taillen = 0; int newline = 0; int *fds, nfiles, nread, nwrite, seen, i, opt; char *s, *buf; const char *fmt; #if !ENABLE_DEBUG_YANK_SUSv2 || ENABLE_FEATURE_FANCY_TAIL /* Allow legacy syntax of an initial numeric option without -n. */ if (argc >=2 && ((argv[1][0] == '+') || ((argv[1][0] == '-') /* && (isdigit)(argv[1][1]) */ && (((unsigned int)(argv[1][1] - '0')) <= 9)))) { optind = 2; optarg = argv[1]; goto GET_COUNT; } #endif while ((opt = getopt(argc, argv, tail_opts)) > 0) { switch (opt) { case 'f': follow = 1; break; case 'c': count_bytes = 1; /* FALLS THROUGH */ case 'n': #if !ENABLE_DEBUG_YANK_SUSv2 || ENABLE_FEATURE_FANCY_TAIL GET_COUNT: #endif count = bb_xgetlarg10_sfx(optarg, tail_suffixes); /* Note: Leading whitespace is an error trapped above. */ if (*optarg == '+') { from_top = 1; } else { from_top = 0; } if (count < 0) { count = -count; } break; #if ENABLE_FEATURE_FANCY_TAIL case 'q': header_threshhold = INT_MAX; break; case 's': sleep_period =bb_xgetularg10_bnd(optarg, 0, UINT_MAX); break; case 'v': header_threshhold = 0; break; #endif default: bb_show_usage(); } } /* open all the files */ fds = (int *)xmalloc(sizeof(int) * (argc - optind + 1)); argv += optind; nfiles = i = 0; if ((argc -= optind) == 0) { struct stat statbuf; if (!fstat(STDIN_FILENO, &statbuf) && S_ISFIFO(statbuf.st_mode)) { follow = 0; } /* --argv; */ *argv = (char *) bb_msg_standard_input; goto DO_STDIN; } do { if ((argv[i][0] == '-') && !argv[i][1]) { DO_STDIN: fds[nfiles] = STDIN_FILENO; } else if ((fds[nfiles] = open(argv[i], O_RDONLY)) < 0) { bb_perror_msg("%s", argv[i]); status = EXIT_FAILURE; continue; } argv[nfiles] = argv[i]; ++nfiles; } while (++i < argc); if (!nfiles) { bb_error_msg_and_die("no files"); } tailbufsize = BUFSIZ; /* tail the files */ if (from_top < count_bytes) { /* Each is 0 or 1, so true iff 0 < 1. */ /* Hence, !from_top && count_bytes */ if (tailbufsize < count) { tailbufsize = count + BUFSIZ; } } buf = tailbuf = xmalloc(tailbufsize); fmt = header_fmt + 1; /* Skip header leading newline on first output. */ i = 0; do { /* Be careful. It would be possible to optimize the count-bytes * case if the file is seekable. If you do though, remember that * starting file position may not be the beginning of the file. * Beware of backing up too far. See example in wc.c. */ if ((!(count|from_top)) && (lseek(fds[i], 0, SEEK_END) >= 0)) { continue; } if (nfiles > header_threshhold) { tail_xprint_header(fmt, argv[i]); fmt = header_fmt; } buf = tailbuf; taillen = 0; seen = 1; newline = 0; while ((nread = tail_read(fds[i], buf, tailbufsize-taillen)) > 0) { if (from_top) { nwrite = nread; if (seen < count) { if (count_bytes) { nwrite -= (count - seen); seen = count; } else { s = buf; do { --nwrite; if ((*s++ == '\n') && (++seen == count)) { break; } } while (nwrite); } } tail_xbb_full_write(buf + nread - nwrite, nwrite); } else if (count) { if (count_bytes) { taillen += nread; if (taillen > count) { memmove(tailbuf, tailbuf + taillen - count, count); taillen = count; } } else { int k = nread; int nbuf = 0; while (k) { --k; if (buf[k] == '\n') { ++nbuf; } } if (newline + nbuf < count) { newline += nbuf; taillen += nread; } else { int extra = 0; if (buf[nread-1] != '\n') { extra = 1; } k = newline + nbuf + extra - count; s = tailbuf; while (k) { if (*s == '\n') { --k; } ++s; } taillen += nread - (s - tailbuf); memmove(tailbuf, s, taillen); newline = count - extra; } if (tailbufsize < taillen + BUFSIZ) { tailbufsize = taillen + BUFSIZ; tailbuf = xrealloc(tailbuf, tailbufsize); } } buf = tailbuf + taillen; } } if (!from_top) { tail_xbb_full_write(tailbuf, taillen); } taillen = 0; } while (++i < nfiles); buf = xrealloc(tailbuf, BUFSIZ); fmt = NULL; while (follow) { sleep(sleep_period); i = 0; do { if (nfiles > header_threshhold) { fmt = header_fmt; } while ((nread = tail_read(fds[i], buf, sizeof(buf))) > 0) { if (fmt) { tail_xprint_header(fmt, argv[i]); fmt = NULL; } tail_xbb_full_write(buf, nread); } } while (++i < nfiles); } return status; }
int tail_main(int argc, char **argv) { unsigned count = 10; unsigned sleep_period = 1; bool from_top; int header_threshhold = 1; const char *str_c, *str_n, *str_s; char *tailbuf; size_t tailbufsize; int taillen = 0; int newline = 0; int nfiles, nread, nwrite, seen, i, opt; int *fds; char *s, *buf; const char *fmt; #if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_TAIL /* Allow legacy syntax of an initial numeric option without -n. */ if (argc >= 2 && (argv[1][0] == '+' || argv[1][0] == '-') && isdigit(argv[1][1]) ) { argv[0] = (char*)"-n"; argv--; argc++; } #endif opt = getopt32(argc, argv, "fc:n:" USE_FEATURE_FANCY_TAIL("qs:v"), &str_c, &str_n, &str_s); #define FOLLOW (opt & 0x1) #define COUNT_BYTES (opt & 0x2) //if (opt & 0x1) // -f if (opt & 0x2) count = eat_num(str_c); // -c if (opt & 0x4) count = eat_num(str_n); // -n #if ENABLE_FEATURE_FANCY_TAIL if (opt & 0x8) header_threshhold = INT_MAX; // -q if (opt & 0x10) sleep_period = xatou(str_s); // -s if (opt & 0x20) header_threshhold = 0; // -v #endif argc -= optind; argv += optind; from_top = status; /* open all the files */ fds = xmalloc(sizeof(int) * (argc + 1)); status = nfiles = i = 0; if (argc == 0) { struct stat statbuf; if (!fstat(STDIN_FILENO, &statbuf) && S_ISFIFO(statbuf.st_mode)) { opt &= ~1; /* clear FOLLOW */ } *argv = (char *) bb_msg_standard_input; goto DO_STDIN; } do { if (NOT_LONE_DASH(argv[i])) { fds[nfiles] = open(argv[i], O_RDONLY); if (fds[nfiles] < 0) { bb_perror_msg("%s", argv[i]); status = EXIT_FAILURE; continue; } } else { DO_STDIN: /* "-" */ fds[nfiles] = STDIN_FILENO; } argv[nfiles] = argv[i]; ++nfiles; } while (++i < argc); if (!nfiles) bb_error_msg_and_die("no files"); tailbufsize = BUFSIZ; /* tail the files */ if (!from_top && COUNT_BYTES) { if (tailbufsize < count) { tailbufsize = count + BUFSIZ; } } buf = tailbuf = xmalloc(tailbufsize); fmt = header_fmt + 1; /* Skip header leading newline on first output. */ i = 0; do { /* Be careful. It would be possible to optimize the count-bytes * case if the file is seekable. If you do though, remember that * starting file position may not be the beginning of the file. * Beware of backing up too far. See example in wc.c. */ if (!(count | from_top) && lseek(fds[i], 0, SEEK_END) >= 0) { continue; } if (nfiles > header_threshhold) { tail_xprint_header(fmt, argv[i]); fmt = header_fmt; } buf = tailbuf; taillen = 0; seen = 1; newline = 0; while ((nread = tail_read(fds[i], buf, tailbufsize-taillen)) > 0) { if (from_top) { nwrite = nread; if (seen < count) { if (COUNT_BYTES) { nwrite -= (count - seen); seen = count; } else { s = buf; do { --nwrite; if (*s++ == '\n' && ++seen == count) { break; } } while (nwrite); } } xwrite(STDOUT_FILENO, buf + nread - nwrite, nwrite); } else if (count) { if (COUNT_BYTES) { taillen += nread; if (taillen > count) { memmove(tailbuf, tailbuf + taillen - count, count); taillen = count; } } else { int k = nread; int nbuf = 0; while (k) { --k; if (buf[k] == '\n') { ++nbuf; } } if (newline + nbuf < count) { newline += nbuf; taillen += nread; } else { int extra = 0; if (buf[nread-1] != '\n') { extra = 1; } k = newline + nbuf + extra - count; s = tailbuf; while (k) { if (*s == '\n') { --k; } ++s; } taillen += nread - (s - tailbuf); memmove(tailbuf, s, taillen); newline = count - extra; } if (tailbufsize < taillen + BUFSIZ) { tailbufsize = taillen + BUFSIZ; tailbuf = xrealloc(tailbuf, tailbufsize); } } buf = tailbuf + taillen; } } if (!from_top) { xwrite(STDOUT_FILENO, tailbuf, taillen); } taillen = 0; } while (++i < nfiles); buf = xrealloc(tailbuf, BUFSIZ); fmt = NULL; if (FOLLOW) while (1) { sleep(sleep_period); i = 0; do { if (nfiles > header_threshhold) { fmt = header_fmt; } while ((nread = tail_read(fds[i], buf, sizeof(buf))) > 0) { if (fmt) { tail_xprint_header(fmt, argv[i]); fmt = NULL; } xwrite(STDOUT_FILENO, buf, nread); } } while (++i < nfiles); } return status; }