factor: fix comments
[oweals/busybox.git] / coreutils / tail.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini tail implementation for busybox
4  *
5  * Copyright (C) 2001 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8  */
9 /* Mar 16, 2003      Manuel Novoa III   (mjn3@codepoet.org)
10  *
11  * Pretty much rewritten to fix numerous bugs and reduce realloc() calls.
12  * Bugs fixed (although I may have forgotten one or two... it was pretty bad)
13  * 1) mixing printf/write without fflush()ing stdout
14  * 2) no check that any open files are present
15  * 3) optstring had -q taking an arg
16  * 4) no error checking on write in some cases, and a warning even then
17  * 5) q and s interaction bug
18  * 6) no check for lseek error
19  * 7) lseek attempted when count==0 even if arg was +0 (from top)
20  */
21 //config:config TAIL
22 //config:       bool "tail"
23 //config:       default y
24 //config:       help
25 //config:         tail is used to print the last specified number of lines
26 //config:         from files.
27 //config:
28 //config:config FEATURE_FANCY_TAIL
29 //config:       bool "Enable -q, -s, -v, and -F options"
30 //config:       default y
31 //config:       depends on TAIL
32 //config:       help
33 //config:         These options are provided by GNU tail, but
34 //config:         are not specific in the SUSv3 standard:
35 //config:           -q      Never output headers giving file names
36 //config:           -s SEC  Wait SEC seconds between reads with -f
37 //config:           -v      Always output headers giving file names
38 //config:           -F      Same as -f, but keep retrying
39
40 //applet:IF_TAIL(APPLET(tail, BB_DIR_USR_BIN, BB_SUID_DROP))
41
42 //kbuild:lib-$(CONFIG_TAIL) += tail.o
43
44 /* BB_AUDIT SUSv3 compliant (need fancy for -c) */
45 /* BB_AUDIT GNU compatible -c, -q, and -v options in 'fancy' configuration. */
46 /* http://www.opengroup.org/onlinepubs/007904975/utilities/tail.html */
47
48 //usage:#define tail_trivial_usage
49 //usage:       "[OPTIONS] [FILE]..."
50 //usage:#define tail_full_usage "\n\n"
51 //usage:       "Print last 10 lines of each FILE (or stdin) to stdout.\n"
52 //usage:       "With more than one FILE, precede each with a filename header.\n"
53 //usage:     "\n        -f              Print data as file grows"
54 //usage:     "\n        -c [+]N[kbm]    Print last N bytes"
55 //usage:     "\n        -n N[kbm]       Print last N lines"
56 //usage:     "\n        -n +N[kbm]      Start on Nth line and print the rest"
57 //usage:        IF_FEATURE_FANCY_TAIL(
58 //usage:     "\n        -q              Never print headers"
59 //usage:     "\n        -s SECONDS      Wait SECONDS between reads with -f"
60 //usage:     "\n        -v              Always print headers"
61 //usage:     "\n        -F              Same as -f, but keep retrying"
62 //usage:     "\n"
63 //usage:     "\nN may be suffixed by k (x1024), b (x512), or m (x1024^2)."
64 //usage:        )
65 //usage:
66 //usage:#define tail_example_usage
67 //usage:       "$ tail -n 1 /etc/resolv.conf\n"
68 //usage:       "nameserver 10.0.0.1\n"
69
70 #include "libbb.h"
71 #include "common_bufsiz.h"
72
73 struct globals {
74         bool from_top;
75         bool exitcode;
76 } FIX_ALIASING;
77 #define G (*(struct globals*)bb_common_bufsiz1)
78 #define INIT_G() do { setup_common_bufsiz(); } while (0)
79
80 static void tail_xprint_header(const char *fmt, const char *filename)
81 {
82         if (fdprintf(STDOUT_FILENO, fmt, filename) < 0)
83                 bb_perror_nomsg_and_die();
84 }
85
86 static ssize_t tail_read(int fd, char *buf, size_t count)
87 {
88         ssize_t r;
89
90         r = full_read(fd, buf, count);
91         if (r < 0) {
92                 bb_perror_msg(bb_msg_read_error);
93                 G.exitcode = EXIT_FAILURE;
94         }
95
96         return r;
97 }
98
99 #define header_fmt_str "\n==> %s <==\n"
100
101 static unsigned eat_num(const char *p)
102 {
103         if (*p == '-')
104                 p++;
105         else if (*p == '+') {
106                 p++;
107                 G.from_top = 1;
108         }
109         return xatou_sfx(p, bkm_suffixes);
110 }
111
112 int tail_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
113 int tail_main(int argc, char **argv)
114 {
115         unsigned count = 10;
116         unsigned sleep_period = 1;
117         const char *str_c, *str_n;
118
119         char *tailbuf;
120         size_t tailbufsize;
121         unsigned header_threshhold = 1;
122         unsigned nfiles;
123         int i, opt;
124
125         int *fds;
126         const char *fmt;
127         int prev_fd;
128
129         INIT_G();
130
131 #if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_TAIL
132         /* Allow legacy syntax of an initial numeric option without -n. */
133         if (argv[1] && (argv[1][0] == '+' || argv[1][0] == '-')
134          && isdigit(argv[1][1])
135         ) {
136                 count = eat_num(argv[1]);
137                 argv++;
138                 argc--;
139         }
140 #endif
141
142         /* -s NUM, -F imlies -f */
143         IF_FEATURE_FANCY_TAIL(opt_complementary = "Ff";)
144         opt = getopt32(argv, "fc:n:" IF_FEATURE_FANCY_TAIL("qs:+vF"),
145                         &str_c, &str_n IF_FEATURE_FANCY_TAIL(,&sleep_period));
146 #define FOLLOW (opt & 0x1)
147 #define COUNT_BYTES (opt & 0x2)
148         //if (opt & 0x1) // -f
149         if (opt & 0x2) count = eat_num(str_c); // -c
150         if (opt & 0x4) count = eat_num(str_n); // -n
151 #if ENABLE_FEATURE_FANCY_TAIL
152         /* q: make it impossible for nfiles to be > header_threshhold */
153         if (opt & 0x8) header_threshhold = UINT_MAX; // -q
154         //if (opt & 0x10) // -s
155         if (opt & 0x20) header_threshhold = 0; // -v
156 # define FOLLOW_RETRY (opt & 0x40)
157 #else
158 # define FOLLOW_RETRY 0
159 #endif
160         argc -= optind;
161         argv += optind;
162
163         /* open all the files */
164         fds = xmalloc(sizeof(fds[0]) * (argc + 1));
165         if (!argv[0]) {
166                 struct stat statbuf;
167
168                 if (fstat(STDIN_FILENO, &statbuf) == 0
169                  && S_ISFIFO(statbuf.st_mode)
170                 ) {
171                         opt &= ~1; /* clear FOLLOW */
172                 }
173                 argv[0] = (char *) bb_msg_standard_input;
174         }
175         nfiles = i = 0;
176         do {
177                 int fd = open_or_warn_stdin(argv[i]);
178                 if (fd < 0 && !FOLLOW_RETRY) {
179                         G.exitcode = EXIT_FAILURE;
180                         continue;
181                 }
182                 fds[nfiles] = fd;
183                 argv[nfiles++] = argv[i];
184         } while (++i < argc);
185
186         if (!nfiles)
187                 bb_error_msg_and_die("no files");
188
189         /* prepare the buffer */
190         tailbufsize = BUFSIZ;
191         if (!G.from_top && COUNT_BYTES) {
192                 if (tailbufsize < count + BUFSIZ) {
193                         tailbufsize = count + BUFSIZ;
194                 }
195         }
196         /* tail -c1024m REGULAR_FILE doesn't really need 1G mem block.
197          * (In fact, it doesn't need ANY memory). So delay allocation.
198          */
199         tailbuf = NULL;
200
201         /* tail the files */
202
203         fmt = header_fmt_str + 1; /* skip leading newline in the header on the first output */
204         i = 0;
205         do {
206                 char *buf;
207                 int taillen;
208                 int newlines_seen;
209                 unsigned seen;
210                 int nread;
211                 int fd = fds[i];
212
213                 if (ENABLE_FEATURE_FANCY_TAIL && fd < 0)
214                         continue; /* may happen with -F */
215
216                 if (nfiles > header_threshhold) {
217                         tail_xprint_header(fmt, argv[i]);
218                         fmt = header_fmt_str;
219                 }
220
221                 if (!G.from_top) {
222                         off_t current = lseek(fd, 0, SEEK_END);
223                         if (current > 0) {
224                                 unsigned off;
225                                 if (COUNT_BYTES) {
226                                 /* Optimizing count-bytes case if the file is seekable.
227                                  * Beware of backing up too far.
228                                  * Also we exclude files with size 0 (because of /proc/xxx) */
229                                         if (count == 0)
230                                                 continue; /* showing zero bytes is easy :) */
231                                         current -= count;
232                                         if (current < 0)
233                                                 current = 0;
234                                         xlseek(fd, current, SEEK_SET);
235                                         bb_copyfd_size(fd, STDOUT_FILENO, count);
236                                         continue;
237                                 }
238 #if 1 /* This is technically incorrect for *LONG* strings, but very useful */
239                                 /* Optimizing count-lines case if the file is seekable.
240                                  * We assume the lines are <64k.
241                                  * (Users complain that tail takes too long
242                                  * on multi-gigabyte files) */
243                                 off = (count | 0xf); /* for small counts, be more paranoid */
244                                 if (off > (INT_MAX / (64*1024)))
245                                         off = (INT_MAX / (64*1024));
246                                 current -= off * (64*1024);
247                                 if (current < 0)
248                                         current = 0;
249                                 xlseek(fd, current, SEEK_SET);
250 #endif
251                         }
252                 }
253
254                 if (!tailbuf)
255                         tailbuf = xmalloc(tailbufsize);
256
257                 buf = tailbuf;
258                 taillen = 0;
259                 /* "We saw 1st line/byte".
260                  * Used only by +N code ("start from Nth", 1-based): */
261                 seen = 1;
262                 newlines_seen = 0;
263                 while ((nread = tail_read(fd, buf, tailbufsize - taillen)) > 0) {
264                         if (G.from_top) {
265                                 int nwrite = nread;
266                                 if (seen < count) {
267                                         /* We need to skip a few more bytes/lines */
268                                         if (COUNT_BYTES) {
269                                                 nwrite -= (count - seen);
270                                                 seen += nread;
271                                         } else {
272                                                 char *s = buf;
273                                                 do {
274                                                         --nwrite;
275                                                         if (*s++ == '\n' && ++seen == count) {
276                                                                 break;
277                                                         }
278                                                 } while (nwrite);
279                                         }
280                                 }
281                                 if (nwrite > 0)
282                                         xwrite(STDOUT_FILENO, buf + nread - nwrite, nwrite);
283                         } else if (count) {
284                                 if (COUNT_BYTES) {
285                                         taillen += nread;
286                                         if (taillen > (int)count) {
287                                                 memmove(tailbuf, tailbuf + taillen - count, count);
288                                                 taillen = count;
289                                         }
290                                 } else {
291                                         int k = nread;
292                                         int newlines_in_buf = 0;
293
294                                         do { /* count '\n' in last read */
295                                                 k--;
296                                                 if (buf[k] == '\n') {
297                                                         newlines_in_buf++;
298                                                 }
299                                         } while (k);
300
301                                         if (newlines_seen + newlines_in_buf < (int)count) {
302                                                 newlines_seen += newlines_in_buf;
303                                                 taillen += nread;
304                                         } else {
305                                                 int extra = (buf[nread-1] != '\n');
306                                                 char *s;
307
308                                                 k = newlines_seen + newlines_in_buf + extra - count;
309                                                 s = tailbuf;
310                                                 while (k) {
311                                                         if (*s == '\n') {
312                                                                 k--;
313                                                         }
314                                                         s++;
315                                                 }
316                                                 taillen += nread - (s - tailbuf);
317                                                 memmove(tailbuf, s, taillen);
318                                                 newlines_seen = count - extra;
319                                         }
320                                         if (tailbufsize < (size_t)taillen + BUFSIZ) {
321                                                 tailbufsize = taillen + BUFSIZ;
322                                                 tailbuf = xrealloc(tailbuf, tailbufsize);
323                                         }
324                                 }
325                                 buf = tailbuf + taillen;
326                         }
327                 } /* while (tail_read() > 0) */
328                 if (!G.from_top) {
329                         xwrite(STDOUT_FILENO, tailbuf, taillen);
330                 }
331         } while (++i < nfiles);
332         prev_fd = fds[i-1];
333
334         tailbuf = xrealloc(tailbuf, BUFSIZ);
335
336         fmt = NULL;
337
338         if (FOLLOW) while (1) {
339                 sleep(sleep_period);
340
341                 i = 0;
342                 do {
343                         int nread;
344                         const char *filename = argv[i];
345                         int fd = fds[i];
346
347                         if (FOLLOW_RETRY) {
348                                 struct stat sbuf, fsbuf;
349
350                                 if (fd < 0
351                                  || fstat(fd, &fsbuf) < 0
352                                  || stat(filename, &sbuf) < 0
353                                  || fsbuf.st_dev != sbuf.st_dev
354                                  || fsbuf.st_ino != sbuf.st_ino
355                                 ) {
356                                         int new_fd;
357
358                                         if (fd >= 0)
359                                                 close(fd);
360                                         new_fd = open(filename, O_RDONLY);
361                                         if (new_fd >= 0) {
362                                                 bb_error_msg("%s has %s; following end of new file",
363                                                         filename, (fd < 0) ? "appeared" : "been replaced"
364                                                 );
365                                         } else if (fd >= 0) {
366                                                 bb_perror_msg("%s has become inaccessible", filename);
367                                         }
368                                         fds[i] = fd = new_fd;
369                                 }
370                         }
371                         if (ENABLE_FEATURE_FANCY_TAIL && fd < 0)
372                                 continue;
373                         if (nfiles > header_threshhold) {
374                                 fmt = header_fmt_str;
375                         }
376                         for (;;) {
377                                 /* tail -f keeps following files even if they are truncated */
378                                 struct stat sbuf;
379                                 /* /proc files report zero st_size, don't lseek them */
380                                 if (fstat(fd, &sbuf) == 0 && sbuf.st_size > 0) {
381                                         off_t current = lseek(fd, 0, SEEK_CUR);
382                                         if (sbuf.st_size < current)
383                                                 xlseek(fd, 0, SEEK_SET);
384                                 }
385
386                                 nread = tail_read(fd, tailbuf, BUFSIZ);
387                                 if (nread <= 0)
388                                         break;
389                                 if (fmt && (fd != prev_fd)) {
390                                         tail_xprint_header(fmt, filename);
391                                         fmt = NULL;
392                                         prev_fd = fd;
393                                 }
394                                 xwrite(STDOUT_FILENO, tailbuf, nread);
395                         }
396                 } while (++i < nfiles);
397         } /* while (1) */
398
399         if (ENABLE_FEATURE_CLEAN_UP) {
400                 free(fds);
401                 free(tailbuf);
402         }
403         return G.exitcode;
404 }