Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini tail implementation for busybox |
| 4 | * |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 5 | * Copyright (C) 2001 by Matt Kraai <kraai@alumni.carnegiemellon.edu> |
| 6 | * |
Rob Landley | f8fd4db | 2006-01-30 01:30:39 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Erik Andersen | 3fe39dc | 2000-01-25 18:13:53 +0000 | [diff] [blame] | 8 | */ |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 9 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 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 */ |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 13 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 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 | |
| 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 29 | #include <string.h> |
Robert Griebl | 53146cc | 2002-05-27 22:24:53 +0000 | [diff] [blame] | 30 | #include <ctype.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 31 | #include <unistd.h> |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 32 | #include <fcntl.h> |
| 33 | #include <sys/stat.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 34 | #include "busybox.h" |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 35 | |
Matt Kraai | a164c64 | 2001-02-05 17:50:03 +0000 | [diff] [blame] | 36 | static const struct suffix_mult tail_suffixes[] = { |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 37 | { "b", 512 }, |
| 38 | { "k", 1024 }, |
| 39 | { "m", 1048576 }, |
| 40 | { NULL, 0 } |
| 41 | }; |
| 42 | |
Rob Landley | f8fd4db | 2006-01-30 01:30:39 +0000 | [diff] [blame] | 43 | static int status; |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 44 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 45 | static void tail_xprint_header(const char *fmt, const char *filename) |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 46 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 47 | /* If we get an output error, there is really no sense in continuing. */ |
Rob Landley | b9dfb8c | 2005-05-07 17:45:38 +0000 | [diff] [blame] | 48 | if (dprintf(STDOUT_FILENO, fmt, filename) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 49 | bb_perror_nomsg_and_die(); |
| 50 | } |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 53 | /* len should probably be size_t */ |
| 54 | static void tail_xbb_full_write(const char *buf, size_t len) |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 55 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 56 | /* If we get a write error, there is really no sense in continuing. */ |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 57 | if (full_write(STDOUT_FILENO, buf, len) < 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 58 | bb_perror_nomsg_and_die(); |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 61 | static ssize_t tail_read(int fd, char *buf, size_t count) |
| 62 | { |
| 63 | ssize_t r; |
Paul Fox | 4905434 | 2005-07-20 19:46:32 +0000 | [diff] [blame] | 64 | off_t current,end; |
| 65 | struct stat sbuf; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 66 | |
Mike Frysinger | dbc049f | 2005-07-26 22:57:51 +0000 | [diff] [blame] | 67 | end = current = lseek(fd, 0, SEEK_CUR); |
| 68 | if (!fstat(fd, &sbuf)) |
| 69 | end = sbuf.st_size; |
Rob Landley | 58a651b | 2005-08-13 00:35:00 +0000 | [diff] [blame] | 70 | lseek(fd, end < current ? 0 : current, SEEK_SET); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 71 | if ((r = safe_read(fd, buf, count)) < 0) { |
Bernhard Reutner-Fischer | 1b9d7c9 | 2006-06-03 22:45:37 +0000 | [diff] [blame] | 72 | bb_perror_msg(bb_msg_read_error); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 73 | status = EXIT_FAILURE; |
| 74 | } |
| 75 | |
| 76 | return r; |
| 77 | } |
| 78 | |
| 79 | static const char tail_opts[] = |
Glenn L McGrath | 4ef5a84 | 2003-10-31 00:35:59 +0000 | [diff] [blame] | 80 | "fn:c:" |
Bernhard Reutner-Fischer | 5816ccb | 2005-12-13 10:48:45 +0000 | [diff] [blame] | 81 | #if ENABLE_FEATURE_FANCY_TAIL |
Glenn L McGrath | 4ef5a84 | 2003-10-31 00:35:59 +0000 | [diff] [blame] | 82 | "qs:v" |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 83 | #endif |
| 84 | ; |
| 85 | |
| 86 | static const char header_fmt[] = "\n==> %s <==\n"; |
| 87 | |
Eric Andersen | 98bbd68 | 2000-07-31 17:05:58 +0000 | [diff] [blame] | 88 | int tail_main(int argc, char **argv) |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 89 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 90 | long count = 10; |
| 91 | unsigned int sleep_period = 1; |
| 92 | int from_top = 0; |
| 93 | int follow = 0; |
| 94 | int header_threshhold = 1; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 95 | int count_bytes = 0; |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 96 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 97 | char *tailbuf; |
| 98 | size_t tailbufsize; |
| 99 | int taillen = 0; |
| 100 | int newline = 0; |
| 101 | |
| 102 | int *fds, nfiles, nread, nwrite, seen, i, opt; |
| 103 | char *s, *buf; |
| 104 | const char *fmt; |
| 105 | |
Rob Landley | f8fd4db | 2006-01-30 01:30:39 +0000 | [diff] [blame] | 106 | #if !ENABLE_DEBUG_YANK_SUSv2 || ENABLE_FEATURE_FANCY_TAIL |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 107 | /* Allow legacy syntax of an initial numeric option without -n. */ |
Eric Andersen | b5b5ac3 | 2003-03-28 16:54:14 +0000 | [diff] [blame] | 108 | if (argc >=2 && ((argv[1][0] == '+') || ((argv[1][0] == '-') |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 109 | /* && (isdigit)(argv[1][1]) */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 110 | && (((unsigned int)(argv[1][1] - '0')) <= 9)))) |
Eric Andersen | b5b5ac3 | 2003-03-28 16:54:14 +0000 | [diff] [blame] | 111 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 112 | optind = 2; |
| 113 | optarg = argv[1]; |
| 114 | goto GET_COUNT; |
Robert Griebl | 13c26fc | 2002-05-17 22:18:04 +0000 | [diff] [blame] | 115 | } |
Glenn L McGrath | 0bd0257 | 2005-12-11 03:09:05 +0000 | [diff] [blame] | 116 | #endif |
Robert Griebl | 13c26fc | 2002-05-17 22:18:04 +0000 | [diff] [blame] | 117 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 118 | while ((opt = getopt(argc, argv, tail_opts)) > 0) { |
Eric Andersen | d5fa3e3 | 2000-08-02 16:42:58 +0000 | [diff] [blame] | 119 | switch (opt) { |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 120 | case 'f': |
| 121 | follow = 1; |
| 122 | break; |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 123 | case 'c': |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 124 | count_bytes = 1; |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 125 | /* FALLS THROUGH */ |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 126 | case 'n': |
Rob Landley | f8fd4db | 2006-01-30 01:30:39 +0000 | [diff] [blame] | 127 | #if !ENABLE_DEBUG_YANK_SUSv2 || ENABLE_FEATURE_FANCY_TAIL |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 128 | GET_COUNT: |
Bernhard Reutner-Fischer | 5816ccb | 2005-12-13 10:48:45 +0000 | [diff] [blame] | 129 | #endif |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 130 | count = bb_xgetlarg10_sfx(optarg, tail_suffixes); |
| 131 | /* Note: Leading whitespace is an error trapped above. */ |
| 132 | if (*optarg == '+') { |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 133 | from_top = 1; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 134 | } else { |
| 135 | from_top = 0; |
| 136 | } |
| 137 | if (count < 0) { |
| 138 | count = -count; |
| 139 | } |
Eric Andersen | d5fa3e3 | 2000-08-02 16:42:58 +0000 | [diff] [blame] | 140 | break; |
Bernhard Reutner-Fischer | 5816ccb | 2005-12-13 10:48:45 +0000 | [diff] [blame] | 141 | #if ENABLE_FEATURE_FANCY_TAIL |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 142 | case 'q': |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 143 | header_threshhold = INT_MAX; |
Eric Andersen | d5fa3e3 | 2000-08-02 16:42:58 +0000 | [diff] [blame] | 144 | break; |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 145 | case 's': |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 146 | sleep_period =bb_xgetularg10_bnd(optarg, 0, UINT_MAX); |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 147 | break; |
| 148 | case 'v': |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 149 | header_threshhold = 0; |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 150 | break; |
| 151 | #endif |
| 152 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 153 | bb_show_usage(); |
Eric Andersen | d5fa3e3 | 2000-08-02 16:42:58 +0000 | [diff] [blame] | 154 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 155 | } |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 156 | |
| 157 | /* open all the files */ |
| 158 | fds = (int *)xmalloc(sizeof(int) * (argc - optind + 1)); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 159 | |
| 160 | argv += optind; |
| 161 | nfiles = i = 0; |
| 162 | |
| 163 | if ((argc -= optind) == 0) { |
| 164 | struct stat statbuf; |
| 165 | |
| 166 | if (!fstat(STDIN_FILENO, &statbuf) && S_ISFIFO(statbuf.st_mode)) { |
| 167 | follow = 0; |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 168 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 169 | /* --argv; */ |
| 170 | *argv = (char *) bb_msg_standard_input; |
| 171 | goto DO_STDIN; |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 172 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 173 | |
| 174 | do { |
| 175 | if ((argv[i][0] == '-') && !argv[i][1]) { |
| 176 | DO_STDIN: |
| 177 | fds[nfiles] = STDIN_FILENO; |
| 178 | } else if ((fds[nfiles] = open(argv[i], O_RDONLY)) < 0) { |
| 179 | bb_perror_msg("%s", argv[i]); |
| 180 | status = EXIT_FAILURE; |
| 181 | continue; |
| 182 | } |
| 183 | argv[nfiles] = argv[i]; |
| 184 | ++nfiles; |
| 185 | } while (++i < argc); |
| 186 | |
| 187 | if (!nfiles) { |
| 188 | bb_error_msg_and_die("no files"); |
| 189 | } |
| 190 | |
| 191 | tailbufsize = BUFSIZ; |
Glenn L McGrath | 4ef5a84 | 2003-10-31 00:35:59 +0000 | [diff] [blame] | 192 | |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 193 | /* tail the files */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 194 | if (from_top < count_bytes) { /* Each is 0 or 1, so true iff 0 < 1. */ |
| 195 | /* Hence, !from_top && count_bytes */ |
| 196 | if (tailbufsize < count) { |
| 197 | tailbufsize = count + BUFSIZ; |
| 198 | } |
| 199 | } |
Glenn L McGrath | 4ef5a84 | 2003-10-31 00:35:59 +0000 | [diff] [blame] | 200 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 201 | buf = tailbuf = xmalloc(tailbufsize); |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 202 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 203 | fmt = header_fmt + 1; /* Skip header leading newline on first output. */ |
| 204 | i = 0; |
| 205 | do { |
| 206 | /* Be careful. It would be possible to optimize the count-bytes |
| 207 | * case if the file is seekable. If you do though, remember that |
| 208 | * starting file position may not be the beginning of the file. |
| 209 | * Beware of backing up too far. See example in wc.c. |
| 210 | */ |
| 211 | if ((!(count|from_top)) && (lseek(fds[i], 0, SEEK_END) >= 0)) { |
Eric Andersen | ce98c19 | 2001-06-26 15:07:08 +0000 | [diff] [blame] | 212 | continue; |
| 213 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 214 | |
| 215 | if (nfiles > header_threshhold) { |
| 216 | tail_xprint_header(fmt, argv[i]); |
| 217 | fmt = header_fmt; |
| 218 | } |
| 219 | |
| 220 | buf = tailbuf; |
| 221 | taillen = 0; |
| 222 | seen = 1; |
Glenn L McGrath | f86391e | 2004-09-30 00:24:21 +0000 | [diff] [blame] | 223 | newline = 0; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 224 | |
| 225 | while ((nread = tail_read(fds[i], buf, tailbufsize-taillen)) > 0) { |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 226 | if (from_top) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 227 | nwrite = nread; |
| 228 | if (seen < count) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 229 | if (count_bytes) { |
| 230 | nwrite -= (count - seen); |
| 231 | seen = count; |
Glenn L McGrath | 4ef5a84 | 2003-10-31 00:35:59 +0000 | [diff] [blame] | 232 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 233 | s = buf; |
| 234 | do { |
| 235 | --nwrite; |
| 236 | if ((*s++ == '\n') && (++seen == count)) { |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 237 | break; |
| 238 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 239 | } while (nwrite); |
| 240 | } |
| 241 | } |
| 242 | tail_xbb_full_write(buf + nread - nwrite, nwrite); |
| 243 | } else if (count) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 244 | if (count_bytes) { |
| 245 | taillen += nread; |
| 246 | if (taillen > count) { |
| 247 | memmove(tailbuf, tailbuf + taillen - count, count); |
| 248 | taillen = count; |
| 249 | } |
Glenn L McGrath | 4ef5a84 | 2003-10-31 00:35:59 +0000 | [diff] [blame] | 250 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 251 | int k = nread; |
| 252 | int nbuf = 0; |
| 253 | |
| 254 | while (k) { |
| 255 | --k; |
| 256 | if (buf[k] == '\n') { |
| 257 | ++nbuf; |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 258 | } |
| 259 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 260 | |
| 261 | if (newline + nbuf < count) { |
| 262 | newline += nbuf; |
| 263 | taillen += nread; |
| 264 | |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 265 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 266 | int extra = 0; |
| 267 | if (buf[nread-1] != '\n') { |
| 268 | extra = 1; |
| 269 | } |
| 270 | |
| 271 | k = newline + nbuf + extra - count; |
| 272 | s = tailbuf; |
| 273 | while (k) { |
| 274 | if (*s == '\n') { |
| 275 | --k; |
| 276 | } |
| 277 | ++s; |
| 278 | } |
| 279 | |
| 280 | taillen += nread - (s - tailbuf); |
| 281 | memmove(tailbuf, s, taillen); |
| 282 | newline = count - extra; |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 283 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 284 | if (tailbufsize < taillen + BUFSIZ) { |
| 285 | tailbufsize = taillen + BUFSIZ; |
| 286 | tailbuf = xrealloc(tailbuf, tailbufsize); |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 287 | } |
Eric Andersen | d5fa3e3 | 2000-08-02 16:42:58 +0000 | [diff] [blame] | 288 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 289 | buf = tailbuf + taillen; |
Eric Andersen | d5fa3e3 | 2000-08-02 16:42:58 +0000 | [diff] [blame] | 290 | } |
| 291 | } |
Erik Andersen | 3fe39dc | 2000-01-25 18:13:53 +0000 | [diff] [blame] | 292 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 293 | if (!from_top) { |
| 294 | tail_xbb_full_write(tailbuf, taillen); |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | taillen = 0; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 298 | } while (++i < nfiles); |
| 299 | |
| 300 | buf = xrealloc(tailbuf, BUFSIZ); |
| 301 | |
| 302 | fmt = NULL; |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 303 | |
| 304 | while (follow) { |
| 305 | sleep(sleep_period); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 306 | i = 0; |
| 307 | do { |
| 308 | if (nfiles > header_threshhold) { |
| 309 | fmt = header_fmt; |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 310 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 311 | while ((nread = tail_read(fds[i], buf, sizeof(buf))) > 0) { |
| 312 | if (fmt) { |
| 313 | tail_xprint_header(fmt, argv[i]); |
| 314 | fmt = NULL; |
| 315 | } |
| 316 | tail_xbb_full_write(buf, nread); |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 317 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 318 | } while (++i < nfiles); |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | return status; |
| 322 | } |