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 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | * |
Erik Andersen | 3fe39dc | 2000-01-25 18:13:53 +0000 | [diff] [blame] | 21 | */ |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 22 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 23 | /* BB_AUDIT SUSv3 compliant (need fancy for -c) */ |
| 24 | /* BB_AUDIT GNU compatible -c, -q, and -v options in 'fancy' configuration. */ |
| 25 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/tail.html */ |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 26 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 27 | /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org) |
| 28 | * |
| 29 | * Pretty much rewritten to fix numerous bugs and reduce realloc() calls. |
| 30 | * Bugs fixed (although I may have forgotten one or two... it was pretty bad) |
| 31 | * 1) mixing printf/write without fflush()ing stdout |
| 32 | * 2) no check that any open files are present |
| 33 | * 3) optstring had -q taking an arg |
| 34 | * 4) no error checking on write in some cases, and a warning even then |
| 35 | * 5) q and s interaction bug |
| 36 | * 6) no check for lseek error |
| 37 | * 7) lseek attempted when count==0 even if arg was +0 (from top) |
| 38 | */ |
| 39 | |
| 40 | #include <stdio.h> |
| 41 | #include <stdlib.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 42 | #include <string.h> |
Robert Griebl | 53146cc | 2002-05-27 22:24:53 +0000 | [diff] [blame] | 43 | #include <ctype.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 44 | #include <unistd.h> |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 45 | #include <fcntl.h> |
| 46 | #include <sys/stat.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 47 | #include "busybox.h" |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 48 | |
Matt Kraai | a164c64 | 2001-02-05 17:50:03 +0000 | [diff] [blame] | 49 | static const struct suffix_mult tail_suffixes[] = { |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 50 | { "b", 512 }, |
| 51 | { "k", 1024 }, |
| 52 | { "m", 1048576 }, |
| 53 | { NULL, 0 } |
| 54 | }; |
| 55 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 56 | static int status |
| 57 | #if EXIT_SUCCESS != 0 |
| 58 | = EXIT_SUCCESS /* If it is 0 (paranoid check), let bss initialize it. */ |
| 59 | #endif |
| 60 | ; |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 61 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 62 | static void tail_xprint_header(const char *fmt, const char *filename) |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 63 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 64 | /* 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] | 65 | if (dprintf(STDOUT_FILENO, fmt, filename) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 66 | bb_perror_nomsg_and_die(); |
| 67 | } |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 70 | /* len should probably be size_t */ |
| 71 | static void tail_xbb_full_write(const char *buf, size_t len) |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 72 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 73 | /* If we get a write error, there is really no sense in continuing. */ |
| 74 | if (bb_full_write(STDOUT_FILENO, buf, len) < 0) { |
| 75 | bb_perror_nomsg_and_die(); |
| 76 | } |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 79 | static ssize_t tail_read(int fd, char *buf, size_t count) |
| 80 | { |
| 81 | ssize_t r; |
Paul Fox | 4905434 | 2005-07-20 19:46:32 +0000 | [diff] [blame] | 82 | off_t current,end; |
| 83 | struct stat sbuf; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 84 | |
Mike Frysinger | dbc049f | 2005-07-26 22:57:51 +0000 | [diff] [blame] | 85 | end = current = lseek(fd, 0, SEEK_CUR); |
| 86 | if (!fstat(fd, &sbuf)) |
| 87 | end = sbuf.st_size; |
Rob Landley | 58a651b | 2005-08-13 00:35:00 +0000 | [diff] [blame] | 88 | lseek(fd, end < current ? 0 : current, SEEK_SET); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 89 | if ((r = safe_read(fd, buf, count)) < 0) { |
| 90 | bb_perror_msg("read"); |
| 91 | status = EXIT_FAILURE; |
| 92 | } |
| 93 | |
| 94 | return r; |
| 95 | } |
| 96 | |
| 97 | static const char tail_opts[] = |
Glenn L McGrath | 4ef5a84 | 2003-10-31 00:35:59 +0000 | [diff] [blame] | 98 | "fn:c:" |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 99 | #ifdef CONFIG_FEATURE_FANCY_TAIL |
Glenn L McGrath | 4ef5a84 | 2003-10-31 00:35:59 +0000 | [diff] [blame] | 100 | "qs:v" |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 101 | #endif |
| 102 | ; |
| 103 | |
| 104 | static const char header_fmt[] = "\n==> %s <==\n"; |
| 105 | |
Eric Andersen | 98bbd68 | 2000-07-31 17:05:58 +0000 | [diff] [blame] | 106 | int tail_main(int argc, char **argv) |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 107 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 108 | long count = 10; |
| 109 | unsigned int sleep_period = 1; |
| 110 | int from_top = 0; |
| 111 | int follow = 0; |
| 112 | int header_threshhold = 1; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 113 | int count_bytes = 0; |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 114 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 115 | char *tailbuf; |
| 116 | size_t tailbufsize; |
| 117 | int taillen = 0; |
| 118 | int newline = 0; |
| 119 | |
| 120 | int *fds, nfiles, nread, nwrite, seen, i, opt; |
| 121 | char *s, *buf; |
| 122 | const char *fmt; |
| 123 | |
| 124 | /* Allow legacy syntax of an initial numeric option without -n. */ |
Eric Andersen | b5b5ac3 | 2003-03-28 16:54:14 +0000 | [diff] [blame] | 125 | if (argc >=2 && ((argv[1][0] == '+') || ((argv[1][0] == '-') |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 126 | /* && (isdigit)(argv[1][1]) */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 127 | && (((unsigned int)(argv[1][1] - '0')) <= 9)))) |
Eric Andersen | b5b5ac3 | 2003-03-28 16:54:14 +0000 | [diff] [blame] | 128 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 129 | optind = 2; |
| 130 | optarg = argv[1]; |
| 131 | goto GET_COUNT; |
Robert Griebl | 13c26fc | 2002-05-17 22:18:04 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 134 | while ((opt = getopt(argc, argv, tail_opts)) > 0) { |
Eric Andersen | d5fa3e3 | 2000-08-02 16:42:58 +0000 | [diff] [blame] | 135 | switch (opt) { |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 136 | case 'f': |
| 137 | follow = 1; |
| 138 | break; |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 139 | case 'c': |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 140 | count_bytes = 1; |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 141 | /* FALLS THROUGH */ |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 142 | case 'n': |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 143 | GET_COUNT: |
| 144 | count = bb_xgetlarg10_sfx(optarg, tail_suffixes); |
| 145 | /* Note: Leading whitespace is an error trapped above. */ |
| 146 | if (*optarg == '+') { |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 147 | from_top = 1; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 148 | } else { |
| 149 | from_top = 0; |
| 150 | } |
| 151 | if (count < 0) { |
| 152 | count = -count; |
| 153 | } |
Eric Andersen | d5fa3e3 | 2000-08-02 16:42:58 +0000 | [diff] [blame] | 154 | break; |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 155 | #ifdef CONFIG_FEATURE_FANCY_TAIL |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 156 | case 'q': |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 157 | header_threshhold = INT_MAX; |
Eric Andersen | d5fa3e3 | 2000-08-02 16:42:58 +0000 | [diff] [blame] | 158 | break; |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 159 | case 's': |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 160 | sleep_period =bb_xgetularg10_bnd(optarg, 0, UINT_MAX); |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 161 | break; |
| 162 | case 'v': |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 163 | header_threshhold = 0; |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 164 | break; |
| 165 | #endif |
| 166 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 167 | bb_show_usage(); |
Eric Andersen | d5fa3e3 | 2000-08-02 16:42:58 +0000 | [diff] [blame] | 168 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 169 | } |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 170 | |
| 171 | /* open all the files */ |
| 172 | fds = (int *)xmalloc(sizeof(int) * (argc - optind + 1)); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 173 | |
| 174 | argv += optind; |
| 175 | nfiles = i = 0; |
| 176 | |
| 177 | if ((argc -= optind) == 0) { |
| 178 | struct stat statbuf; |
| 179 | |
| 180 | if (!fstat(STDIN_FILENO, &statbuf) && S_ISFIFO(statbuf.st_mode)) { |
| 181 | follow = 0; |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 182 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 183 | /* --argv; */ |
| 184 | *argv = (char *) bb_msg_standard_input; |
| 185 | goto DO_STDIN; |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 186 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 187 | |
| 188 | do { |
| 189 | if ((argv[i][0] == '-') && !argv[i][1]) { |
| 190 | DO_STDIN: |
| 191 | fds[nfiles] = STDIN_FILENO; |
| 192 | } else if ((fds[nfiles] = open(argv[i], O_RDONLY)) < 0) { |
| 193 | bb_perror_msg("%s", argv[i]); |
| 194 | status = EXIT_FAILURE; |
| 195 | continue; |
| 196 | } |
| 197 | argv[nfiles] = argv[i]; |
| 198 | ++nfiles; |
| 199 | } while (++i < argc); |
| 200 | |
| 201 | if (!nfiles) { |
| 202 | bb_error_msg_and_die("no files"); |
| 203 | } |
| 204 | |
| 205 | tailbufsize = BUFSIZ; |
Glenn L McGrath | 4ef5a84 | 2003-10-31 00:35:59 +0000 | [diff] [blame] | 206 | |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 207 | /* tail the files */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 208 | if (from_top < count_bytes) { /* Each is 0 or 1, so true iff 0 < 1. */ |
| 209 | /* Hence, !from_top && count_bytes */ |
| 210 | if (tailbufsize < count) { |
| 211 | tailbufsize = count + BUFSIZ; |
| 212 | } |
| 213 | } |
Glenn L McGrath | 4ef5a84 | 2003-10-31 00:35:59 +0000 | [diff] [blame] | 214 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 215 | buf = tailbuf = xmalloc(tailbufsize); |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 216 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 217 | fmt = header_fmt + 1; /* Skip header leading newline on first output. */ |
| 218 | i = 0; |
| 219 | do { |
| 220 | /* Be careful. It would be possible to optimize the count-bytes |
| 221 | * case if the file is seekable. If you do though, remember that |
| 222 | * starting file position may not be the beginning of the file. |
| 223 | * Beware of backing up too far. See example in wc.c. |
| 224 | */ |
| 225 | if ((!(count|from_top)) && (lseek(fds[i], 0, SEEK_END) >= 0)) { |
Eric Andersen | ce98c19 | 2001-06-26 15:07:08 +0000 | [diff] [blame] | 226 | continue; |
| 227 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 228 | |
| 229 | if (nfiles > header_threshhold) { |
| 230 | tail_xprint_header(fmt, argv[i]); |
| 231 | fmt = header_fmt; |
| 232 | } |
| 233 | |
| 234 | buf = tailbuf; |
| 235 | taillen = 0; |
| 236 | seen = 1; |
Glenn L McGrath | f86391e | 2004-09-30 00:24:21 +0000 | [diff] [blame] | 237 | newline = 0; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 238 | |
| 239 | while ((nread = tail_read(fds[i], buf, tailbufsize-taillen)) > 0) { |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 240 | if (from_top) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 241 | nwrite = nread; |
| 242 | if (seen < count) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 243 | if (count_bytes) { |
| 244 | nwrite -= (count - seen); |
| 245 | seen = count; |
Glenn L McGrath | 4ef5a84 | 2003-10-31 00:35:59 +0000 | [diff] [blame] | 246 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 247 | s = buf; |
| 248 | do { |
| 249 | --nwrite; |
| 250 | if ((*s++ == '\n') && (++seen == count)) { |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 251 | break; |
| 252 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 253 | } while (nwrite); |
| 254 | } |
| 255 | } |
| 256 | tail_xbb_full_write(buf + nread - nwrite, nwrite); |
| 257 | } else if (count) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 258 | if (count_bytes) { |
| 259 | taillen += nread; |
| 260 | if (taillen > count) { |
| 261 | memmove(tailbuf, tailbuf + taillen - count, count); |
| 262 | taillen = count; |
| 263 | } |
Glenn L McGrath | 4ef5a84 | 2003-10-31 00:35:59 +0000 | [diff] [blame] | 264 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 265 | int k = nread; |
| 266 | int nbuf = 0; |
| 267 | |
| 268 | while (k) { |
| 269 | --k; |
| 270 | if (buf[k] == '\n') { |
| 271 | ++nbuf; |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 272 | } |
| 273 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 274 | |
| 275 | if (newline + nbuf < count) { |
| 276 | newline += nbuf; |
| 277 | taillen += nread; |
| 278 | |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 279 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 280 | int extra = 0; |
| 281 | if (buf[nread-1] != '\n') { |
| 282 | extra = 1; |
| 283 | } |
| 284 | |
| 285 | k = newline + nbuf + extra - count; |
| 286 | s = tailbuf; |
| 287 | while (k) { |
| 288 | if (*s == '\n') { |
| 289 | --k; |
| 290 | } |
| 291 | ++s; |
| 292 | } |
| 293 | |
| 294 | taillen += nread - (s - tailbuf); |
| 295 | memmove(tailbuf, s, taillen); |
| 296 | newline = count - extra; |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 297 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 298 | if (tailbufsize < taillen + BUFSIZ) { |
| 299 | tailbufsize = taillen + BUFSIZ; |
| 300 | tailbuf = xrealloc(tailbuf, tailbufsize); |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 301 | } |
Eric Andersen | d5fa3e3 | 2000-08-02 16:42:58 +0000 | [diff] [blame] | 302 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 303 | buf = tailbuf + taillen; |
Eric Andersen | d5fa3e3 | 2000-08-02 16:42:58 +0000 | [diff] [blame] | 304 | } |
| 305 | } |
Erik Andersen | 3fe39dc | 2000-01-25 18:13:53 +0000 | [diff] [blame] | 306 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 307 | if (!from_top) { |
| 308 | tail_xbb_full_write(tailbuf, taillen); |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | taillen = 0; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 312 | } while (++i < nfiles); |
| 313 | |
| 314 | buf = xrealloc(tailbuf, BUFSIZ); |
| 315 | |
| 316 | fmt = NULL; |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 317 | |
| 318 | while (follow) { |
| 319 | sleep(sleep_period); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 320 | i = 0; |
| 321 | do { |
| 322 | if (nfiles > header_threshhold) { |
| 323 | fmt = header_fmt; |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 324 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 325 | while ((nread = tail_read(fds[i], buf, sizeof(buf))) > 0) { |
| 326 | if (fmt) { |
| 327 | tail_xprint_header(fmt, argv[i]); |
| 328 | fmt = NULL; |
| 329 | } |
| 330 | tail_xbb_full_write(buf, nread); |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 331 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 332 | } while (++i < nfiles); |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | return status; |
| 336 | } |