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