blob: 49f1bcd6afe5ef845fa96366cd8ffa7c1486b8f9 [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_xbb_full_write(const char *buf, size_t len)
Eric Andersenabc0f4f1999-12-08 23:19:36 +000046{
Manuel Novoa III cad53642003-03-19 09:13:01 +000047 /* If we get a write error, there is really no sense in continuing. */
Rob Landley53437472006-07-16 08:14:35 +000048 if (full_write(STDOUT_FILENO, buf, len) < 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000049 bb_perror_nomsg_and_die();
Eric Andersenabc0f4f1999-12-08 23:19:36 +000050}
51
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000052static 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 cad53642003-03-19 09:13:01 +000066static ssize_t tail_read(int fd, char *buf, size_t count)
67{
68 ssize_t r;
Paul Fox49054342005-07-20 19:46:32 +000069 off_t current,end;
70 struct stat sbuf;
Manuel Novoa III cad53642003-03-19 09:13:01 +000071
Mike Frysingerdbc049f2005-07-26 22:57:51 +000072 end = current = lseek(fd, 0, SEEK_CUR);
73 if (!fstat(fd, &sbuf))
74 end = sbuf.st_size;
Rob Landley58a651b2005-08-13 00:35:00 +000075 lseek(fd, end < current ? 0 : current, SEEK_SET);
Manuel Novoa III cad53642003-03-19 09:13:01 +000076 if ((r = safe_read(fd, buf, count)) < 0) {
Bernhard Reutner-Fischer1b9d7c92006-06-03 22:45:37 +000077 bb_perror_msg(bb_msg_read_error);
Manuel Novoa III cad53642003-03-19 09:13:01 +000078 status = EXIT_FAILURE;
79 }
80
81 return r;
82}
83
84static const char tail_opts[] =
Glenn L McGrath4ef5a842003-10-31 00:35:59 +000085 "fn:c:"
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +000086#if ENABLE_FEATURE_FANCY_TAIL
Glenn L McGrath4ef5a842003-10-31 00:35:59 +000087 "qs:v"
Manuel Novoa III cad53642003-03-19 09:13:01 +000088#endif
89 ;
90
91static const char header_fmt[] = "\n==> %s <==\n";
92
Eric Andersen98bbd682000-07-31 17:05:58 +000093int tail_main(int argc, char **argv)
Eric Andersenabc0f4f1999-12-08 23:19:36 +000094{
Manuel Novoa III cad53642003-03-19 09:13:01 +000095 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 cad53642003-03-19 09:13:01 +0000100 int count_bytes = 0;
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000101
Manuel Novoa III cad53642003-03-19 09:13:01 +0000102 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 Landleyf8fd4db2006-01-30 01:30:39 +0000111#if !ENABLE_DEBUG_YANK_SUSv2 || ENABLE_FEATURE_FANCY_TAIL
Manuel Novoa III cad53642003-03-19 09:13:01 +0000112 /* Allow legacy syntax of an initial numeric option without -n. */
Eric Andersenb5b5ac32003-03-28 16:54:14 +0000113 if (argc >=2 && ((argv[1][0] == '+') || ((argv[1][0] == '-')
Manuel Novoa III cad53642003-03-19 09:13:01 +0000114 /* && (isdigit)(argv[1][1]) */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000115 && (((unsigned int)(argv[1][1] - '0')) <= 9))))
Eric Andersenb5b5ac32003-03-28 16:54:14 +0000116 {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000117 optind = 2;
118 optarg = argv[1];
119 goto GET_COUNT;
Robert Griebl13c26fc2002-05-17 22:18:04 +0000120 }
Glenn L McGrath0bd02572005-12-11 03:09:05 +0000121#endif
Robert Griebl13c26fc2002-05-17 22:18:04 +0000122
Manuel Novoa III cad53642003-03-19 09:13:01 +0000123 while ((opt = getopt(argc, argv, tail_opts)) > 0) {
Eric Andersend5fa3e32000-08-02 16:42:58 +0000124 switch (opt) {
Matt Kraai55bccf32001-01-05 02:57:53 +0000125 case 'f':
126 follow = 1;
127 break;
Matt Kraai55bccf32001-01-05 02:57:53 +0000128 case 'c':
Manuel Novoa III cad53642003-03-19 09:13:01 +0000129 count_bytes = 1;
Matt Kraai55bccf32001-01-05 02:57:53 +0000130 /* FALLS THROUGH */
Matt Kraai55bccf32001-01-05 02:57:53 +0000131 case 'n':
Rob Landleyf8fd4db2006-01-30 01:30:39 +0000132#if !ENABLE_DEBUG_YANK_SUSv2 || ENABLE_FEATURE_FANCY_TAIL
Manuel Novoa III cad53642003-03-19 09:13:01 +0000133 GET_COUNT:
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +0000134#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +0000135 count = bb_xgetlarg10_sfx(optarg, tail_suffixes);
136 /* Note: Leading whitespace is an error trapped above. */
137 if (*optarg == '+') {
Matt Kraai55bccf32001-01-05 02:57:53 +0000138 from_top = 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000139 } else {
140 from_top = 0;
141 }
142 if (count < 0) {
143 count = -count;
144 }
Eric Andersend5fa3e32000-08-02 16:42:58 +0000145 break;
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +0000146#if ENABLE_FEATURE_FANCY_TAIL
Matt Kraai55bccf32001-01-05 02:57:53 +0000147 case 'q':
Manuel Novoa III cad53642003-03-19 09:13:01 +0000148 header_threshhold = INT_MAX;
Eric Andersend5fa3e32000-08-02 16:42:58 +0000149 break;
Matt Kraai55bccf32001-01-05 02:57:53 +0000150 case 's':
Manuel Novoa III cad53642003-03-19 09:13:01 +0000151 sleep_period =bb_xgetularg10_bnd(optarg, 0, UINT_MAX);
Matt Kraai55bccf32001-01-05 02:57:53 +0000152 break;
153 case 'v':
Manuel Novoa III cad53642003-03-19 09:13:01 +0000154 header_threshhold = 0;
Matt Kraai55bccf32001-01-05 02:57:53 +0000155 break;
156#endif
157 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000158 bb_show_usage();
Eric Andersend5fa3e32000-08-02 16:42:58 +0000159 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000160 }
Matt Kraai55bccf32001-01-05 02:57:53 +0000161
162 /* open all the files */
163 fds = (int *)xmalloc(sizeof(int) * (argc - optind + 1));
Manuel Novoa III cad53642003-03-19 09:13:01 +0000164
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 Kraai55bccf32001-01-05 02:57:53 +0000173 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000174 /* --argv; */
175 *argv = (char *) bb_msg_standard_input;
176 goto DO_STDIN;
Matt Kraai55bccf32001-01-05 02:57:53 +0000177 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000178
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 McGrath4ef5a842003-10-31 00:35:59 +0000197
Matt Kraai55bccf32001-01-05 02:57:53 +0000198 /* tail the files */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000199 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 McGrath4ef5a842003-10-31 00:35:59 +0000205
Manuel Novoa III cad53642003-03-19 09:13:01 +0000206 buf = tailbuf = xmalloc(tailbufsize);
Matt Kraai55bccf32001-01-05 02:57:53 +0000207
Manuel Novoa III cad53642003-03-19 09:13:01 +0000208 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 Andersence98c192001-06-26 15:07:08 +0000217 continue;
218 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000219
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 McGrathf86391e2004-09-30 00:24:21 +0000228 newline = 0;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000229
230 while ((nread = tail_read(fds[i], buf, tailbufsize-taillen)) > 0) {
Matt Kraai55bccf32001-01-05 02:57:53 +0000231 if (from_top) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000232 nwrite = nread;
233 if (seen < count) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000234 if (count_bytes) {
235 nwrite -= (count - seen);
236 seen = count;
Glenn L McGrath4ef5a842003-10-31 00:35:59 +0000237 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000238 s = buf;
239 do {
240 --nwrite;
241 if ((*s++ == '\n') && (++seen == count)) {
Matt Kraai55bccf32001-01-05 02:57:53 +0000242 break;
243 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000244 } while (nwrite);
245 }
246 }
247 tail_xbb_full_write(buf + nread - nwrite, nwrite);
248 } else if (count) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000249 if (count_bytes) {
250 taillen += nread;
251 if (taillen > count) {
252 memmove(tailbuf, tailbuf + taillen - count, count);
253 taillen = count;
254 }
Glenn L McGrath4ef5a842003-10-31 00:35:59 +0000255 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000256 int k = nread;
257 int nbuf = 0;
258
259 while (k) {
260 --k;
261 if (buf[k] == '\n') {
262 ++nbuf;
Matt Kraai55bccf32001-01-05 02:57:53 +0000263 }
264 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000265
266 if (newline + nbuf < count) {
267 newline += nbuf;
268 taillen += nread;
269
Matt Kraai55bccf32001-01-05 02:57:53 +0000270 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000271 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 Kraai55bccf32001-01-05 02:57:53 +0000288 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000289 if (tailbufsize < taillen + BUFSIZ) {
290 tailbufsize = taillen + BUFSIZ;
291 tailbuf = xrealloc(tailbuf, tailbufsize);
Matt Kraai55bccf32001-01-05 02:57:53 +0000292 }
Eric Andersend5fa3e32000-08-02 16:42:58 +0000293 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000294 buf = tailbuf + taillen;
Eric Andersend5fa3e32000-08-02 16:42:58 +0000295 }
296 }
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000297
Manuel Novoa III cad53642003-03-19 09:13:01 +0000298 if (!from_top) {
299 tail_xbb_full_write(tailbuf, taillen);
Matt Kraai55bccf32001-01-05 02:57:53 +0000300 }
301
302 taillen = 0;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000303 } while (++i < nfiles);
304
305 buf = xrealloc(tailbuf, BUFSIZ);
306
307 fmt = NULL;
Matt Kraai55bccf32001-01-05 02:57:53 +0000308
309 while (follow) {
310 sleep(sleep_period);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000311 i = 0;
312 do {
313 if (nfiles > header_threshhold) {
314 fmt = header_fmt;
Matt Kraai55bccf32001-01-05 02:57:53 +0000315 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000316 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 Kraai55bccf32001-01-05 02:57:53 +0000322 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000323 } while (++i < nfiles);
Matt Kraai55bccf32001-01-05 02:57:53 +0000324 }
325
326 return status;
327}