blob: 629ee212534a406f0410f323b7c7e666585160c2 [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
Denis Vlasenkoac678ec2007-04-16 22:32:04 +000068static unsigned eat_num(const char *p)
69{
Bernhard Reutner-Fischer84d2d492007-01-24 21:38:10 +000070 if (*p == '-') p++;
Bernhard Reutner-Fischerd9c2d5f2007-04-04 20:29:15 +000071 else if (*p == '+') { p++; G.status = EXIT_FAILURE; }
Bernhard Reutner-Fischer84d2d492007-01-24 21:38:10 +000072 return xatou_sfx(p, tail_suffixes);
73}
74
Denis Vlasenko06af2162007-02-03 17:28:39 +000075int tail_main(int argc, char **argv);
Eric Andersen98bbd682000-07-31 17:05:58 +000076int tail_main(int argc, char **argv)
Eric Andersenabc0f4f1999-12-08 23:19:36 +000077{
Denis Vlasenkoe31f7212006-12-22 16:06:16 +000078 unsigned count = 10;
Denis Vlasenko459903b2006-11-27 14:44:18 +000079 unsigned sleep_period = 1;
Bernhard Reutner-Fischer84d2d492007-01-24 21:38:10 +000080 bool from_top;
Manuel Novoa III cad53642003-03-19 09:13:01 +000081 int header_threshhold = 1;
Denis Vlasenkoe31f7212006-12-22 16:06:16 +000082 const char *str_c, *str_n, *str_s;
Eric Andersenabc0f4f1999-12-08 23:19:36 +000083
Manuel Novoa III cad53642003-03-19 09:13:01 +000084 char *tailbuf;
85 size_t tailbufsize;
86 int taillen = 0;
87 int newline = 0;
Denis Vlasenkoe31f7212006-12-22 16:06:16 +000088 int nfiles, nread, nwrite, seen, i, opt;
Manuel Novoa III cad53642003-03-19 09:13:01 +000089
Denis Vlasenkoe31f7212006-12-22 16:06:16 +000090 int *fds;
Manuel Novoa III cad53642003-03-19 09:13:01 +000091 char *s, *buf;
92 const char *fmt;
93
Denis Vlasenko08492072006-12-22 13:56:36 +000094#if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_TAIL
Manuel Novoa III cad53642003-03-19 09:13:01 +000095 /* Allow legacy syntax of an initial numeric option without -n. */
Denis Vlasenko459903b2006-11-27 14:44:18 +000096 if (argc >= 2 && (argv[1][0] == '+' || argv[1][0] == '-')
97 && isdigit(argv[1][1])
98 ) {
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +000099 argv[0] = (char*)"-n";
Denis Vlasenkoe31f7212006-12-22 16:06:16 +0000100 argv--;
101 argc++;
Robert Griebl13c26fc2002-05-17 22:18:04 +0000102 }
Glenn L McGrath0bd02572005-12-11 03:09:05 +0000103#endif
Robert Griebl13c26fc2002-05-17 22:18:04 +0000104
Denis Vlasenkoe31f7212006-12-22 16:06:16 +0000105 opt = getopt32(argc, argv, "fc:n:" USE_FEATURE_FANCY_TAIL("qs:v"), &str_c, &str_n, &str_s);
106#define FOLLOW (opt & 0x1)
107#define COUNT_BYTES (opt & 0x2)
108 //if (opt & 0x1) // -f
Bernhard Reutner-Fischer84d2d492007-01-24 21:38:10 +0000109 if (opt & 0x2) count = eat_num(str_c); // -c
110 if (opt & 0x4) count = eat_num(str_n); // -n
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +0000111#if ENABLE_FEATURE_FANCY_TAIL
Denis Vlasenkoe31f7212006-12-22 16:06:16 +0000112 if (opt & 0x8) header_threshhold = INT_MAX; // -q
113 if (opt & 0x10) sleep_period = xatou(str_s); // -s
114 if (opt & 0x20) header_threshhold = 0; // -v
Matt Kraai55bccf32001-01-05 02:57:53 +0000115#endif
Denis Vlasenko69107412006-12-21 00:43:06 +0000116 argc -= optind;
117 argv += optind;
Bernhard Reutner-Fischerd9c2d5f2007-04-04 20:29:15 +0000118 from_top = G.status;
Matt Kraai55bccf32001-01-05 02:57:53 +0000119
120 /* open all the files */
Denis Vlasenko69107412006-12-21 00:43:06 +0000121 fds = xmalloc(sizeof(int) * (argc + 1));
Bernhard Reutner-Fischerd9c2d5f2007-04-04 20:29:15 +0000122 nfiles = i = 0;
123 G.status = EXIT_SUCCESS;
Denis Vlasenko69107412006-12-21 00:43:06 +0000124 if (argc == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000125 struct stat statbuf;
126
127 if (!fstat(STDIN_FILENO, &statbuf) && S_ISFIFO(statbuf.st_mode)) {
Denis Vlasenkoe31f7212006-12-22 16:06:16 +0000128 opt &= ~1; /* clear FOLLOW */
Matt Kraai55bccf32001-01-05 02:57:53 +0000129 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000130 *argv = (char *) bb_msg_standard_input;
Matt Kraai55bccf32001-01-05 02:57:53 +0000131 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000132 do {
Bernhard Reutner-Fischerd9c2d5f2007-04-04 20:29:15 +0000133 FILE* fil = fopen_or_warn_stdin(argv[i]);
134 if (!fil) {
135 G.status = EXIT_FAILURE;
136 continue;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000137 }
Bernhard Reutner-Fischerd9c2d5f2007-04-04 20:29:15 +0000138 fds[nfiles] = fileno(fil);
139 argv[nfiles++] = argv[i];
Manuel Novoa III cad53642003-03-19 09:13:01 +0000140 } while (++i < argc);
141
Denis Vlasenkoe31f7212006-12-22 16:06:16 +0000142 if (!nfiles)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000143 bb_error_msg_and_die("no files");
Manuel Novoa III cad53642003-03-19 09:13:01 +0000144
145 tailbufsize = BUFSIZ;
Glenn L McGrath4ef5a842003-10-31 00:35:59 +0000146
Matt Kraai55bccf32001-01-05 02:57:53 +0000147 /* tail the files */
Denis Vlasenkoe31f7212006-12-22 16:06:16 +0000148 if (!from_top && COUNT_BYTES) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000149 if (tailbufsize < count) {
150 tailbufsize = count + BUFSIZ;
151 }
152 }
Glenn L McGrath4ef5a842003-10-31 00:35:59 +0000153
Manuel Novoa III cad53642003-03-19 09:13:01 +0000154 buf = tailbuf = xmalloc(tailbufsize);
Matt Kraai55bccf32001-01-05 02:57:53 +0000155
Manuel Novoa III cad53642003-03-19 09:13:01 +0000156 fmt = header_fmt + 1; /* Skip header leading newline on first output. */
157 i = 0;
158 do {
159 /* Be careful. It would be possible to optimize the count-bytes
160 * case if the file is seekable. If you do though, remember that
161 * starting file position may not be the beginning of the file.
162 * Beware of backing up too far. See example in wc.c.
163 */
Denis Vlasenkoace35ee2007-01-02 16:32:16 +0000164 if (!(count | from_top) && lseek(fds[i], 0, SEEK_END) >= 0) {
Eric Andersence98c192001-06-26 15:07:08 +0000165 continue;
166 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000167
168 if (nfiles > header_threshhold) {
169 tail_xprint_header(fmt, argv[i]);
170 fmt = header_fmt;
171 }
172
173 buf = tailbuf;
174 taillen = 0;
175 seen = 1;
Glenn L McGrathf86391e2004-09-30 00:24:21 +0000176 newline = 0;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000177
178 while ((nread = tail_read(fds[i], buf, tailbufsize-taillen)) > 0) {
Matt Kraai55bccf32001-01-05 02:57:53 +0000179 if (from_top) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000180 nwrite = nread;
181 if (seen < count) {
Denis Vlasenkoe31f7212006-12-22 16:06:16 +0000182 if (COUNT_BYTES) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000183 nwrite -= (count - seen);
184 seen = count;
Glenn L McGrath4ef5a842003-10-31 00:35:59 +0000185 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000186 s = buf;
187 do {
188 --nwrite;
Denis Vlasenkoe31f7212006-12-22 16:06:16 +0000189 if (*s++ == '\n' && ++seen == count) {
Matt Kraai55bccf32001-01-05 02:57:53 +0000190 break;
191 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000192 } while (nwrite);
193 }
194 }
Denis Vlasenkoe31f7212006-12-22 16:06:16 +0000195 xwrite(STDOUT_FILENO, buf + nread - nwrite, nwrite);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000196 } else if (count) {
Denis Vlasenkoe31f7212006-12-22 16:06:16 +0000197 if (COUNT_BYTES) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000198 taillen += nread;
199 if (taillen > count) {
200 memmove(tailbuf, tailbuf + taillen - count, count);
201 taillen = count;
202 }
Glenn L McGrath4ef5a842003-10-31 00:35:59 +0000203 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000204 int k = nread;
205 int nbuf = 0;
206
207 while (k) {
208 --k;
209 if (buf[k] == '\n') {
210 ++nbuf;
Matt Kraai55bccf32001-01-05 02:57:53 +0000211 }
212 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000213
214 if (newline + nbuf < count) {
215 newline += nbuf;
216 taillen += nread;
Matt Kraai55bccf32001-01-05 02:57:53 +0000217 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000218 int extra = 0;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000219
Bernhard Reutner-Fischerd9c2d5f2007-04-04 20:29:15 +0000220 if (buf[nread-1] != '\n')
221 extra = 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000222 k = newline + nbuf + extra - count;
223 s = tailbuf;
224 while (k) {
225 if (*s == '\n') {
226 --k;
227 }
228 ++s;
229 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000230 taillen += nread - (s - tailbuf);
231 memmove(tailbuf, s, taillen);
232 newline = count - extra;
Matt Kraai55bccf32001-01-05 02:57:53 +0000233 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000234 if (tailbufsize < taillen + BUFSIZ) {
235 tailbufsize = taillen + BUFSIZ;
236 tailbuf = xrealloc(tailbuf, tailbufsize);
Matt Kraai55bccf32001-01-05 02:57:53 +0000237 }
Eric Andersend5fa3e32000-08-02 16:42:58 +0000238 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000239 buf = tailbuf + taillen;
Eric Andersend5fa3e32000-08-02 16:42:58 +0000240 }
241 }
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000242
Manuel Novoa III cad53642003-03-19 09:13:01 +0000243 if (!from_top) {
Denis Vlasenkoe31f7212006-12-22 16:06:16 +0000244 xwrite(STDOUT_FILENO, tailbuf, taillen);
Matt Kraai55bccf32001-01-05 02:57:53 +0000245 }
246
247 taillen = 0;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000248 } while (++i < nfiles);
249
250 buf = xrealloc(tailbuf, BUFSIZ);
251
252 fmt = NULL;
Matt Kraai55bccf32001-01-05 02:57:53 +0000253
Denis Vlasenkoe31f7212006-12-22 16:06:16 +0000254 if (FOLLOW) while (1) {
Matt Kraai55bccf32001-01-05 02:57:53 +0000255 sleep(sleep_period);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000256 i = 0;
257 do {
258 if (nfiles > header_threshhold) {
259 fmt = header_fmt;
Matt Kraai55bccf32001-01-05 02:57:53 +0000260 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000261 while ((nread = tail_read(fds[i], buf, sizeof(buf))) > 0) {
262 if (fmt) {
263 tail_xprint_header(fmt, argv[i]);
264 fmt = NULL;
265 }
Denis Vlasenkoe31f7212006-12-22 16:06:16 +0000266 xwrite(STDOUT_FILENO, buf, nread);
Matt Kraai55bccf32001-01-05 02:57:53 +0000267 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000268 } while (++i < nfiles);
Matt Kraai55bccf32001-01-05 02:57:53 +0000269 }
Bernhard Reutner-Fischerd9c2d5f2007-04-04 20:29:15 +0000270 if (ENABLE_FEATURE_CLEAN_UP) {
271 free(fds);
272 }
273 return G.status;
Matt Kraai55bccf32001-01-05 02:57:53 +0000274}