blob: 2182b0936d353e7db1233b494773ad36ab6345fe [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
Eric Andersencbe31da2001-02-20 06:14:08 +000027#include "busybox.h"
Eric Andersenabc0f4f1999-12-08 23:19:36 +000028
Matt Kraaia164c642001-02-05 17:50:03 +000029static const struct suffix_mult tail_suffixes[] = {
Matt Kraai24ac0172000-12-18 21:38:57 +000030 { "b", 512 },
31 { "k", 1024 },
32 { "m", 1048576 },
33 { NULL, 0 }
34};
35
Rob Landleyf8fd4db2006-01-30 01:30:39 +000036static int status;
Matt Kraai55bccf32001-01-05 02:57:53 +000037
Manuel Novoa III cad53642003-03-19 09:13:01 +000038static void tail_xbb_full_write(const char *buf, size_t len)
Eric Andersenabc0f4f1999-12-08 23:19:36 +000039{
Manuel Novoa III cad53642003-03-19 09:13:01 +000040 /* If we get a write error, there is really no sense in continuing. */
Rob Landley53437472006-07-16 08:14:35 +000041 if (full_write(STDOUT_FILENO, buf, len) < 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000042 bb_perror_nomsg_and_die();
Eric Andersenabc0f4f1999-12-08 23:19:36 +000043}
44
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000045static void tail_xprint_header(const char *fmt, const char *filename)
46{
47#if defined __GLIBC__
48 if (dprintf(STDOUT_FILENO, fmt, filename) < 0) {
49 bb_perror_nomsg_and_die();
50 }
51#else
52 int hdr_len = strlen(fmt) + strlen(filename);
53 char *hdr = xzalloc(hdr_len);
54 sprintf(hdr, filename, filename);
55 tail_xbb_full_write(hdr, hdr_len);
56#endif
57}
58
Manuel Novoa III cad53642003-03-19 09:13:01 +000059static ssize_t tail_read(int fd, char *buf, size_t count)
60{
61 ssize_t r;
Paul Fox49054342005-07-20 19:46:32 +000062 off_t current,end;
63 struct stat sbuf;
Manuel Novoa III cad53642003-03-19 09:13:01 +000064
Mike Frysingerdbc049f2005-07-26 22:57:51 +000065 end = current = lseek(fd, 0, SEEK_CUR);
66 if (!fstat(fd, &sbuf))
67 end = sbuf.st_size;
Rob Landley58a651b2005-08-13 00:35:00 +000068 lseek(fd, end < current ? 0 : current, SEEK_SET);
Manuel Novoa III cad53642003-03-19 09:13:01 +000069 if ((r = safe_read(fd, buf, count)) < 0) {
Bernhard Reutner-Fischer1b9d7c92006-06-03 22:45:37 +000070 bb_perror_msg(bb_msg_read_error);
Manuel Novoa III cad53642003-03-19 09:13:01 +000071 status = EXIT_FAILURE;
72 }
73
74 return r;
75}
76
77static const char tail_opts[] =
Glenn L McGrath4ef5a842003-10-31 00:35:59 +000078 "fn:c:"
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +000079#if ENABLE_FEATURE_FANCY_TAIL
Glenn L McGrath4ef5a842003-10-31 00:35:59 +000080 "qs:v"
Manuel Novoa III cad53642003-03-19 09:13:01 +000081#endif
82 ;
83
84static const char header_fmt[] = "\n==> %s <==\n";
85
Eric Andersen98bbd682000-07-31 17:05:58 +000086int tail_main(int argc, char **argv)
Eric Andersenabc0f4f1999-12-08 23:19:36 +000087{
Manuel Novoa III cad53642003-03-19 09:13:01 +000088 long count = 10;
Denis Vlasenko459903b2006-11-27 14:44:18 +000089 unsigned sleep_period = 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +000090 int from_top = 0;
91 int follow = 0;
92 int header_threshhold = 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +000093 int count_bytes = 0;
Eric Andersenabc0f4f1999-12-08 23:19:36 +000094
Manuel Novoa III cad53642003-03-19 09:13:01 +000095 char *tailbuf;
96 size_t tailbufsize;
97 int taillen = 0;
98 int newline = 0;
99
100 int *fds, nfiles, nread, nwrite, seen, i, opt;
101 char *s, *buf;
102 const char *fmt;
103
Rob Landleyf8fd4db2006-01-30 01:30:39 +0000104#if !ENABLE_DEBUG_YANK_SUSv2 || ENABLE_FEATURE_FANCY_TAIL
Manuel Novoa III cad53642003-03-19 09:13:01 +0000105 /* Allow legacy syntax of an initial numeric option without -n. */
Denis Vlasenko459903b2006-11-27 14:44:18 +0000106 if (argc >= 2 && (argv[1][0] == '+' || argv[1][0] == '-')
107 && isdigit(argv[1][1])
108 ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000109 optind = 2;
110 optarg = argv[1];
111 goto GET_COUNT;
Robert Griebl13c26fc2002-05-17 22:18:04 +0000112 }
Glenn L McGrath0bd02572005-12-11 03:09:05 +0000113#endif
Robert Griebl13c26fc2002-05-17 22:18:04 +0000114
Manuel Novoa III cad53642003-03-19 09:13:01 +0000115 while ((opt = getopt(argc, argv, tail_opts)) > 0) {
Eric Andersend5fa3e32000-08-02 16:42:58 +0000116 switch (opt) {
Matt Kraai55bccf32001-01-05 02:57:53 +0000117 case 'f':
118 follow = 1;
119 break;
Matt Kraai55bccf32001-01-05 02:57:53 +0000120 case 'c':
Manuel Novoa III cad53642003-03-19 09:13:01 +0000121 count_bytes = 1;
Matt Kraai55bccf32001-01-05 02:57:53 +0000122 /* FALLS THROUGH */
Matt Kraai55bccf32001-01-05 02:57:53 +0000123 case 'n':
Rob Landleyf8fd4db2006-01-30 01:30:39 +0000124#if !ENABLE_DEBUG_YANK_SUSv2 || ENABLE_FEATURE_FANCY_TAIL
Denis Vlasenko69107412006-12-21 00:43:06 +0000125 GET_COUNT:
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +0000126#endif
Denis Vlasenko69107412006-12-21 00:43:06 +0000127 from_top = 0;
128 if (*optarg == '+') {
129 ++optarg;
130 from_top = 1;
131 }
Denis Vlasenko13858992006-10-08 12:49:22 +0000132 count = xatol_sfx(optarg, tail_suffixes);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000133 /* Note: Leading whitespace is an error trapped above. */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000134 if (count < 0) {
135 count = -count;
136 }
Eric Andersend5fa3e32000-08-02 16:42:58 +0000137 break;
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +0000138#if ENABLE_FEATURE_FANCY_TAIL
Matt Kraai55bccf32001-01-05 02:57:53 +0000139 case 'q':
Manuel Novoa III cad53642003-03-19 09:13:01 +0000140 header_threshhold = INT_MAX;
Eric Andersend5fa3e32000-08-02 16:42:58 +0000141 break;
Matt Kraai55bccf32001-01-05 02:57:53 +0000142 case 's':
Denis Vlasenko13858992006-10-08 12:49:22 +0000143 sleep_period = xatou(optarg);
Matt Kraai55bccf32001-01-05 02:57:53 +0000144 break;
145 case 'v':
Manuel Novoa III cad53642003-03-19 09:13:01 +0000146 header_threshhold = 0;
Matt Kraai55bccf32001-01-05 02:57:53 +0000147 break;
148#endif
149 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000150 bb_show_usage();
Eric Andersend5fa3e32000-08-02 16:42:58 +0000151 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000152 }
Denis Vlasenko69107412006-12-21 00:43:06 +0000153 argc -= optind;
154 argv += optind;
Matt Kraai55bccf32001-01-05 02:57:53 +0000155
156 /* open all the files */
Denis Vlasenko69107412006-12-21 00:43:06 +0000157 fds = xmalloc(sizeof(int) * (argc + 1));
Manuel Novoa III cad53642003-03-19 09:13:01 +0000158 nfiles = i = 0;
Denis Vlasenko69107412006-12-21 00:43:06 +0000159 if (argc == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000160 struct stat statbuf;
161
162 if (!fstat(STDIN_FILENO, &statbuf) && S_ISFIFO(statbuf.st_mode)) {
163 follow = 0;
Matt Kraai55bccf32001-01-05 02:57:53 +0000164 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000165 *argv = (char *) bb_msg_standard_input;
166 goto DO_STDIN;
Matt Kraai55bccf32001-01-05 02:57:53 +0000167 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000168
169 do {
Denis Vlasenko9f739442006-12-16 23:49:13 +0000170 if (LONE_DASH(argv[i])) {
171 DO_STDIN:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000172 fds[nfiles] = STDIN_FILENO;
173 } else if ((fds[nfiles] = open(argv[i], O_RDONLY)) < 0) {
174 bb_perror_msg("%s", argv[i]);
175 status = EXIT_FAILURE;
176 continue;
177 }
178 argv[nfiles] = argv[i];
179 ++nfiles;
180 } while (++i < argc);
181
182 if (!nfiles) {
183 bb_error_msg_and_die("no files");
184 }
185
186 tailbufsize = BUFSIZ;
Glenn L McGrath4ef5a842003-10-31 00:35:59 +0000187
Matt Kraai55bccf32001-01-05 02:57:53 +0000188 /* tail the files */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000189 if (from_top < count_bytes) { /* Each is 0 or 1, so true iff 0 < 1. */
190 /* Hence, !from_top && count_bytes */
191 if (tailbufsize < count) {
192 tailbufsize = count + BUFSIZ;
193 }
194 }
Glenn L McGrath4ef5a842003-10-31 00:35:59 +0000195
Manuel Novoa III cad53642003-03-19 09:13:01 +0000196 buf = tailbuf = xmalloc(tailbufsize);
Matt Kraai55bccf32001-01-05 02:57:53 +0000197
Manuel Novoa III cad53642003-03-19 09:13:01 +0000198 fmt = header_fmt + 1; /* Skip header leading newline on first output. */
199 i = 0;
200 do {
201 /* Be careful. It would be possible to optimize the count-bytes
202 * case if the file is seekable. If you do though, remember that
203 * starting file position may not be the beginning of the file.
204 * Beware of backing up too far. See example in wc.c.
205 */
206 if ((!(count|from_top)) && (lseek(fds[i], 0, SEEK_END) >= 0)) {
Eric Andersence98c192001-06-26 15:07:08 +0000207 continue;
208 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000209
210 if (nfiles > header_threshhold) {
211 tail_xprint_header(fmt, argv[i]);
212 fmt = header_fmt;
213 }
214
215 buf = tailbuf;
216 taillen = 0;
217 seen = 1;
Glenn L McGrathf86391e2004-09-30 00:24:21 +0000218 newline = 0;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000219
220 while ((nread = tail_read(fds[i], buf, tailbufsize-taillen)) > 0) {
Matt Kraai55bccf32001-01-05 02:57:53 +0000221 if (from_top) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000222 nwrite = nread;
223 if (seen < count) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000224 if (count_bytes) {
225 nwrite -= (count - seen);
226 seen = count;
Glenn L McGrath4ef5a842003-10-31 00:35:59 +0000227 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000228 s = buf;
229 do {
230 --nwrite;
231 if ((*s++ == '\n') && (++seen == count)) {
Matt Kraai55bccf32001-01-05 02:57:53 +0000232 break;
233 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000234 } while (nwrite);
235 }
236 }
237 tail_xbb_full_write(buf + nread - nwrite, nwrite);
238 } else if (count) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000239 if (count_bytes) {
240 taillen += nread;
241 if (taillen > count) {
242 memmove(tailbuf, tailbuf + taillen - count, count);
243 taillen = count;
244 }
Glenn L McGrath4ef5a842003-10-31 00:35:59 +0000245 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000246 int k = nread;
247 int nbuf = 0;
248
249 while (k) {
250 --k;
251 if (buf[k] == '\n') {
252 ++nbuf;
Matt Kraai55bccf32001-01-05 02:57:53 +0000253 }
254 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000255
256 if (newline + nbuf < count) {
257 newline += nbuf;
258 taillen += nread;
259
Matt Kraai55bccf32001-01-05 02:57:53 +0000260 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000261 int extra = 0;
262 if (buf[nread-1] != '\n') {
263 extra = 1;
264 }
265
266 k = newline + nbuf + extra - count;
267 s = tailbuf;
268 while (k) {
269 if (*s == '\n') {
270 --k;
271 }
272 ++s;
273 }
274
275 taillen += nread - (s - tailbuf);
276 memmove(tailbuf, s, taillen);
277 newline = count - extra;
Matt Kraai55bccf32001-01-05 02:57:53 +0000278 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000279 if (tailbufsize < taillen + BUFSIZ) {
280 tailbufsize = taillen + BUFSIZ;
281 tailbuf = xrealloc(tailbuf, tailbufsize);
Matt Kraai55bccf32001-01-05 02:57:53 +0000282 }
Eric Andersend5fa3e32000-08-02 16:42:58 +0000283 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000284 buf = tailbuf + taillen;
Eric Andersend5fa3e32000-08-02 16:42:58 +0000285 }
286 }
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000287
Manuel Novoa III cad53642003-03-19 09:13:01 +0000288 if (!from_top) {
289 tail_xbb_full_write(tailbuf, taillen);
Matt Kraai55bccf32001-01-05 02:57:53 +0000290 }
291
292 taillen = 0;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000293 } while (++i < nfiles);
294
295 buf = xrealloc(tailbuf, BUFSIZ);
296
297 fmt = NULL;
Matt Kraai55bccf32001-01-05 02:57:53 +0000298
299 while (follow) {
300 sleep(sleep_period);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000301 i = 0;
302 do {
303 if (nfiles > header_threshhold) {
304 fmt = header_fmt;
Matt Kraai55bccf32001-01-05 02:57:53 +0000305 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000306 while ((nread = tail_read(fds[i], buf, sizeof(buf))) > 0) {
307 if (fmt) {
308 tail_xprint_header(fmt, argv[i]);
309 fmt = NULL;
310 }
311 tail_xbb_full_write(buf, nread);
Matt Kraai55bccf32001-01-05 02:57:53 +0000312 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000313 } while (++i < nfiles);
Matt Kraai55bccf32001-01-05 02:57:53 +0000314 }
315
316 return status;
317}