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)
36 static const struct suffix_mult tail_suffixes[] = {
45 static void tail_xprint_header(const char *fmt, const char *filename)
47 /* If we get an output error, there is really no sense in continuing. */
48 if (dprintf(STDOUT_FILENO, fmt, filename) < 0) {
49 bb_perror_nomsg_and_die();
53 /* len should probably be size_t */
54 static void tail_xbb_full_write(const char *buf, size_t len)
56 /* If we get a write error, there is really no sense in continuing. */
57 if (full_write(STDOUT_FILENO, buf, len) < 0)
58 bb_perror_nomsg_and_die();
61 static ssize_t tail_read(int fd, char *buf, size_t count)
67 end = current = lseek(fd, 0, SEEK_CUR);
68 if (!fstat(fd, &sbuf))
70 lseek(fd, end < current ? 0 : current, SEEK_SET);
71 if ((r = safe_read(fd, buf, count)) < 0) {
72 bb_perror_msg(bb_msg_read_error);
73 status = EXIT_FAILURE;
79 static const char tail_opts[] =
81 #if ENABLE_FEATURE_FANCY_TAIL
86 static const char header_fmt[] = "\n==> %s <==\n";
88 int tail_main(int argc, char **argv)
91 unsigned int sleep_period = 1;
94 int header_threshhold = 1;
102 int *fds, nfiles, nread, nwrite, seen, i, opt;
106 #if !ENABLE_DEBUG_YANK_SUSv2 || ENABLE_FEATURE_FANCY_TAIL
107 /* Allow legacy syntax of an initial numeric option without -n. */
108 if (argc >=2 && ((argv[1][0] == '+') || ((argv[1][0] == '-')
109 /* && (isdigit)(argv[1][1]) */
110 && (((unsigned int)(argv[1][1] - '0')) <= 9))))
118 while ((opt = getopt(argc, argv, tail_opts)) > 0) {
127 #if !ENABLE_DEBUG_YANK_SUSv2 || ENABLE_FEATURE_FANCY_TAIL
130 count = bb_xgetlarg10_sfx(optarg, tail_suffixes);
131 /* Note: Leading whitespace is an error trapped above. */
132 if (*optarg == '+') {
141 #if ENABLE_FEATURE_FANCY_TAIL
143 header_threshhold = INT_MAX;
146 sleep_period =bb_xgetularg10_bnd(optarg, 0, UINT_MAX);
149 header_threshhold = 0;
157 /* open all the files */
158 fds = (int *)xmalloc(sizeof(int) * (argc - optind + 1));
163 if ((argc -= optind) == 0) {
166 if (!fstat(STDIN_FILENO, &statbuf) && S_ISFIFO(statbuf.st_mode)) {
170 *argv = (char *) bb_msg_standard_input;
175 if ((argv[i][0] == '-') && !argv[i][1]) {
177 fds[nfiles] = STDIN_FILENO;
178 } else if ((fds[nfiles] = open(argv[i], O_RDONLY)) < 0) {
179 bb_perror_msg("%s", argv[i]);
180 status = EXIT_FAILURE;
183 argv[nfiles] = argv[i];
185 } while (++i < argc);
188 bb_error_msg_and_die("no files");
191 tailbufsize = BUFSIZ;
194 if (from_top < count_bytes) { /* Each is 0 or 1, so true iff 0 < 1. */
195 /* Hence, !from_top && count_bytes */
196 if (tailbufsize < count) {
197 tailbufsize = count + BUFSIZ;
201 buf = tailbuf = xmalloc(tailbufsize);
203 fmt = header_fmt + 1; /* Skip header leading newline on first output. */
206 /* Be careful. It would be possible to optimize the count-bytes
207 * case if the file is seekable. If you do though, remember that
208 * starting file position may not be the beginning of the file.
209 * Beware of backing up too far. See example in wc.c.
211 if ((!(count|from_top)) && (lseek(fds[i], 0, SEEK_END) >= 0)) {
215 if (nfiles > header_threshhold) {
216 tail_xprint_header(fmt, argv[i]);
225 while ((nread = tail_read(fds[i], buf, tailbufsize-taillen)) > 0) {
230 nwrite -= (count - seen);
236 if ((*s++ == '\n') && (++seen == count)) {
242 tail_xbb_full_write(buf + nread - nwrite, nwrite);
246 if (taillen > count) {
247 memmove(tailbuf, tailbuf + taillen - count, count);
256 if (buf[k] == '\n') {
261 if (newline + nbuf < count) {
267 if (buf[nread-1] != '\n') {
271 k = newline + nbuf + extra - count;
280 taillen += nread - (s - tailbuf);
281 memmove(tailbuf, s, taillen);
282 newline = count - extra;
284 if (tailbufsize < taillen + BUFSIZ) {
285 tailbufsize = taillen + BUFSIZ;
286 tailbuf = xrealloc(tailbuf, tailbufsize);
289 buf = tailbuf + taillen;
294 tail_xbb_full_write(tailbuf, taillen);
298 } while (++i < nfiles);
300 buf = xrealloc(tailbuf, BUFSIZ);
308 if (nfiles > header_threshhold) {
311 while ((nread = tail_read(fds[i], buf, sizeof(buf))) > 0) {
313 tail_xprint_header(fmt, argv[i]);
316 tail_xbb_full_write(buf, nread);
318 } while (++i < nfiles);