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 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /* BB_AUDIT SUSv3 compliant (need fancy for -c) */
24 /* BB_AUDIT GNU compatible -c, -q, and -v options in 'fancy' configuration. */
25 /* http://www.opengroup.org/onlinepubs/007904975/utilities/tail.html */
27 /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
29 * Pretty much rewritten to fix numerous bugs and reduce realloc() calls.
30 * Bugs fixed (although I may have forgotten one or two... it was pretty bad)
31 * 1) mixing printf/write without fflush()ing stdout
32 * 2) no check that any open files are present
33 * 3) optstring had -q taking an arg
34 * 4) no error checking on write in some cases, and a warning even then
35 * 5) q and s interaction bug
36 * 6) no check for lseek error
37 * 7) lseek attempted when count==0 even if arg was +0 (from top)
49 static const struct suffix_mult tail_suffixes[] = {
58 = EXIT_SUCCESS /* If it is 0 (paranoid check), let bss initialize it. */
62 static void tail_xprint_header(const char *fmt, const char *filename)
64 /* If we get an output error, there is really no sense in continuing. */
65 if (dprintf(STDOUT_FILENO, fmt, filename) < 0) {
66 bb_perror_nomsg_and_die();
70 /* len should probably be size_t */
71 static void tail_xbb_full_write(const char *buf, size_t len)
73 /* If we get a write error, there is really no sense in continuing. */
74 if (bb_full_write(STDOUT_FILENO, buf, len) < 0) {
75 bb_perror_nomsg_and_die();
79 static ssize_t tail_read(int fd, char *buf, size_t count)
86 end = current = lseek (fd, 0, SEEK_CUR);
87 if (!fstat(fd, &sbuf)){
91 lseek(fd, 0, SEEK_SET);
93 lseek(fd, current, SEEK_SET);
95 if ((r = safe_read(fd, buf, count)) < 0) {
96 bb_perror_msg("read");
97 status = EXIT_FAILURE;
103 static const char tail_opts[] =
105 #ifdef CONFIG_FEATURE_FANCY_TAIL
110 static const char header_fmt[] = "\n==> %s <==\n";
112 int tail_main(int argc, char **argv)
115 unsigned int sleep_period = 1;
118 int header_threshhold = 1;
126 int *fds, nfiles, nread, nwrite, seen, i, opt;
130 /* Allow legacy syntax of an initial numeric option without -n. */
131 if (argc >=2 && ((argv[1][0] == '+') || ((argv[1][0] == '-')
132 /* && (isdigit)(argv[1][1]) */
133 && (((unsigned int)(argv[1][1] - '0')) <= 9))))
140 while ((opt = getopt(argc, argv, tail_opts)) > 0) {
150 count = bb_xgetlarg10_sfx(optarg, tail_suffixes);
151 /* Note: Leading whitespace is an error trapped above. */
152 if (*optarg == '+') {
161 #ifdef CONFIG_FEATURE_FANCY_TAIL
163 header_threshhold = INT_MAX;
166 sleep_period =bb_xgetularg10_bnd(optarg, 0, UINT_MAX);
169 header_threshhold = 0;
177 /* open all the files */
178 fds = (int *)xmalloc(sizeof(int) * (argc - optind + 1));
183 if ((argc -= optind) == 0) {
186 if (!fstat(STDIN_FILENO, &statbuf) && S_ISFIFO(statbuf.st_mode)) {
190 *argv = (char *) bb_msg_standard_input;
195 if ((argv[i][0] == '-') && !argv[i][1]) {
197 fds[nfiles] = STDIN_FILENO;
198 } else if ((fds[nfiles] = open(argv[i], O_RDONLY)) < 0) {
199 bb_perror_msg("%s", argv[i]);
200 status = EXIT_FAILURE;
203 argv[nfiles] = argv[i];
205 } while (++i < argc);
208 bb_error_msg_and_die("no files");
211 tailbufsize = BUFSIZ;
214 if (from_top < count_bytes) { /* Each is 0 or 1, so true iff 0 < 1. */
215 /* Hence, !from_top && count_bytes */
216 if (tailbufsize < count) {
217 tailbufsize = count + BUFSIZ;
221 buf = tailbuf = xmalloc(tailbufsize);
223 fmt = header_fmt + 1; /* Skip header leading newline on first output. */
226 /* Be careful. It would be possible to optimize the count-bytes
227 * case if the file is seekable. If you do though, remember that
228 * starting file position may not be the beginning of the file.
229 * Beware of backing up too far. See example in wc.c.
231 if ((!(count|from_top)) && (lseek(fds[i], 0, SEEK_END) >= 0)) {
235 if (nfiles > header_threshhold) {
236 tail_xprint_header(fmt, argv[i]);
245 while ((nread = tail_read(fds[i], buf, tailbufsize-taillen)) > 0) {
250 nwrite -= (count - seen);
256 if ((*s++ == '\n') && (++seen == count)) {
262 tail_xbb_full_write(buf + nread - nwrite, nwrite);
266 if (taillen > count) {
267 memmove(tailbuf, tailbuf + taillen - count, count);
276 if (buf[k] == '\n') {
281 if (newline + nbuf < count) {
287 if (buf[nread-1] != '\n') {
291 k = newline + nbuf + extra - count;
300 taillen += nread - (s - tailbuf);
301 memmove(tailbuf, s, taillen);
302 newline = count - extra;
304 if (tailbufsize < taillen + BUFSIZ) {
305 tailbufsize = taillen + BUFSIZ;
306 tailbuf = xrealloc(tailbuf, tailbufsize);
309 buf = tailbuf + taillen;
314 tail_xbb_full_write(tailbuf, taillen);
318 } while (++i < nfiles);
320 buf = xrealloc(tailbuf, BUFSIZ);
328 if (nfiles > header_threshhold) {
331 while ((nread = tail_read(fds[i], buf, sizeof(buf))) > 0) {
333 tail_xprint_header(fmt, argv[i]);
336 tail_xbb_full_write(buf, nread);
338 } while (++i < nfiles);