[821] | 1 | /* vi: set sw=4 ts=4: */
|
---|
| 2 | /*
|
---|
| 3 | * Mini tail implementation for busybox
|
---|
| 4 | *
|
---|
| 5 | * Copyright (C) 2001 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
|
---|
| 6 | *
|
---|
| 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
---|
| 8 | */
|
---|
| 9 |
|
---|
| 10 | /* BB_AUDIT SUSv3 compliant (need fancy for -c) */
|
---|
| 11 | /* BB_AUDIT GNU compatible -c, -q, and -v options in 'fancy' configuration. */
|
---|
| 12 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/tail.html */
|
---|
| 13 |
|
---|
| 14 | /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
|
---|
| 15 | *
|
---|
| 16 | * Pretty much rewritten to fix numerous bugs and reduce realloc() calls.
|
---|
| 17 | * Bugs fixed (although I may have forgotten one or two... it was pretty bad)
|
---|
| 18 | * 1) mixing printf/write without fflush()ing stdout
|
---|
| 19 | * 2) no check that any open files are present
|
---|
| 20 | * 3) optstring had -q taking an arg
|
---|
| 21 | * 4) no error checking on write in some cases, and a warning even then
|
---|
| 22 | * 5) q and s interaction bug
|
---|
| 23 | * 6) no check for lseek error
|
---|
| 24 | * 7) lseek attempted when count==0 even if arg was +0 (from top)
|
---|
| 25 | */
|
---|
| 26 |
|
---|
[1770] | 27 | #include "libbb.h"
|
---|
[821] | 28 |
|
---|
| 29 | static const struct suffix_mult tail_suffixes[] = {
|
---|
| 30 | { "b", 512 },
|
---|
| 31 | { "k", 1024 },
|
---|
[1770] | 32 | { "m", 1024*1024 },
|
---|
| 33 | { }
|
---|
[821] | 34 | };
|
---|
| 35 |
|
---|
[1770] | 36 | struct globals {
|
---|
| 37 | bool status;
|
---|
| 38 | };
|
---|
| 39 | #define G (*(struct globals*)&bb_common_bufsiz1)
|
---|
[821] | 40 |
|
---|
| 41 | static void tail_xprint_header(const char *fmt, const char *filename)
|
---|
| 42 | {
|
---|
[1770] | 43 | if (fdprintf(STDOUT_FILENO, fmt, filename) < 0)
|
---|
[821] | 44 | bb_perror_nomsg_and_die();
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | static ssize_t tail_read(int fd, char *buf, size_t count)
|
---|
| 48 | {
|
---|
| 49 | ssize_t r;
|
---|
[1770] | 50 | off_t current, end;
|
---|
[821] | 51 | struct stat sbuf;
|
---|
| 52 |
|
---|
| 53 | end = current = lseek(fd, 0, SEEK_CUR);
|
---|
| 54 | if (!fstat(fd, &sbuf))
|
---|
| 55 | end = sbuf.st_size;
|
---|
| 56 | lseek(fd, end < current ? 0 : current, SEEK_SET);
|
---|
[1770] | 57 | r = safe_read(fd, buf, count);
|
---|
| 58 | if (r < 0) {
|
---|
[821] | 59 | bb_perror_msg(bb_msg_read_error);
|
---|
[1770] | 60 | G.status = EXIT_FAILURE;
|
---|
[821] | 61 | }
|
---|
| 62 |
|
---|
| 63 | return r;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[1770] | 66 | static const char header_fmt[] ALIGN1 = "\n==> %s <==\n";
|
---|
[821] | 67 |
|
---|
[1770] | 68 | static unsigned eat_num(const char *p)
|
---|
| 69 | {
|
---|
| 70 | if (*p == '-') p++;
|
---|
| 71 | else if (*p == '+') { p++; G.status = EXIT_FAILURE; }
|
---|
| 72 | return xatou_sfx(p, tail_suffixes);
|
---|
| 73 | }
|
---|
[821] | 74 |
|
---|
[1770] | 75 | int tail_main(int argc, char **argv);
|
---|
[821] | 76 | int tail_main(int argc, char **argv)
|
---|
| 77 | {
|
---|
[1770] | 78 | unsigned count = 10;
|
---|
| 79 | unsigned sleep_period = 1;
|
---|
| 80 | bool from_top;
|
---|
[821] | 81 | int header_threshhold = 1;
|
---|
[1770] | 82 | const char *str_c, *str_n;
|
---|
| 83 | USE_FEATURE_FANCY_TAIL(const char *str_s;)
|
---|
[821] | 84 |
|
---|
| 85 | char *tailbuf;
|
---|
| 86 | size_t tailbufsize;
|
---|
| 87 | int taillen = 0;
|
---|
| 88 | int newline = 0;
|
---|
[1770] | 89 | int nfiles, nread, nwrite, seen, i, opt;
|
---|
[821] | 90 |
|
---|
[1770] | 91 | int *fds;
|
---|
[821] | 92 | char *s, *buf;
|
---|
| 93 | const char *fmt;
|
---|
| 94 |
|
---|
[1770] | 95 | #if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_TAIL
|
---|
[821] | 96 | /* Allow legacy syntax of an initial numeric option without -n. */
|
---|
[1770] | 97 | if (argc >= 2 && (argv[1][0] == '+' || argv[1][0] == '-')
|
---|
| 98 | && isdigit(argv[1][1])
|
---|
| 99 | ) {
|
---|
| 100 | /* replacing arg[0] with "-n" can segfault, so... */
|
---|
| 101 | argv[1] = xasprintf("-n%s", argv[1]);
|
---|
| 102 | #if 0 /* If we ever decide to make tail NOFORK */
|
---|
| 103 | char *s = alloca(strlen(argv[1]) + 3);
|
---|
| 104 | sprintf(s, "-n%s", argv[1]);
|
---|
| 105 | argv[1] = s;
|
---|
| 106 | #endif
|
---|
[821] | 107 | }
|
---|
| 108 | #endif
|
---|
| 109 |
|
---|
[1770] | 110 | opt = getopt32(argv, "fc:n:" USE_FEATURE_FANCY_TAIL("qs:v"),
|
---|
| 111 | &str_c, &str_n USE_FEATURE_FANCY_TAIL(,&str_s));
|
---|
| 112 | #define FOLLOW (opt & 0x1)
|
---|
| 113 | #define COUNT_BYTES (opt & 0x2)
|
---|
| 114 | //if (opt & 0x1) // -f
|
---|
| 115 | if (opt & 0x2) count = eat_num(str_c); // -c
|
---|
| 116 | if (opt & 0x4) count = eat_num(str_n); // -n
|
---|
[821] | 117 | #if ENABLE_FEATURE_FANCY_TAIL
|
---|
[1770] | 118 | if (opt & 0x8) header_threshhold = INT_MAX; // -q
|
---|
| 119 | if (opt & 0x10) sleep_period = xatou(str_s); // -s
|
---|
| 120 | if (opt & 0x20) header_threshhold = 0; // -v
|
---|
[821] | 121 | #endif
|
---|
[1770] | 122 | argc -= optind;
|
---|
| 123 | argv += optind;
|
---|
| 124 | from_top = G.status;
|
---|
[821] | 125 |
|
---|
| 126 | /* open all the files */
|
---|
[1770] | 127 | fds = xmalloc(sizeof(int) * (argc + 1));
|
---|
[821] | 128 | nfiles = i = 0;
|
---|
[1770] | 129 | G.status = EXIT_SUCCESS;
|
---|
| 130 | if (argc == 0) {
|
---|
[821] | 131 | struct stat statbuf;
|
---|
| 132 |
|
---|
| 133 | if (!fstat(STDIN_FILENO, &statbuf) && S_ISFIFO(statbuf.st_mode)) {
|
---|
[1770] | 134 | opt &= ~1; /* clear FOLLOW */
|
---|
[821] | 135 | }
|
---|
| 136 | *argv = (char *) bb_msg_standard_input;
|
---|
| 137 | }
|
---|
| 138 | do {
|
---|
[1770] | 139 | FILE* fil = fopen_or_warn_stdin(argv[i]);
|
---|
| 140 | if (!fil) {
|
---|
| 141 | G.status = EXIT_FAILURE;
|
---|
[821] | 142 | continue;
|
---|
| 143 | }
|
---|
[1770] | 144 | fds[nfiles] = fileno(fil);
|
---|
| 145 | argv[nfiles++] = argv[i];
|
---|
[821] | 146 | } while (++i < argc);
|
---|
| 147 |
|
---|
[1770] | 148 | if (!nfiles)
|
---|
[821] | 149 | bb_error_msg_and_die("no files");
|
---|
| 150 |
|
---|
| 151 | tailbufsize = BUFSIZ;
|
---|
| 152 |
|
---|
| 153 | /* tail the files */
|
---|
[1770] | 154 | if (!from_top && COUNT_BYTES) {
|
---|
[821] | 155 | if (tailbufsize < count) {
|
---|
| 156 | tailbufsize = count + BUFSIZ;
|
---|
| 157 | }
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | buf = tailbuf = xmalloc(tailbufsize);
|
---|
| 161 |
|
---|
| 162 | fmt = header_fmt + 1; /* Skip header leading newline on first output. */
|
---|
| 163 | i = 0;
|
---|
| 164 | do {
|
---|
| 165 | /* Be careful. It would be possible to optimize the count-bytes
|
---|
| 166 | * case if the file is seekable. If you do though, remember that
|
---|
| 167 | * starting file position may not be the beginning of the file.
|
---|
| 168 | * Beware of backing up too far. See example in wc.c.
|
---|
| 169 | */
|
---|
[1770] | 170 | if (!(count | from_top) && lseek(fds[i], 0, SEEK_END) >= 0) {
|
---|
[821] | 171 | continue;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | if (nfiles > header_threshhold) {
|
---|
| 175 | tail_xprint_header(fmt, argv[i]);
|
---|
| 176 | fmt = header_fmt;
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | buf = tailbuf;
|
---|
| 180 | taillen = 0;
|
---|
| 181 | seen = 1;
|
---|
| 182 | newline = 0;
|
---|
| 183 |
|
---|
| 184 | while ((nread = tail_read(fds[i], buf, tailbufsize-taillen)) > 0) {
|
---|
| 185 | if (from_top) {
|
---|
| 186 | nwrite = nread;
|
---|
| 187 | if (seen < count) {
|
---|
[1770] | 188 | if (COUNT_BYTES) {
|
---|
[821] | 189 | nwrite -= (count - seen);
|
---|
| 190 | seen = count;
|
---|
| 191 | } else {
|
---|
| 192 | s = buf;
|
---|
| 193 | do {
|
---|
| 194 | --nwrite;
|
---|
[1770] | 195 | if (*s++ == '\n' && ++seen == count) {
|
---|
[821] | 196 | break;
|
---|
| 197 | }
|
---|
| 198 | } while (nwrite);
|
---|
| 199 | }
|
---|
| 200 | }
|
---|
[1770] | 201 | xwrite(STDOUT_FILENO, buf + nread - nwrite, nwrite);
|
---|
[821] | 202 | } else if (count) {
|
---|
[1770] | 203 | if (COUNT_BYTES) {
|
---|
[821] | 204 | taillen += nread;
|
---|
| 205 | if (taillen > count) {
|
---|
| 206 | memmove(tailbuf, tailbuf + taillen - count, count);
|
---|
| 207 | taillen = count;
|
---|
| 208 | }
|
---|
| 209 | } else {
|
---|
| 210 | int k = nread;
|
---|
| 211 | int nbuf = 0;
|
---|
| 212 |
|
---|
| 213 | while (k) {
|
---|
| 214 | --k;
|
---|
| 215 | if (buf[k] == '\n') {
|
---|
| 216 | ++nbuf;
|
---|
| 217 | }
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | if (newline + nbuf < count) {
|
---|
| 221 | newline += nbuf;
|
---|
| 222 | taillen += nread;
|
---|
| 223 | } else {
|
---|
| 224 | int extra = 0;
|
---|
[1770] | 225 |
|
---|
| 226 | if (buf[nread-1] != '\n')
|
---|
[821] | 227 | extra = 1;
|
---|
| 228 | k = newline + nbuf + extra - count;
|
---|
| 229 | s = tailbuf;
|
---|
| 230 | while (k) {
|
---|
| 231 | if (*s == '\n') {
|
---|
| 232 | --k;
|
---|
| 233 | }
|
---|
| 234 | ++s;
|
---|
| 235 | }
|
---|
| 236 | taillen += nread - (s - tailbuf);
|
---|
| 237 | memmove(tailbuf, s, taillen);
|
---|
| 238 | newline = count - extra;
|
---|
| 239 | }
|
---|
| 240 | if (tailbufsize < taillen + BUFSIZ) {
|
---|
| 241 | tailbufsize = taillen + BUFSIZ;
|
---|
| 242 | tailbuf = xrealloc(tailbuf, tailbufsize);
|
---|
| 243 | }
|
---|
| 244 | }
|
---|
| 245 | buf = tailbuf + taillen;
|
---|
| 246 | }
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | if (!from_top) {
|
---|
[1770] | 250 | xwrite(STDOUT_FILENO, tailbuf, taillen);
|
---|
[821] | 251 | }
|
---|
| 252 |
|
---|
| 253 | taillen = 0;
|
---|
| 254 | } while (++i < nfiles);
|
---|
| 255 |
|
---|
| 256 | buf = xrealloc(tailbuf, BUFSIZ);
|
---|
| 257 |
|
---|
| 258 | fmt = NULL;
|
---|
| 259 |
|
---|
[1770] | 260 | if (FOLLOW) while (1) {
|
---|
[821] | 261 | sleep(sleep_period);
|
---|
| 262 | i = 0;
|
---|
| 263 | do {
|
---|
| 264 | if (nfiles > header_threshhold) {
|
---|
| 265 | fmt = header_fmt;
|
---|
| 266 | }
|
---|
| 267 | while ((nread = tail_read(fds[i], buf, sizeof(buf))) > 0) {
|
---|
| 268 | if (fmt) {
|
---|
| 269 | tail_xprint_header(fmt, argv[i]);
|
---|
| 270 | fmt = NULL;
|
---|
| 271 | }
|
---|
[1770] | 272 | xwrite(STDOUT_FILENO, buf, nread);
|
---|
[821] | 273 | }
|
---|
| 274 | } while (++i < nfiles);
|
---|
| 275 | }
|
---|
[1770] | 276 | if (ENABLE_FEATURE_CLEAN_UP) {
|
---|
| 277 | free(fds);
|
---|
| 278 | }
|
---|
| 279 | return G.status;
|
---|
[821] | 280 | }
|
---|