1 /* vi: set sw=4 ts=4: */
3 * Mini tail implementation for busybox
5 * Copyright (C) 2001 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
10 /* 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 */
14 /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
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)
29 static const struct suffix_mult tail_suffixes[] = {
38 static void tail_xbb_full_write(const char *buf, size_t len)
40 /* If we get a write error, there is really no sense in continuing. */
41 if (full_write(STDOUT_FILENO, buf, len) < 0)
42 bb_perror_nomsg_and_die();
45 static void tail_xprint_header(const char *fmt, const char *filename)
48 if (dprintf(STDOUT_FILENO, fmt, filename) < 0) {
49 bb_perror_nomsg_and_die();
52 int hdr_len = strlen(fmt) + strlen(filename);
53 char *hdr = xzalloc(hdr_len);
54 sprintf(hdr, filename, filename);
55 tail_xbb_full_write(hdr, hdr_len);
59 static ssize_t tail_read(int fd, char *buf, size_t count)
65 end = current = lseek(fd, 0, SEEK_CUR);
66 if (!fstat(fd, &sbuf))
68 lseek(fd, end < current ? 0 : current, SEEK_SET);
69 if ((r = safe_read(fd, buf, count)) < 0) {
70 bb_perror_msg(bb_msg_read_error);
71 status = EXIT_FAILURE;
77 static const char tail_opts[] =
79 #if ENABLE_FEATURE_FANCY_TAIL
84 static const char header_fmt[] = "\n==> %s <==\n";
86 int tail_main(int argc, char **argv)
89 unsigned sleep_period = 1;
92 int header_threshhold = 1;
100 int *fds, nfiles, nread, nwrite, seen, i, opt;
104 #if !ENABLE_DEBUG_YANK_SUSv2 || ENABLE_FEATURE_FANCY_TAIL
105 /* Allow legacy syntax of an initial numeric option without -n. */
106 if (argc >= 2 && (argv[1][0] == '+' || argv[1][0] == '-')
107 && isdigit(argv[1][1])
115 while ((opt = getopt(argc, argv, tail_opts)) > 0) {
124 #if !ENABLE_DEBUG_YANK_SUSv2 || ENABLE_FEATURE_FANCY_TAIL
128 if (*optarg == '+') {
132 count = xatol_sfx(optarg, tail_suffixes);
133 /* Note: Leading whitespace is an error trapped above. */
138 #if ENABLE_FEATURE_FANCY_TAIL
140 header_threshhold = INT_MAX;
143 sleep_period = xatou(optarg);
146 header_threshhold = 0;
156 /* open all the files */
157 fds = xmalloc(sizeof(int) * (argc + 1));
162 if (!fstat(STDIN_FILENO, &statbuf) && S_ISFIFO(statbuf.st_mode)) {
165 *argv = (char *) bb_msg_standard_input;
170 if (LONE_DASH(argv[i])) {
172 fds[nfiles] = STDIN_FILENO;
173 } else if ((fds[nfiles] = open(argv[i], O_RDONLY)) < 0) {
174 bb_perror_msg("%s", argv[i]);
175 status = EXIT_FAILURE;
178 argv[nfiles] = argv[i];
180 } while (++i < argc);
183 bb_error_msg_and_die("no files");
186 tailbufsize = BUFSIZ;
189 if (from_top < count_bytes) { /* Each is 0 or 1, so true iff 0 < 1. */
190 /* Hence, !from_top && count_bytes */
191 if (tailbufsize < count) {
192 tailbufsize = count + BUFSIZ;
196 buf = tailbuf = xmalloc(tailbufsize);
198 fmt = header_fmt + 1; /* Skip header leading newline on first output. */
201 /* Be careful. It would be possible to optimize the count-bytes
202 * case if the file is seekable. If you do though, remember that
203 * starting file position may not be the beginning of the file.
204 * Beware of backing up too far. See example in wc.c.
206 if ((!(count|from_top)) && (lseek(fds[i], 0, SEEK_END) >= 0)) {
210 if (nfiles > header_threshhold) {
211 tail_xprint_header(fmt, argv[i]);
220 while ((nread = tail_read(fds[i], buf, tailbufsize-taillen)) > 0) {
225 nwrite -= (count - seen);
231 if ((*s++ == '\n') && (++seen == count)) {
237 tail_xbb_full_write(buf + nread - nwrite, nwrite);
241 if (taillen > count) {
242 memmove(tailbuf, tailbuf + taillen - count, count);
251 if (buf[k] == '\n') {
256 if (newline + nbuf < count) {
262 if (buf[nread-1] != '\n') {
266 k = newline + nbuf + extra - count;
275 taillen += nread - (s - tailbuf);
276 memmove(tailbuf, s, taillen);
277 newline = count - extra;
279 if (tailbufsize < taillen + BUFSIZ) {
280 tailbufsize = taillen + BUFSIZ;
281 tailbuf = xrealloc(tailbuf, tailbufsize);
284 buf = tailbuf + taillen;
289 tail_xbb_full_write(tailbuf, taillen);
293 } while (++i < nfiles);
295 buf = xrealloc(tailbuf, BUFSIZ);
303 if (nfiles > header_threshhold) {
306 while ((nread = tail_read(fds[i], buf, sizeof(buf))) > 0) {
308 tail_xprint_header(fmt, argv[i]);
311 tail_xbb_full_write(buf, nread);
313 } while (++i < nfiles);