blob: 49c00f3c9483e220f7f7b5c83cf076b6fe3362e4 [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 *
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 Andersen3fe39dc2000-01-25 18:13:53 +000021 */
Matt Kraai55bccf32001-01-05 02:57:53 +000022
Manuel Novoa III cad53642003-03-19 09:13:01 +000023/* 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 Andersenabc0f4f1999-12-08 23:19:36 +000026
Manuel Novoa III cad53642003-03-19 09:13:01 +000027/* 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 Andersened3ef502001-01-27 08:24:39 +000042#include <string.h>
Robert Griebl53146cc2002-05-27 22:24:53 +000043#include <ctype.h>
Eric Andersened3ef502001-01-27 08:24:39 +000044#include <unistd.h>
Manuel Novoa III cad53642003-03-19 09:13:01 +000045#include <fcntl.h>
46#include <sys/stat.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000047#include "busybox.h"
Eric Andersenabc0f4f1999-12-08 23:19:36 +000048
Matt Kraaia164c642001-02-05 17:50:03 +000049static const struct suffix_mult tail_suffixes[] = {
Matt Kraai24ac0172000-12-18 21:38:57 +000050 { "b", 512 },
51 { "k", 1024 },
52 { "m", 1048576 },
53 { NULL, 0 }
54};
55
Manuel Novoa III cad53642003-03-19 09:13:01 +000056static int status
57#if EXIT_SUCCESS != 0
58 = EXIT_SUCCESS /* If it is 0 (paranoid check), let bss initialize it. */
59#endif
60 ;
Matt Kraai55bccf32001-01-05 02:57:53 +000061
Manuel Novoa III cad53642003-03-19 09:13:01 +000062static void tail_xprint_header(const char *fmt, const char *filename)
Matt Kraai55bccf32001-01-05 02:57:53 +000063{
Manuel Novoa III cad53642003-03-19 09:13:01 +000064 /* If we get an output error, there is really no sense in continuing. */
Rob Landleyb9dfb8c2005-05-07 17:45:38 +000065 if (dprintf(STDOUT_FILENO, fmt, filename) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000066 bb_perror_nomsg_and_die();
67 }
Eric Andersenabc0f4f1999-12-08 23:19:36 +000068}
69
Manuel Novoa III cad53642003-03-19 09:13:01 +000070/* len should probably be size_t */
71static void tail_xbb_full_write(const char *buf, size_t len)
Eric Andersenabc0f4f1999-12-08 23:19:36 +000072{
Manuel Novoa III cad53642003-03-19 09:13:01 +000073 /* 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 Andersenabc0f4f1999-12-08 23:19:36 +000077}
78
Manuel Novoa III cad53642003-03-19 09:13:01 +000079static ssize_t tail_read(int fd, char *buf, size_t count)
80{
81 ssize_t r;
Paul Fox49054342005-07-20 19:46:32 +000082 off_t current,end;
83 struct stat sbuf;
Manuel Novoa III cad53642003-03-19 09:13:01 +000084
Mike Frysingerdbc049f2005-07-26 22:57:51 +000085 end = current = lseek(fd, 0, SEEK_CUR);
86 if (!fstat(fd, &sbuf))
87 end = sbuf.st_size;
Rob Landley58a651b2005-08-13 00:35:00 +000088 lseek(fd, end < current ? 0 : current, SEEK_SET);
Manuel Novoa III cad53642003-03-19 09:13:01 +000089 if ((r = safe_read(fd, buf, count)) < 0) {
90 bb_perror_msg("read");
91 status = EXIT_FAILURE;
92 }
93
94 return r;
95}
96
97static const char tail_opts[] =
Glenn L McGrath4ef5a842003-10-31 00:35:59 +000098 "fn:c:"
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +000099#if ENABLE_FEATURE_FANCY_TAIL
Glenn L McGrath4ef5a842003-10-31 00:35:59 +0000100 "qs:v"
Manuel Novoa III cad53642003-03-19 09:13:01 +0000101#endif
102 ;
103
104static const char header_fmt[] = "\n==> %s <==\n";
105
Eric Andersen98bbd682000-07-31 17:05:58 +0000106int tail_main(int argc, char **argv)
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000107{
Manuel Novoa III cad53642003-03-19 09:13:01 +0000108 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 cad53642003-03-19 09:13:01 +0000113 int count_bytes = 0;
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000114
Manuel Novoa III cad53642003-03-19 09:13:01 +0000115 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
Bernhard Reutner-Fischer9a14bd02005-12-15 11:47:16 +0000124#if defined CONFIG_FEATURE_SUSv2 || ENABLE_FEATURE_FANCY_TAIL
Manuel Novoa III cad53642003-03-19 09:13:01 +0000125 /* Allow legacy syntax of an initial numeric option without -n. */
Eric Andersenb5b5ac32003-03-28 16:54:14 +0000126 if (argc >=2 && ((argv[1][0] == '+') || ((argv[1][0] == '-')
Manuel Novoa III cad53642003-03-19 09:13:01 +0000127 /* && (isdigit)(argv[1][1]) */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000128 && (((unsigned int)(argv[1][1] - '0')) <= 9))))
Eric Andersenb5b5ac32003-03-28 16:54:14 +0000129 {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000130 optind = 2;
131 optarg = argv[1];
132 goto GET_COUNT;
Robert Griebl13c26fc2002-05-17 22:18:04 +0000133 }
Glenn L McGrath0bd02572005-12-11 03:09:05 +0000134#endif
Robert Griebl13c26fc2002-05-17 22:18:04 +0000135
Manuel Novoa III cad53642003-03-19 09:13:01 +0000136 while ((opt = getopt(argc, argv, tail_opts)) > 0) {
Eric Andersend5fa3e32000-08-02 16:42:58 +0000137 switch (opt) {
Matt Kraai55bccf32001-01-05 02:57:53 +0000138 case 'f':
139 follow = 1;
140 break;
Matt Kraai55bccf32001-01-05 02:57:53 +0000141 case 'c':
Manuel Novoa III cad53642003-03-19 09:13:01 +0000142 count_bytes = 1;
Matt Kraai55bccf32001-01-05 02:57:53 +0000143 /* FALLS THROUGH */
Matt Kraai55bccf32001-01-05 02:57:53 +0000144 case 'n':
Bernhard Reutner-Fischer9a14bd02005-12-15 11:47:16 +0000145#if defined CONFIG_FEATURE_SUSv2 || ENABLE_FEATURE_FANCY_TAIL
Manuel Novoa III cad53642003-03-19 09:13:01 +0000146 GET_COUNT:
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +0000147#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +0000148 count = bb_xgetlarg10_sfx(optarg, tail_suffixes);
149 /* Note: Leading whitespace is an error trapped above. */
150 if (*optarg == '+') {
Matt Kraai55bccf32001-01-05 02:57:53 +0000151 from_top = 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000152 } else {
153 from_top = 0;
154 }
155 if (count < 0) {
156 count = -count;
157 }
Eric Andersend5fa3e32000-08-02 16:42:58 +0000158 break;
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +0000159#if ENABLE_FEATURE_FANCY_TAIL
Matt Kraai55bccf32001-01-05 02:57:53 +0000160 case 'q':
Manuel Novoa III cad53642003-03-19 09:13:01 +0000161 header_threshhold = INT_MAX;
Eric Andersend5fa3e32000-08-02 16:42:58 +0000162 break;
Matt Kraai55bccf32001-01-05 02:57:53 +0000163 case 's':
Manuel Novoa III cad53642003-03-19 09:13:01 +0000164 sleep_period =bb_xgetularg10_bnd(optarg, 0, UINT_MAX);
Matt Kraai55bccf32001-01-05 02:57:53 +0000165 break;
166 case 'v':
Manuel Novoa III cad53642003-03-19 09:13:01 +0000167 header_threshhold = 0;
Matt Kraai55bccf32001-01-05 02:57:53 +0000168 break;
169#endif
170 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000171 bb_show_usage();
Eric Andersend5fa3e32000-08-02 16:42:58 +0000172 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000173 }
Matt Kraai55bccf32001-01-05 02:57:53 +0000174
175 /* open all the files */
176 fds = (int *)xmalloc(sizeof(int) * (argc - optind + 1));
Manuel Novoa III cad53642003-03-19 09:13:01 +0000177
178 argv += optind;
179 nfiles = i = 0;
180
181 if ((argc -= optind) == 0) {
182 struct stat statbuf;
183
184 if (!fstat(STDIN_FILENO, &statbuf) && S_ISFIFO(statbuf.st_mode)) {
185 follow = 0;
Matt Kraai55bccf32001-01-05 02:57:53 +0000186 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000187 /* --argv; */
188 *argv = (char *) bb_msg_standard_input;
189 goto DO_STDIN;
Matt Kraai55bccf32001-01-05 02:57:53 +0000190 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000191
192 do {
193 if ((argv[i][0] == '-') && !argv[i][1]) {
194 DO_STDIN:
195 fds[nfiles] = STDIN_FILENO;
196 } else if ((fds[nfiles] = open(argv[i], O_RDONLY)) < 0) {
197 bb_perror_msg("%s", argv[i]);
198 status = EXIT_FAILURE;
199 continue;
200 }
201 argv[nfiles] = argv[i];
202 ++nfiles;
203 } while (++i < argc);
204
205 if (!nfiles) {
206 bb_error_msg_and_die("no files");
207 }
208
209 tailbufsize = BUFSIZ;
Glenn L McGrath4ef5a842003-10-31 00:35:59 +0000210
Matt Kraai55bccf32001-01-05 02:57:53 +0000211 /* tail the files */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000212 if (from_top < count_bytes) { /* Each is 0 or 1, so true iff 0 < 1. */
213 /* Hence, !from_top && count_bytes */
214 if (tailbufsize < count) {
215 tailbufsize = count + BUFSIZ;
216 }
217 }
Glenn L McGrath4ef5a842003-10-31 00:35:59 +0000218
Manuel Novoa III cad53642003-03-19 09:13:01 +0000219 buf = tailbuf = xmalloc(tailbufsize);
Matt Kraai55bccf32001-01-05 02:57:53 +0000220
Manuel Novoa III cad53642003-03-19 09:13:01 +0000221 fmt = header_fmt + 1; /* Skip header leading newline on first output. */
222 i = 0;
223 do {
224 /* Be careful. It would be possible to optimize the count-bytes
225 * case if the file is seekable. If you do though, remember that
226 * starting file position may not be the beginning of the file.
227 * Beware of backing up too far. See example in wc.c.
228 */
229 if ((!(count|from_top)) && (lseek(fds[i], 0, SEEK_END) >= 0)) {
Eric Andersence98c192001-06-26 15:07:08 +0000230 continue;
231 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000232
233 if (nfiles > header_threshhold) {
234 tail_xprint_header(fmt, argv[i]);
235 fmt = header_fmt;
236 }
237
238 buf = tailbuf;
239 taillen = 0;
240 seen = 1;
Glenn L McGrathf86391e2004-09-30 00:24:21 +0000241 newline = 0;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000242
243 while ((nread = tail_read(fds[i], buf, tailbufsize-taillen)) > 0) {
Matt Kraai55bccf32001-01-05 02:57:53 +0000244 if (from_top) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000245 nwrite = nread;
246 if (seen < count) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000247 if (count_bytes) {
248 nwrite -= (count - seen);
249 seen = count;
Glenn L McGrath4ef5a842003-10-31 00:35:59 +0000250 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000251 s = buf;
252 do {
253 --nwrite;
254 if ((*s++ == '\n') && (++seen == count)) {
Matt Kraai55bccf32001-01-05 02:57:53 +0000255 break;
256 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000257 } while (nwrite);
258 }
259 }
260 tail_xbb_full_write(buf + nread - nwrite, nwrite);
261 } else if (count) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000262 if (count_bytes) {
263 taillen += nread;
264 if (taillen > count) {
265 memmove(tailbuf, tailbuf + taillen - count, count);
266 taillen = count;
267 }
Glenn L McGrath4ef5a842003-10-31 00:35:59 +0000268 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000269 int k = nread;
270 int nbuf = 0;
271
272 while (k) {
273 --k;
274 if (buf[k] == '\n') {
275 ++nbuf;
Matt Kraai55bccf32001-01-05 02:57:53 +0000276 }
277 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000278
279 if (newline + nbuf < count) {
280 newline += nbuf;
281 taillen += nread;
282
Matt Kraai55bccf32001-01-05 02:57:53 +0000283 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000284 int extra = 0;
285 if (buf[nread-1] != '\n') {
286 extra = 1;
287 }
288
289 k = newline + nbuf + extra - count;
290 s = tailbuf;
291 while (k) {
292 if (*s == '\n') {
293 --k;
294 }
295 ++s;
296 }
297
298 taillen += nread - (s - tailbuf);
299 memmove(tailbuf, s, taillen);
300 newline = count - extra;
Matt Kraai55bccf32001-01-05 02:57:53 +0000301 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000302 if (tailbufsize < taillen + BUFSIZ) {
303 tailbufsize = taillen + BUFSIZ;
304 tailbuf = xrealloc(tailbuf, tailbufsize);
Matt Kraai55bccf32001-01-05 02:57:53 +0000305 }
Eric Andersend5fa3e32000-08-02 16:42:58 +0000306 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000307 buf = tailbuf + taillen;
Eric Andersend5fa3e32000-08-02 16:42:58 +0000308 }
309 }
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000310
Manuel Novoa III cad53642003-03-19 09:13:01 +0000311 if (!from_top) {
312 tail_xbb_full_write(tailbuf, taillen);
Matt Kraai55bccf32001-01-05 02:57:53 +0000313 }
314
315 taillen = 0;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000316 } while (++i < nfiles);
317
318 buf = xrealloc(tailbuf, BUFSIZ);
319
320 fmt = NULL;
Matt Kraai55bccf32001-01-05 02:57:53 +0000321
322 while (follow) {
323 sleep(sleep_period);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000324 i = 0;
325 do {
326 if (nfiles > header_threshhold) {
327 fmt = header_fmt;
Matt Kraai55bccf32001-01-05 02:57:53 +0000328 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000329 while ((nread = tail_read(fds[i], buf, sizeof(buf))) > 0) {
330 if (fmt) {
331 tail_xprint_header(fmt, argv[i]);
332 fmt = NULL;
333 }
334 tail_xbb_full_write(buf, nread);
Matt Kraai55bccf32001-01-05 02:57:53 +0000335 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000336 } while (++i < nfiles);
Matt Kraai55bccf32001-01-05 02:57:53 +0000337 }
338
339 return status;
340}