blob: 67396ab1c52456c60f8aebf66d3fcf6a3cefb604 [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 },
Denis Vlasenkoe31f7212006-12-22 16:06:16 +000032 { "m", 1024*1024 },
Matt Kraai24ac0172000-12-18 21:38:57 +000033 { NULL, 0 }
34};
35
Bernhard Reutner-Fischerd9c2d5f2007-04-04 20:29:15 +000036struct globals {
37 bool status;
38};
39#define G (*(struct globals*)&bb_common_bufsiz1)
Matt Kraai55bccf32001-01-05 02:57:53 +000040
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000041static void tail_xprint_header(const char *fmt, const char *filename)
42{
Denis Vlasenkoe31f7212006-12-22 16:06:16 +000043 if (fdprintf(STDOUT_FILENO, fmt, filename) < 0)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000044 bb_perror_nomsg_and_die();
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000045}
46
Manuel Novoa III cad53642003-03-19 09:13:01 +000047static ssize_t tail_read(int fd, char *buf, size_t count)
48{
49 ssize_t r;
Denis Vlasenkoe31f7212006-12-22 16:06:16 +000050 off_t current, end;
Paul Fox49054342005-07-20 19:46:32 +000051 struct stat sbuf;
Manuel Novoa III cad53642003-03-19 09:13:01 +000052
Mike Frysingerdbc049f2005-07-26 22:57:51 +000053 end = current = lseek(fd, 0, SEEK_CUR);
54 if (!fstat(fd, &sbuf))
55 end = sbuf.st_size;
Rob Landley58a651b2005-08-13 00:35:00 +000056 lseek(fd, end < current ? 0 : current, SEEK_SET);
Denis Vlasenkoe31f7212006-12-22 16:06:16 +000057 r = safe_read(fd, buf, count);
58 if (r < 0) {
Bernhard Reutner-Fischer1b9d7c92006-06-03 22:45:37 +000059 bb_perror_msg(bb_msg_read_error);
Bernhard Reutner-Fischerd9c2d5f2007-04-04 20:29:15 +000060 G.status = EXIT_FAILURE;
Manuel Novoa III cad53642003-03-19 09:13:01 +000061 }
62
63 return r;
64}
65
Manuel Novoa III cad53642003-03-19 09:13:01 +000066static const char header_fmt[] = "\n==> %s <==\n";
67
Bernhard Reutner-Fischer84d2d492007-01-24 21:38:10 +000068static unsigned eat_num(const char *p) {
69 if (*p == '-') p++;
Bernhard Reutner-Fischerd9c2d5f2007-04-04 20:29:15 +000070 else if (*p == '+') { p++; G.status = EXIT_FAILURE; }
Bernhard Reutner-Fischer84d2d492007-01-24 21:38:10 +000071 return xatou_sfx(p, tail_suffixes);
72}
73
Denis Vlasenko06af2162007-02-03 17:28:39 +000074int tail_main(int argc, char **argv);
Eric Andersen98bbd682000-07-31 17:05:58 +000075int tail_main(int argc, char **argv)
Eric Andersenabc0f4f1999-12-08 23:19:36 +000076{
Denis Vlasenkoe31f7212006-12-22 16:06:16 +000077 unsigned count = 10;
Denis Vlasenko459903b2006-11-27 14:44:18 +000078 unsigned sleep_period = 1;
Bernhard Reutner-Fischer84d2d492007-01-24 21:38:10 +000079 bool from_top;
Manuel Novoa III cad53642003-03-19 09:13:01 +000080 int header_threshhold = 1;
Denis Vlasenkoe31f7212006-12-22 16:06:16 +000081 const char *str_c, *str_n, *str_s;
Eric Andersenabc0f4f1999-12-08 23:19:36 +000082
Manuel Novoa III cad53642003-03-19 09:13:01 +000083 char *tailbuf;
84 size_t tailbufsize;
85 int taillen = 0;
86 int newline = 0;
Denis Vlasenkoe31f7212006-12-22 16:06:16 +000087 int nfiles, nread, nwrite, seen, i, opt;
Manuel Novoa III cad53642003-03-19 09:13:01 +000088
Denis Vlasenkoe31f7212006-12-22 16:06:16 +000089 int *fds;
Manuel Novoa III cad53642003-03-19 09:13:01 +000090 char *s, *buf;
91 const char *fmt;
92
Denis Vlasenko08492072006-12-22 13:56:36 +000093#if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_TAIL
Manuel Novoa III cad53642003-03-19 09:13:01 +000094 /* Allow legacy syntax of an initial numeric option without -n. */
Denis Vlasenko459903b2006-11-27 14:44:18 +000095 if (argc >= 2 && (argv[1][0] == '+' || argv[1][0] == '-')
96 && isdigit(argv[1][1])
97 ) {
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +000098 argv[0] = (char*)"-n";
Denis Vlasenkoe31f7212006-12-22 16:06:16 +000099 argv--;
100 argc++;
Robert Griebl13c26fc2002-05-17 22:18:04 +0000101 }
Glenn L McGrath0bd02572005-12-11 03:09:05 +0000102#endif
Robert Griebl13c26fc2002-05-17 22:18:04 +0000103
Denis Vlasenkoe31f7212006-12-22 16:06:16 +0000104 opt = getopt32(argc, argv, "fc:n:" USE_FEATURE_FANCY_TAIL("qs:v"), &str_c, &str_n, &str_s);
105#define FOLLOW (opt & 0x1)
106#define COUNT_BYTES (opt & 0x2)
107 //if (opt & 0x1) // -f
Bernhard Reutner-Fischer84d2d492007-01-24 21:38:10 +0000108 if (opt & 0x2) count = eat_num(str_c); // -c
109 if (opt & 0x4) count = eat_num(str_n); // -n
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +0000110#if ENABLE_FEATURE_FANCY_TAIL
Denis Vlasenkoe31f7212006-12-22 16:06:16 +0000111 if (opt & 0x8) header_threshhold = INT_MAX; // -q
112 if (opt & 0x10) sleep_period = xatou(str_s); // -s
113 if (opt & 0x20) header_threshhold = 0; // -v
Matt Kraai55bccf32001-01-05 02:57:53 +0000114#endif
Denis Vlasenko69107412006-12-21 00:43:06 +0000115 argc -= optind;
116 argv += optind;
Bernhard Reutner-Fischerd9c2d5f2007-04-04 20:29:15 +0000117 from_top = G.status;
Matt Kraai55bccf32001-01-05 02:57:53 +0000118
119 /* open all the files */
Denis Vlasenko69107412006-12-21 00:43:06 +0000120 fds = xmalloc(sizeof(int) * (argc + 1));
Bernhard Reutner-Fischerd9c2d5f2007-04-04 20:29:15 +0000121 nfiles = i = 0;
122 G.status = EXIT_SUCCESS;
Denis Vlasenko69107412006-12-21 00:43:06 +0000123 if (argc == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000124 struct stat statbuf;
125
126 if (!fstat(STDIN_FILENO, &statbuf) && S_ISFIFO(statbuf.st_mode)) {
Denis Vlasenkoe31f7212006-12-22 16:06:16 +0000127 opt &= ~1; /* clear FOLLOW */
Matt Kraai55bccf32001-01-05 02:57:53 +0000128 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000129 *argv = (char *) bb_msg_standard_input;
Matt Kraai55bccf32001-01-05 02:57:53 +0000130 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000131 do {
Bernhard Reutner-Fischerd9c2d5f2007-04-04 20:29:15 +0000132 FILE* fil = fopen_or_warn_stdin(argv[i]);
133 if (!fil) {
134 G.status = EXIT_FAILURE;
135 continue;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000136 }
Bernhard Reutner-Fischerd9c2d5f2007-04-04 20:29:15 +0000137 fds[nfiles] = fileno(fil);
138 argv[nfiles++] = argv[i];
Manuel Novoa III cad53642003-03-19 09:13:01 +0000139 } while (++i < argc);
140
Denis Vlasenkoe31f7212006-12-22 16:06:16 +0000141 if (!nfiles)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000142 bb_error_msg_and_die("no files");
Manuel Novoa III cad53642003-03-19 09:13:01 +0000143
144 tailbufsize = BUFSIZ;
Glenn L McGrath4ef5a842003-10-31 00:35:59 +0000145
Matt Kraai55bccf32001-01-05 02:57:53 +0000146 /* tail the files */
Denis Vlasenkoe31f7212006-12-22 16:06:16 +0000147 if (!from_top && COUNT_BYTES) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000148 if (tailbufsize < count) {
149 tailbufsize = count + BUFSIZ;
150 }
151 }
Glenn L McGrath4ef5a842003-10-31 00:35:59 +0000152
Manuel Novoa III cad53642003-03-19 09:13:01 +0000153 buf = tailbuf = xmalloc(tailbufsize);
Matt Kraai55bccf32001-01-05 02:57:53 +0000154
Manuel Novoa III cad53642003-03-19 09:13:01 +0000155 fmt = header_fmt + 1; /* Skip header leading newline on first output. */
156 i = 0;
157 do {
158 /* Be careful. It would be possible to optimize the count-bytes
159 * case if the file is seekable. If you do though, remember that
160 * starting file position may not be the beginning of the file.
161 * Beware of backing up too far. See example in wc.c.
162 */
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000163 if (!(count | from_top) && lseek(fds[i], 0, SEEK_END) >= 0) {
Eric Andersence98c192001-06-26 15:07:08 +0000164 continue;
165 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000166
167 if (nfiles > header_threshhold) {
168 tail_xprint_header(fmt, argv[i]);
169 fmt = header_fmt;
170 }
171
172 buf = tailbuf;
173 taillen = 0;
174 seen = 1;
Glenn L McGrathf86391e2004-09-30 00:24:21 +0000175 newline = 0;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000176
177 while ((nread = tail_read(fds[i], buf, tailbufsize-taillen)) > 0) {
Matt Kraai55bccf32001-01-05 02:57:53 +0000178 if (from_top) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000179 nwrite = nread;
180 if (seen < count) {
Denis Vlasenkoe31f7212006-12-22 16:06:16 +0000181 if (COUNT_BYTES) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000182 nwrite -= (count - seen);
183 seen = count;
Glenn L McGrath4ef5a842003-10-31 00:35:59 +0000184 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000185 s = buf;
186 do {
187 --nwrite;
Denis Vlasenkoe31f7212006-12-22 16:06:16 +0000188 if (*s++ == '\n' && ++seen == count) {
Matt Kraai55bccf32001-01-05 02:57:53 +0000189 break;
190 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000191 } while (nwrite);
192 }
193 }
Denis Vlasenkoe31f7212006-12-22 16:06:16 +0000194 xwrite(STDOUT_FILENO, buf + nread - nwrite, nwrite);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000195 } else if (count) {
Denis Vlasenkoe31f7212006-12-22 16:06:16 +0000196 if (COUNT_BYTES) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000197 taillen += nread;
198 if (taillen > count) {
199 memmove(tailbuf, tailbuf + taillen - count, count);
200 taillen = count;
201 }
Glenn L McGrath4ef5a842003-10-31 00:35:59 +0000202 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000203 int k = nread;
204 int nbuf = 0;
205
206 while (k) {
207 --k;
208 if (buf[k] == '\n') {
209 ++nbuf;
Matt Kraai55bccf32001-01-05 02:57:53 +0000210 }
211 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000212
213 if (newline + nbuf < count) {
214 newline += nbuf;
215 taillen += nread;
Matt Kraai55bccf32001-01-05 02:57:53 +0000216 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000217 int extra = 0;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000218
Bernhard Reutner-Fischerd9c2d5f2007-04-04 20:29:15 +0000219 if (buf[nread-1] != '\n')
220 extra = 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000221 k = newline + nbuf + extra - count;
222 s = tailbuf;
223 while (k) {
224 if (*s == '\n') {
225 --k;
226 }
227 ++s;
228 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000229 taillen += nread - (s - tailbuf);
230 memmove(tailbuf, s, taillen);
231 newline = count - extra;
Matt Kraai55bccf32001-01-05 02:57:53 +0000232 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000233 if (tailbufsize < taillen + BUFSIZ) {
234 tailbufsize = taillen + BUFSIZ;
235 tailbuf = xrealloc(tailbuf, tailbufsize);
Matt Kraai55bccf32001-01-05 02:57:53 +0000236 }
Eric Andersend5fa3e32000-08-02 16:42:58 +0000237 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000238 buf = tailbuf + taillen;
Eric Andersend5fa3e32000-08-02 16:42:58 +0000239 }
240 }
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000241
Manuel Novoa III cad53642003-03-19 09:13:01 +0000242 if (!from_top) {
Denis Vlasenkoe31f7212006-12-22 16:06:16 +0000243 xwrite(STDOUT_FILENO, tailbuf, taillen);
Matt Kraai55bccf32001-01-05 02:57:53 +0000244 }
245
246 taillen = 0;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000247 } while (++i < nfiles);
248
249 buf = xrealloc(tailbuf, BUFSIZ);
250
251 fmt = NULL;
Matt Kraai55bccf32001-01-05 02:57:53 +0000252
Denis Vlasenkoe31f7212006-12-22 16:06:16 +0000253 if (FOLLOW) while (1) {
Matt Kraai55bccf32001-01-05 02:57:53 +0000254 sleep(sleep_period);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000255 i = 0;
256 do {
257 if (nfiles > header_threshhold) {
258 fmt = header_fmt;
Matt Kraai55bccf32001-01-05 02:57:53 +0000259 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000260 while ((nread = tail_read(fds[i], buf, sizeof(buf))) > 0) {
261 if (fmt) {
262 tail_xprint_header(fmt, argv[i]);
263 fmt = NULL;
264 }
Denis Vlasenkoe31f7212006-12-22 16:06:16 +0000265 xwrite(STDOUT_FILENO, buf, nread);
Matt Kraai55bccf32001-01-05 02:57:53 +0000266 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000267 } while (++i < nfiles);
Matt Kraai55bccf32001-01-05 02:57:53 +0000268 }
Bernhard Reutner-Fischerd9c2d5f2007-04-04 20:29:15 +0000269 if (ENABLE_FEATURE_CLEAN_UP) {
270 free(fds);
271 }
272 return G.status;
Matt Kraai55bccf32001-01-05 02:57:53 +0000273}