blob: 48abc4b847c7460b278fc5b0ae89a34c2a6f9118 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Matt Kraai55bccf32001-01-05 02:57:53 +00002/*
3 * Mini tail implementation for busybox
4 *
Matt Kraai55bccf32001-01-05 02:57:53 +00005 * Copyright (C) 2001 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
6 *
Rob Landleyf8fd4db2006-01-30 01:30:39 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Erik Andersen3fe39dc2000-01-25 18:13:53 +00008 */
Matt Kraai55bccf32001-01-05 02:57:53 +00009
Manuel Novoa III cad53642003-03-19 09:13:01 +000010/* 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 Andersenabc0f4f1999-12-08 23:19:36 +000013
Manuel Novoa III cad53642003-03-19 09:13:01 +000014/* 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 Andersened3ef502001-01-27 08:24:39 +000029#include <string.h>
Robert Griebl53146cc2002-05-27 22:24:53 +000030#include <ctype.h>
Eric Andersened3ef502001-01-27 08:24:39 +000031#include <unistd.h>
Manuel Novoa III cad53642003-03-19 09:13:01 +000032#include <fcntl.h>
33#include <sys/stat.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000034#include "busybox.h"
Eric Andersenabc0f4f1999-12-08 23:19:36 +000035
Matt Kraaia164c642001-02-05 17:50:03 +000036static const struct suffix_mult tail_suffixes[] = {
Matt Kraai24ac0172000-12-18 21:38:57 +000037 { "b", 512 },
38 { "k", 1024 },
39 { "m", 1048576 },
40 { NULL, 0 }
41};
42
Rob Landleyf8fd4db2006-01-30 01:30:39 +000043static int status;
Matt Kraai55bccf32001-01-05 02:57:53 +000044
Manuel Novoa III cad53642003-03-19 09:13:01 +000045static void tail_xprint_header(const char *fmt, const char *filename)
Matt Kraai55bccf32001-01-05 02:57:53 +000046{
Manuel Novoa III cad53642003-03-19 09:13:01 +000047 /* If we get an output error, there is really no sense in continuing. */
Rob Landleyb9dfb8c2005-05-07 17:45:38 +000048 if (dprintf(STDOUT_FILENO, fmt, filename) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000049 bb_perror_nomsg_and_die();
50 }
Eric Andersenabc0f4f1999-12-08 23:19:36 +000051}
52
Manuel Novoa III cad53642003-03-19 09:13:01 +000053/* len should probably be size_t */
54static void tail_xbb_full_write(const char *buf, size_t len)
Eric Andersenabc0f4f1999-12-08 23:19:36 +000055{
Manuel Novoa III cad53642003-03-19 09:13:01 +000056 /* If we get a write error, there is really no sense in continuing. */
57 if (bb_full_write(STDOUT_FILENO, buf, len) < 0) {
58 bb_perror_nomsg_and_die();
59 }
Eric Andersenabc0f4f1999-12-08 23:19:36 +000060}
61
Manuel Novoa III cad53642003-03-19 09:13:01 +000062static ssize_t tail_read(int fd, char *buf, size_t count)
63{
64 ssize_t r;
Paul Fox49054342005-07-20 19:46:32 +000065 off_t current,end;
66 struct stat sbuf;
Manuel Novoa III cad53642003-03-19 09:13:01 +000067
Mike Frysingerdbc049f2005-07-26 22:57:51 +000068 end = current = lseek(fd, 0, SEEK_CUR);
69 if (!fstat(fd, &sbuf))
70 end = sbuf.st_size;
Rob Landley58a651b2005-08-13 00:35:00 +000071 lseek(fd, end < current ? 0 : current, SEEK_SET);
Manuel Novoa III cad53642003-03-19 09:13:01 +000072 if ((r = safe_read(fd, buf, count)) < 0) {
73 bb_perror_msg("read");
74 status = EXIT_FAILURE;
75 }
76
77 return r;
78}
79
80static const char tail_opts[] =
Glenn L McGrath4ef5a842003-10-31 00:35:59 +000081 "fn:c:"
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +000082#if ENABLE_FEATURE_FANCY_TAIL
Glenn L McGrath4ef5a842003-10-31 00:35:59 +000083 "qs:v"
Manuel Novoa III cad53642003-03-19 09:13:01 +000084#endif
85 ;
86
87static const char header_fmt[] = "\n==> %s <==\n";
88
Eric Andersen98bbd682000-07-31 17:05:58 +000089int tail_main(int argc, char **argv)
Eric Andersenabc0f4f1999-12-08 23:19:36 +000090{
Manuel Novoa III cad53642003-03-19 09:13:01 +000091 long count = 10;
92 unsigned int sleep_period = 1;
93 int from_top = 0;
94 int follow = 0;
95 int header_threshhold = 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +000096 int count_bytes = 0;
Eric Andersenabc0f4f1999-12-08 23:19:36 +000097
Manuel Novoa III cad53642003-03-19 09:13:01 +000098 char *tailbuf;
99 size_t tailbufsize;
100 int taillen = 0;
101 int newline = 0;
102
103 int *fds, nfiles, nread, nwrite, seen, i, opt;
104 char *s, *buf;
105 const char *fmt;
106
Rob Landleyf8fd4db2006-01-30 01:30:39 +0000107#if !ENABLE_DEBUG_YANK_SUSv2 || ENABLE_FEATURE_FANCY_TAIL
Manuel Novoa III cad53642003-03-19 09:13:01 +0000108 /* Allow legacy syntax of an initial numeric option without -n. */
Eric Andersenb5b5ac32003-03-28 16:54:14 +0000109 if (argc >=2 && ((argv[1][0] == '+') || ((argv[1][0] == '-')
Manuel Novoa III cad53642003-03-19 09:13:01 +0000110 /* && (isdigit)(argv[1][1]) */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000111 && (((unsigned int)(argv[1][1] - '0')) <= 9))))
Eric Andersenb5b5ac32003-03-28 16:54:14 +0000112 {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000113 optind = 2;
114 optarg = argv[1];
115 goto GET_COUNT;
Robert Griebl13c26fc2002-05-17 22:18:04 +0000116 }
Glenn L McGrath0bd02572005-12-11 03:09:05 +0000117#endif
Robert Griebl13c26fc2002-05-17 22:18:04 +0000118
Manuel Novoa III cad53642003-03-19 09:13:01 +0000119 while ((opt = getopt(argc, argv, tail_opts)) > 0) {
Eric Andersend5fa3e32000-08-02 16:42:58 +0000120 switch (opt) {
Matt Kraai55bccf32001-01-05 02:57:53 +0000121 case 'f':
122 follow = 1;
123 break;
Matt Kraai55bccf32001-01-05 02:57:53 +0000124 case 'c':
Manuel Novoa III cad53642003-03-19 09:13:01 +0000125 count_bytes = 1;
Matt Kraai55bccf32001-01-05 02:57:53 +0000126 /* FALLS THROUGH */
Matt Kraai55bccf32001-01-05 02:57:53 +0000127 case 'n':
Rob Landleyf8fd4db2006-01-30 01:30:39 +0000128#if !ENABLE_DEBUG_YANK_SUSv2 || ENABLE_FEATURE_FANCY_TAIL
Manuel Novoa III cad53642003-03-19 09:13:01 +0000129 GET_COUNT:
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +0000130#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +0000131 count = bb_xgetlarg10_sfx(optarg, tail_suffixes);
132 /* Note: Leading whitespace is an error trapped above. */
133 if (*optarg == '+') {
Matt Kraai55bccf32001-01-05 02:57:53 +0000134 from_top = 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000135 } else {
136 from_top = 0;
137 }
138 if (count < 0) {
139 count = -count;
140 }
Eric Andersend5fa3e32000-08-02 16:42:58 +0000141 break;
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +0000142#if ENABLE_FEATURE_FANCY_TAIL
Matt Kraai55bccf32001-01-05 02:57:53 +0000143 case 'q':
Manuel Novoa III cad53642003-03-19 09:13:01 +0000144 header_threshhold = INT_MAX;
Eric Andersend5fa3e32000-08-02 16:42:58 +0000145 break;
Matt Kraai55bccf32001-01-05 02:57:53 +0000146 case 's':
Manuel Novoa III cad53642003-03-19 09:13:01 +0000147 sleep_period =bb_xgetularg10_bnd(optarg, 0, UINT_MAX);
Matt Kraai55bccf32001-01-05 02:57:53 +0000148 break;
149 case 'v':
Manuel Novoa III cad53642003-03-19 09:13:01 +0000150 header_threshhold = 0;
Matt Kraai55bccf32001-01-05 02:57:53 +0000151 break;
152#endif
153 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000154 bb_show_usage();
Eric Andersend5fa3e32000-08-02 16:42:58 +0000155 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000156 }
Matt Kraai55bccf32001-01-05 02:57:53 +0000157
158 /* open all the files */
159 fds = (int *)xmalloc(sizeof(int) * (argc - optind + 1));
Manuel Novoa III cad53642003-03-19 09:13:01 +0000160
161 argv += optind;
162 nfiles = i = 0;
163
164 if ((argc -= optind) == 0) {
165 struct stat statbuf;
166
167 if (!fstat(STDIN_FILENO, &statbuf) && S_ISFIFO(statbuf.st_mode)) {
168 follow = 0;
Matt Kraai55bccf32001-01-05 02:57:53 +0000169 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000170 /* --argv; */
171 *argv = (char *) bb_msg_standard_input;
172 goto DO_STDIN;
Matt Kraai55bccf32001-01-05 02:57:53 +0000173 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000174
175 do {
176 if ((argv[i][0] == '-') && !argv[i][1]) {
177 DO_STDIN:
178 fds[nfiles] = STDIN_FILENO;
179 } else if ((fds[nfiles] = open(argv[i], O_RDONLY)) < 0) {
180 bb_perror_msg("%s", argv[i]);
181 status = EXIT_FAILURE;
182 continue;
183 }
184 argv[nfiles] = argv[i];
185 ++nfiles;
186 } while (++i < argc);
187
188 if (!nfiles) {
189 bb_error_msg_and_die("no files");
190 }
191
192 tailbufsize = BUFSIZ;
Glenn L McGrath4ef5a842003-10-31 00:35:59 +0000193
Matt Kraai55bccf32001-01-05 02:57:53 +0000194 /* tail the files */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000195 if (from_top < count_bytes) { /* Each is 0 or 1, so true iff 0 < 1. */
196 /* Hence, !from_top && count_bytes */
197 if (tailbufsize < count) {
198 tailbufsize = count + BUFSIZ;
199 }
200 }
Glenn L McGrath4ef5a842003-10-31 00:35:59 +0000201
Manuel Novoa III cad53642003-03-19 09:13:01 +0000202 buf = tailbuf = xmalloc(tailbufsize);
Matt Kraai55bccf32001-01-05 02:57:53 +0000203
Manuel Novoa III cad53642003-03-19 09:13:01 +0000204 fmt = header_fmt + 1; /* Skip header leading newline on first output. */
205 i = 0;
206 do {
207 /* Be careful. It would be possible to optimize the count-bytes
208 * case if the file is seekable. If you do though, remember that
209 * starting file position may not be the beginning of the file.
210 * Beware of backing up too far. See example in wc.c.
211 */
212 if ((!(count|from_top)) && (lseek(fds[i], 0, SEEK_END) >= 0)) {
Eric Andersence98c192001-06-26 15:07:08 +0000213 continue;
214 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000215
216 if (nfiles > header_threshhold) {
217 tail_xprint_header(fmt, argv[i]);
218 fmt = header_fmt;
219 }
220
221 buf = tailbuf;
222 taillen = 0;
223 seen = 1;
Glenn L McGrathf86391e2004-09-30 00:24:21 +0000224 newline = 0;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000225
226 while ((nread = tail_read(fds[i], buf, tailbufsize-taillen)) > 0) {
Matt Kraai55bccf32001-01-05 02:57:53 +0000227 if (from_top) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000228 nwrite = nread;
229 if (seen < count) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000230 if (count_bytes) {
231 nwrite -= (count - seen);
232 seen = count;
Glenn L McGrath4ef5a842003-10-31 00:35:59 +0000233 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000234 s = buf;
235 do {
236 --nwrite;
237 if ((*s++ == '\n') && (++seen == count)) {
Matt Kraai55bccf32001-01-05 02:57:53 +0000238 break;
239 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000240 } while (nwrite);
241 }
242 }
243 tail_xbb_full_write(buf + nread - nwrite, nwrite);
244 } else if (count) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000245 if (count_bytes) {
246 taillen += nread;
247 if (taillen > count) {
248 memmove(tailbuf, tailbuf + taillen - count, count);
249 taillen = count;
250 }
Glenn L McGrath4ef5a842003-10-31 00:35:59 +0000251 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000252 int k = nread;
253 int nbuf = 0;
254
255 while (k) {
256 --k;
257 if (buf[k] == '\n') {
258 ++nbuf;
Matt Kraai55bccf32001-01-05 02:57:53 +0000259 }
260 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000261
262 if (newline + nbuf < count) {
263 newline += nbuf;
264 taillen += nread;
265
Matt Kraai55bccf32001-01-05 02:57:53 +0000266 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000267 int extra = 0;
268 if (buf[nread-1] != '\n') {
269 extra = 1;
270 }
271
272 k = newline + nbuf + extra - count;
273 s = tailbuf;
274 while (k) {
275 if (*s == '\n') {
276 --k;
277 }
278 ++s;
279 }
280
281 taillen += nread - (s - tailbuf);
282 memmove(tailbuf, s, taillen);
283 newline = count - extra;
Matt Kraai55bccf32001-01-05 02:57:53 +0000284 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000285 if (tailbufsize < taillen + BUFSIZ) {
286 tailbufsize = taillen + BUFSIZ;
287 tailbuf = xrealloc(tailbuf, tailbufsize);
Matt Kraai55bccf32001-01-05 02:57:53 +0000288 }
Eric Andersend5fa3e32000-08-02 16:42:58 +0000289 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000290 buf = tailbuf + taillen;
Eric Andersend5fa3e32000-08-02 16:42:58 +0000291 }
292 }
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000293
Manuel Novoa III cad53642003-03-19 09:13:01 +0000294 if (!from_top) {
295 tail_xbb_full_write(tailbuf, taillen);
Matt Kraai55bccf32001-01-05 02:57:53 +0000296 }
297
298 taillen = 0;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000299 } while (++i < nfiles);
300
301 buf = xrealloc(tailbuf, BUFSIZ);
302
303 fmt = NULL;
Matt Kraai55bccf32001-01-05 02:57:53 +0000304
305 while (follow) {
306 sleep(sleep_period);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000307 i = 0;
308 do {
309 if (nfiles > header_threshhold) {
310 fmt = header_fmt;
Matt Kraai55bccf32001-01-05 02:57:53 +0000311 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000312 while ((nread = tail_read(fds[i], buf, sizeof(buf))) > 0) {
313 if (fmt) {
314 tail_xprint_header(fmt, argv[i]);
315 fmt = NULL;
316 }
317 tail_xbb_full_write(buf, nread);
Matt Kraai55bccf32001-01-05 02:57:53 +0000318 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000319 } while (++i < nfiles);
Matt Kraai55bccf32001-01-05 02:57:53 +0000320 }
321
322 return status;
323}