blob: ef00383853831a7f183a5ba506ae73d1b4e05bc7 [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 Landleycce1ae22005-05-07 07:53:16 +000065 if (printf(fmt, filename) < 0 ||
66 fflush(stdout) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000067 bb_perror_nomsg_and_die();
68 }
Eric Andersenabc0f4f1999-12-08 23:19:36 +000069}
70
Manuel Novoa III cad53642003-03-19 09:13:01 +000071/* len should probably be size_t */
72static void tail_xbb_full_write(const char *buf, size_t len)
Eric Andersenabc0f4f1999-12-08 23:19:36 +000073{
Manuel Novoa III cad53642003-03-19 09:13:01 +000074 /* If we get a write error, there is really no sense in continuing. */
75 if (bb_full_write(STDOUT_FILENO, buf, len) < 0) {
76 bb_perror_nomsg_and_die();
77 }
Eric Andersenabc0f4f1999-12-08 23:19:36 +000078}
79
Manuel Novoa III cad53642003-03-19 09:13:01 +000080static ssize_t tail_read(int fd, char *buf, size_t count)
81{
82 ssize_t r;
83
84 if ((r = safe_read(fd, buf, count)) < 0) {
85 bb_perror_msg("read");
86 status = EXIT_FAILURE;
87 }
88
89 return r;
90}
91
92static const char tail_opts[] =
Glenn L McGrath4ef5a842003-10-31 00:35:59 +000093 "fn:c:"
Manuel Novoa III cad53642003-03-19 09:13:01 +000094#ifdef CONFIG_FEATURE_FANCY_TAIL
Glenn L McGrath4ef5a842003-10-31 00:35:59 +000095 "qs:v"
Manuel Novoa III cad53642003-03-19 09:13:01 +000096#endif
97 ;
98
99static const char header_fmt[] = "\n==> %s <==\n";
100
Eric Andersen98bbd682000-07-31 17:05:58 +0000101int tail_main(int argc, char **argv)
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000102{
Manuel Novoa III cad53642003-03-19 09:13:01 +0000103 long count = 10;
104 unsigned int sleep_period = 1;
105 int from_top = 0;
106 int follow = 0;
107 int header_threshhold = 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000108 int count_bytes = 0;
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000109
Manuel Novoa III cad53642003-03-19 09:13:01 +0000110 char *tailbuf;
111 size_t tailbufsize;
112 int taillen = 0;
113 int newline = 0;
114
115 int *fds, nfiles, nread, nwrite, seen, i, opt;
116 char *s, *buf;
117 const char *fmt;
118
119 /* Allow legacy syntax of an initial numeric option without -n. */
Eric Andersenb5b5ac32003-03-28 16:54:14 +0000120 if (argc >=2 && ((argv[1][0] == '+') || ((argv[1][0] == '-')
Manuel Novoa III cad53642003-03-19 09:13:01 +0000121 /* && (isdigit)(argv[1][1]) */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000122 && (((unsigned int)(argv[1][1] - '0')) <= 9))))
Eric Andersenb5b5ac32003-03-28 16:54:14 +0000123 {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000124 optind = 2;
125 optarg = argv[1];
126 goto GET_COUNT;
Robert Griebl13c26fc2002-05-17 22:18:04 +0000127 }
128
Manuel Novoa III cad53642003-03-19 09:13:01 +0000129 while ((opt = getopt(argc, argv, tail_opts)) > 0) {
Eric Andersend5fa3e32000-08-02 16:42:58 +0000130 switch (opt) {
Matt Kraai55bccf32001-01-05 02:57:53 +0000131 case 'f':
132 follow = 1;
133 break;
Matt Kraai55bccf32001-01-05 02:57:53 +0000134 case 'c':
Manuel Novoa III cad53642003-03-19 09:13:01 +0000135 count_bytes = 1;
Matt Kraai55bccf32001-01-05 02:57:53 +0000136 /* FALLS THROUGH */
Matt Kraai55bccf32001-01-05 02:57:53 +0000137 case 'n':
Manuel Novoa III cad53642003-03-19 09:13:01 +0000138 GET_COUNT:
139 count = bb_xgetlarg10_sfx(optarg, tail_suffixes);
140 /* Note: Leading whitespace is an error trapped above. */
141 if (*optarg == '+') {
Matt Kraai55bccf32001-01-05 02:57:53 +0000142 from_top = 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000143 } else {
144 from_top = 0;
145 }
146 if (count < 0) {
147 count = -count;
148 }
Eric Andersend5fa3e32000-08-02 16:42:58 +0000149 break;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000150#ifdef CONFIG_FEATURE_FANCY_TAIL
Matt Kraai55bccf32001-01-05 02:57:53 +0000151 case 'q':
Manuel Novoa III cad53642003-03-19 09:13:01 +0000152 header_threshhold = INT_MAX;
Eric Andersend5fa3e32000-08-02 16:42:58 +0000153 break;
Matt Kraai55bccf32001-01-05 02:57:53 +0000154 case 's':
Manuel Novoa III cad53642003-03-19 09:13:01 +0000155 sleep_period =bb_xgetularg10_bnd(optarg, 0, UINT_MAX);
Matt Kraai55bccf32001-01-05 02:57:53 +0000156 break;
157 case 'v':
Manuel Novoa III cad53642003-03-19 09:13:01 +0000158 header_threshhold = 0;
Matt Kraai55bccf32001-01-05 02:57:53 +0000159 break;
160#endif
161 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000162 bb_show_usage();
Eric Andersend5fa3e32000-08-02 16:42:58 +0000163 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000164 }
Matt Kraai55bccf32001-01-05 02:57:53 +0000165
166 /* open all the files */
167 fds = (int *)xmalloc(sizeof(int) * (argc - optind + 1));
Manuel Novoa III cad53642003-03-19 09:13:01 +0000168
169 argv += optind;
170 nfiles = i = 0;
171
172 if ((argc -= optind) == 0) {
173 struct stat statbuf;
174
175 if (!fstat(STDIN_FILENO, &statbuf) && S_ISFIFO(statbuf.st_mode)) {
176 follow = 0;
Matt Kraai55bccf32001-01-05 02:57:53 +0000177 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000178 /* --argv; */
179 *argv = (char *) bb_msg_standard_input;
180 goto DO_STDIN;
Matt Kraai55bccf32001-01-05 02:57:53 +0000181 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000182
183 do {
184 if ((argv[i][0] == '-') && !argv[i][1]) {
185 DO_STDIN:
186 fds[nfiles] = STDIN_FILENO;
187 } else if ((fds[nfiles] = open(argv[i], O_RDONLY)) < 0) {
188 bb_perror_msg("%s", argv[i]);
189 status = EXIT_FAILURE;
190 continue;
191 }
192 argv[nfiles] = argv[i];
193 ++nfiles;
194 } while (++i < argc);
195
196 if (!nfiles) {
197 bb_error_msg_and_die("no files");
198 }
199
200 tailbufsize = BUFSIZ;
Glenn L McGrath4ef5a842003-10-31 00:35:59 +0000201
Matt Kraai55bccf32001-01-05 02:57:53 +0000202 /* tail the files */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000203 if (from_top < count_bytes) { /* Each is 0 or 1, so true iff 0 < 1. */
204 /* Hence, !from_top && count_bytes */
205 if (tailbufsize < count) {
206 tailbufsize = count + BUFSIZ;
207 }
208 }
Glenn L McGrath4ef5a842003-10-31 00:35:59 +0000209
Manuel Novoa III cad53642003-03-19 09:13:01 +0000210 buf = tailbuf = xmalloc(tailbufsize);
Matt Kraai55bccf32001-01-05 02:57:53 +0000211
Manuel Novoa III cad53642003-03-19 09:13:01 +0000212 fmt = header_fmt + 1; /* Skip header leading newline on first output. */
213 i = 0;
214 do {
215 /* Be careful. It would be possible to optimize the count-bytes
216 * case if the file is seekable. If you do though, remember that
217 * starting file position may not be the beginning of the file.
218 * Beware of backing up too far. See example in wc.c.
219 */
220 if ((!(count|from_top)) && (lseek(fds[i], 0, SEEK_END) >= 0)) {
Eric Andersence98c192001-06-26 15:07:08 +0000221 continue;
222 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000223
224 if (nfiles > header_threshhold) {
225 tail_xprint_header(fmt, argv[i]);
226 fmt = header_fmt;
227 }
228
229 buf = tailbuf;
230 taillen = 0;
231 seen = 1;
Glenn L McGrathf86391e2004-09-30 00:24:21 +0000232 newline = 0;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000233
234 while ((nread = tail_read(fds[i], buf, tailbufsize-taillen)) > 0) {
Matt Kraai55bccf32001-01-05 02:57:53 +0000235 if (from_top) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000236 nwrite = nread;
237 if (seen < count) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000238 if (count_bytes) {
239 nwrite -= (count - seen);
240 seen = count;
Glenn L McGrath4ef5a842003-10-31 00:35:59 +0000241 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000242 s = buf;
243 do {
244 --nwrite;
245 if ((*s++ == '\n') && (++seen == count)) {
Matt Kraai55bccf32001-01-05 02:57:53 +0000246 break;
247 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000248 } while (nwrite);
249 }
250 }
251 tail_xbb_full_write(buf + nread - nwrite, nwrite);
252 } else if (count) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000253 if (count_bytes) {
254 taillen += nread;
255 if (taillen > count) {
256 memmove(tailbuf, tailbuf + taillen - count, count);
257 taillen = count;
258 }
Glenn L McGrath4ef5a842003-10-31 00:35:59 +0000259 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000260 int k = nread;
261 int nbuf = 0;
262
263 while (k) {
264 --k;
265 if (buf[k] == '\n') {
266 ++nbuf;
Matt Kraai55bccf32001-01-05 02:57:53 +0000267 }
268 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000269
270 if (newline + nbuf < count) {
271 newline += nbuf;
272 taillen += nread;
273
Matt Kraai55bccf32001-01-05 02:57:53 +0000274 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000275 int extra = 0;
276 if (buf[nread-1] != '\n') {
277 extra = 1;
278 }
279
280 k = newline + nbuf + extra - count;
281 s = tailbuf;
282 while (k) {
283 if (*s == '\n') {
284 --k;
285 }
286 ++s;
287 }
288
289 taillen += nread - (s - tailbuf);
290 memmove(tailbuf, s, taillen);
291 newline = count - extra;
Matt Kraai55bccf32001-01-05 02:57:53 +0000292 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000293 if (tailbufsize < taillen + BUFSIZ) {
294 tailbufsize = taillen + BUFSIZ;
295 tailbuf = xrealloc(tailbuf, tailbufsize);
Matt Kraai55bccf32001-01-05 02:57:53 +0000296 }
Eric Andersend5fa3e32000-08-02 16:42:58 +0000297 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000298 buf = tailbuf + taillen;
Eric Andersend5fa3e32000-08-02 16:42:58 +0000299 }
300 }
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000301
Manuel Novoa III cad53642003-03-19 09:13:01 +0000302 if (!from_top) {
303 tail_xbb_full_write(tailbuf, taillen);
Matt Kraai55bccf32001-01-05 02:57:53 +0000304 }
305
306 taillen = 0;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000307 } while (++i < nfiles);
308
309 buf = xrealloc(tailbuf, BUFSIZ);
310
311 fmt = NULL;
Matt Kraai55bccf32001-01-05 02:57:53 +0000312
313 while (follow) {
314 sleep(sleep_period);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000315 i = 0;
316 do {
317 if (nfiles > header_threshhold) {
318 fmt = header_fmt;
Matt Kraai55bccf32001-01-05 02:57:53 +0000319 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000320 while ((nread = tail_read(fds[i], buf, sizeof(buf))) > 0) {
321 if (fmt) {
322 tail_xprint_header(fmt, argv[i]);
323 fmt = NULL;
324 }
325 tail_xbb_full_write(buf, nread);
Matt Kraai55bccf32001-01-05 02:57:53 +0000326 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000327 } while (++i < nfiles);
Matt Kraai55bccf32001-01-05 02:57:53 +0000328 }
329
330 return status;
331}