1 /* tail -- output the last part of file(s)
2 Copyright (C) 89, 90, 91, 95, 1996 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 Original version by Paul Rubin <phr@ocf.berkeley.edu>.
19 Extensions by David MacKenzie <djm@gnu.ai.mit.edu>.
20 tail -f for multiple files by Ian Lance Taylor <ian@airs.com>.
22 Rewrote the option parser, removed locales support,
23 and generally busyboxed, Erik Andersen <andersen@lineo.com>
32 #include <sys/types.h>
33 #include <sys/types.h>
40 /* Disable assertions. Some systems have broken assert macros. */
44 static void error(int i, int errnum, char* fmt, ...)
48 va_start(arguments, fmt);
49 vfprintf(stderr, fmt, arguments);
50 fprintf(stderr, "\n%s\n", strerror( errnum));
56 #define XWRITE(fd, buffer, n_bytes) \
60 assert ((n_bytes) >= 0); \
61 if (n_bytes > 0 && fwrite ((buffer), 1, (n_bytes), stdout) == 0) \
62 error (EXIT_FAILURE, errno, "write error"); \
66 /* Number of items to tail. */
67 #define DEFAULT_N_LINES 10
69 /* Size of atomic reads. */
71 #define BUFSIZ (512 * 8)
74 /* If nonzero, interpret the numeric argument as the number of lines.
75 Otherwise, interpret it as the number of bytes. */
76 static int count_lines;
78 /* If nonzero, read from the end of one file until killed. */
81 /* If nonzero, read from the end of multiple files until killed. */
82 static int forever_multiple;
84 /* Array of file descriptors if forever_multiple is 1. */
85 static int *file_descs;
87 /* Array of file sizes if forever_multiple is 1. */
88 static off_t *file_sizes;
90 /* If nonzero, count from start of file instead of end. */
91 static int from_start;
93 /* If nonzero, print filename headers. */
94 static int print_headers;
96 /* When to print the filename banners. */
99 multiple_files, always, never
104 /* The name this program was run with. */
107 /* Nonzero if we have ever read standard input. */
108 static int have_read_stdin;
111 static const char tail_usage[] =
112 "tail [OPTION]... [FILE]...\n\
114 Print last 10 lines of each FILE to standard output.\n\
115 With more than one FILE, precede each with a header giving the file name.\n\
116 With no FILE, or when FILE is -, read standard input.\n\
118 -c=N[kbm] output the last N bytes\n\
119 -f output appended data as the file grows\n\
120 -n=N output the last N lines, instead of last 10\n\
121 -q never output headers giving file names\n\
122 -v always output headers giving file names\n\
123 --help display this help and exit\n\
125 If the first character of N (bytes or lines) is a `+', output begins with \n\
126 the Nth item from the start of each file, otherwise, print the last N items\n\
127 in the file. N bytes may be suffixed by k (x1024), b (x512), or m (1024^2).\n\n";
130 write_header (const char *filename, const char *comment)
132 static int first_file = 1;
134 printf ("%s==> %s%s%s <==\n", (first_file ? "" : "\n"), filename,
135 (comment ? ": " : ""),
136 (comment ? comment : ""));
140 /* Print the last N_LINES lines from the end of file FD.
141 Go backward through the file, reading `BUFSIZ' bytes at a time (except
142 probably the first), until we hit the start of the file or have
143 read NUMBER newlines.
144 POS starts out as the length of the file (the offset of the last
145 byte of the file + 1).
146 Return 0 if successful, 1 if an error occurred. */
149 file_lines (const char *filename, int fd, long int n_lines, off_t pos)
153 int i; /* Index into `buffer' for scanning. */
158 /* Set `bytes_read' to the size of the last, probably partial, buffer;
159 0 < `bytes_read' <= `BUFSIZ'. */
160 bytes_read = pos % BUFSIZ;
163 /* Make `pos' a multiple of `BUFSIZ' (0 if the file is short), so that all
164 reads will be on block boundaries, which might increase efficiency. */
166 lseek (fd, pos, SEEK_SET);
167 bytes_read = fullRead (fd, buffer, bytes_read);
168 if (bytes_read == -1)
170 error (0, errno, "%s", filename);
174 /* Count the incomplete line on files that don't end with a newline. */
175 if (bytes_read && buffer[bytes_read - 1] != '\n')
180 /* Scan backward, counting the newlines in this bufferfull. */
181 for (i = bytes_read - 1; i >= 0; i--)
183 /* Have we counted the requested number of newlines yet? */
184 if (buffer[i] == '\n' && n_lines-- == 0)
186 /* If this newline wasn't the last character in the buffer,
187 print the text after it. */
188 if (i != bytes_read - 1)
189 XWRITE (STDOUT_FILENO, &buffer[i + 1], bytes_read - (i + 1));
193 /* Not enough newlines in that bufferfull. */
196 /* Not enough lines in the file; print the entire file. */
197 lseek (fd, (off_t) 0, SEEK_SET);
201 lseek (fd, pos, SEEK_SET);
203 while ((bytes_read = fullRead (fd, buffer, BUFSIZ)) > 0);
204 if (bytes_read == -1)
206 error (0, errno, "%s", filename);
212 /* Print the last N_LINES lines from the end of the standard input,
213 open for reading as pipe FD.
214 Buffer the text as a linked list of LBUFFERs, adding them as needed.
215 Return 0 if successful, 1 if an error occured. */
218 pipe_lines (const char *filename, int fd, long int n_lines)
224 struct linebuffer *next;
226 typedef struct linebuffer LBUFFER;
227 LBUFFER *first, *last, *tmp;
228 int i; /* Index into buffers. */
229 int total_lines = 0; /* Total number of newlines in all buffers. */
232 first = last = (LBUFFER *) xmalloc (sizeof (LBUFFER));
233 first->nbytes = first->nlines = 0;
235 tmp = (LBUFFER *) xmalloc (sizeof (LBUFFER));
237 /* Input is always read into a fresh buffer. */
238 while ((tmp->nbytes = fullRead (fd, tmp->buffer, BUFSIZ)) > 0)
243 /* Count the number of newlines just read. */
244 for (i = 0; i < tmp->nbytes; i++)
245 if (tmp->buffer[i] == '\n')
247 total_lines += tmp->nlines;
249 /* If there is enough room in the last buffer read, just append the new
250 one to it. This is because when reading from a pipe, `nbytes' can
251 often be very small. */
252 if (tmp->nbytes + last->nbytes < BUFSIZ)
254 memcpy (&last->buffer[last->nbytes], tmp->buffer, tmp->nbytes);
255 last->nbytes += tmp->nbytes;
256 last->nlines += tmp->nlines;
260 /* If there's not enough room, link the new buffer onto the end of
261 the list, then either free up the oldest buffer for the next
262 read if that would leave enough lines, or else malloc a new one.
263 Some compaction mechanism is possible but probably not
265 last = last->next = tmp;
266 if (total_lines - first->nlines > n_lines)
269 total_lines -= first->nlines;
273 tmp = (LBUFFER *) xmalloc (sizeof (LBUFFER));
276 if (tmp->nbytes == -1)
278 error (0, errno, "%s", filename);
286 /* This prevents a core dump when the pipe contains no newlines. */
290 /* Count the incomplete line on files that don't end with a newline. */
291 if (last->buffer[last->nbytes - 1] != '\n')
297 /* Run through the list, printing lines. First, skip over unneeded
299 for (tmp = first; total_lines - tmp->nlines > n_lines; tmp = tmp->next)
300 total_lines -= tmp->nlines;
302 /* Find the correct beginning, then print the rest of the file. */
303 if (total_lines > n_lines)
307 /* Skip `total_lines' - `n_lines' newlines. We made sure that
308 `total_lines' - `n_lines' <= `tmp->nlines'. */
310 for (i = total_lines - n_lines; i; --i)
311 while (*cp++ != '\n')
313 i = cp - tmp->buffer;
317 XWRITE (STDOUT_FILENO, &tmp->buffer[i], tmp->nbytes - i);
319 for (tmp = tmp->next; tmp; tmp = tmp->next)
320 XWRITE (STDOUT_FILENO, tmp->buffer, tmp->nbytes);
326 free ((char *) first);
332 /* Print the last N_BYTES characters from the end of pipe FD.
333 This is a stripped down version of pipe_lines.
334 Return 0 if successful, 1 if an error occurred. */
337 pipe_bytes (const char *filename, int fd, off_t n_bytes)
343 struct charbuffer *next;
345 typedef struct charbuffer CBUFFER;
346 CBUFFER *first, *last, *tmp;
347 int i; /* Index into buffers. */
348 int total_bytes = 0; /* Total characters in all buffers. */
351 first = last = (CBUFFER *) xmalloc (sizeof (CBUFFER));
354 tmp = (CBUFFER *) xmalloc (sizeof (CBUFFER));
356 /* Input is always read into a fresh buffer. */
357 while ((tmp->nbytes = fullRead (fd, tmp->buffer, BUFSIZ)) > 0)
361 total_bytes += tmp->nbytes;
362 /* If there is enough room in the last buffer read, just append the new
363 one to it. This is because when reading from a pipe, `nbytes' can
364 often be very small. */
365 if (tmp->nbytes + last->nbytes < BUFSIZ)
367 memcpy (&last->buffer[last->nbytes], tmp->buffer, tmp->nbytes);
368 last->nbytes += tmp->nbytes;
372 /* If there's not enough room, link the new buffer onto the end of
373 the list, then either free up the oldest buffer for the next
374 read if that would leave enough characters, or else malloc a new
375 one. Some compaction mechanism is possible but probably not
377 last = last->next = tmp;
378 if (total_bytes - first->nbytes > n_bytes)
381 total_bytes -= first->nbytes;
386 tmp = (CBUFFER *) xmalloc (sizeof (CBUFFER));
390 if (tmp->nbytes == -1)
392 error (0, errno, "%s", filename);
400 /* Run through the list, printing characters. First, skip over unneeded
402 for (tmp = first; total_bytes - tmp->nbytes > n_bytes; tmp = tmp->next)
403 total_bytes -= tmp->nbytes;
405 /* Find the correct beginning, then print the rest of the file.
406 We made sure that `total_bytes' - `n_bytes' <= `tmp->nbytes'. */
407 if (total_bytes > n_bytes)
408 i = total_bytes - n_bytes;
411 XWRITE (STDOUT_FILENO, &tmp->buffer[i], tmp->nbytes - i);
413 for (tmp = tmp->next; tmp; tmp = tmp->next)
414 XWRITE (STDOUT_FILENO, tmp->buffer, tmp->nbytes);
420 free ((char *) first);
426 /* Skip N_BYTES characters from the start of pipe FD, and print
427 any extra characters that were read beyond that.
428 Return 1 on error, 0 if ok. */
431 start_bytes (const char *filename, int fd, off_t n_bytes)
436 while (n_bytes > 0 && (bytes_read = fullRead (fd, buffer, BUFSIZ)) > 0)
437 n_bytes -= bytes_read;
438 if (bytes_read == -1)
440 error (0, errno, "%s", filename);
443 else if (n_bytes < 0)
444 XWRITE (STDOUT_FILENO, &buffer[bytes_read + n_bytes], -n_bytes);
448 /* Skip N_LINES lines at the start of file or pipe FD, and print
449 any extra characters that were read beyond that.
450 Return 1 on error, 0 if ok. */
453 start_lines (const char *filename, int fd, long int n_lines)
457 int bytes_to_skip = 0;
459 while (n_lines && (bytes_read = fullRead (fd, buffer, BUFSIZ)) > 0)
462 while (bytes_to_skip < bytes_read)
463 if (buffer[bytes_to_skip++] == '\n' && --n_lines == 0)
466 if (bytes_read == -1)
468 error (0, errno, "%s", filename);
471 else if (bytes_to_skip < bytes_read)
473 XWRITE (STDOUT_FILENO, &buffer[bytes_to_skip],
474 bytes_read - bytes_to_skip);
479 /* Display file FILENAME from the current position in FD to the end.
480 If `forever' is nonzero, keep reading from the end of the file
481 until killed. Return the number of bytes read from the file. */
484 dump_remainder (const char *filename, int fd)
492 while ((bytes_read = fullRead (fd, buffer, BUFSIZ)) > 0)
494 XWRITE (STDOUT_FILENO, buffer, bytes_read);
497 if (bytes_read == -1)
498 error (EXIT_FAILURE, errno, "%s", filename);
507 if (forever_multiple)
514 /* Tail NFILES (>1) files forever until killed. The file names are in
515 NAMES. The open file descriptors are in `file_descs', and the size
516 at which we stopped tailing them is in `file_sizes'. We loop over
517 each of them, doing an fstat to see if they have changed size. If
518 none of them have changed size in one iteration, we sleep for a
519 second and try again. We do this until the user interrupts us. */
522 tail_forever (char **names, int nfiles)
534 for (i = 0; i < nfiles; i++)
538 if (file_descs[i] < 0)
540 if (fstat (file_descs[i], &stats) < 0)
542 error (0, errno, "%s", names[i]);
546 if (stats.st_size == file_sizes[i])
549 /* This file has changed size. Print out what we can, and
550 then keep looping. */
554 if (stats.st_size < file_sizes[i])
556 write_header (names[i], "file truncated");
558 lseek (file_descs[i], stats.st_size, SEEK_SET);
559 file_sizes[i] = stats.st_size;
566 write_header (names[i], NULL);
569 file_sizes[i] += dump_remainder (names[i], file_descs[i]);
572 /* If none of the files changed size, sleep. */
578 /* Output the last N_BYTES bytes of file FILENAME open for reading in FD.
579 Return 0 if successful, 1 if an error occurred. */
582 tail_bytes (const char *filename, int fd, off_t n_bytes)
586 /* FIXME: resolve this like in dd.c. */
587 /* Use fstat instead of checking for errno == ESPIPE because
588 lseek doesn't work on some special files but doesn't return an
590 if (fstat (fd, &stats))
592 error (0, errno, "%s", filename);
598 if (S_ISREG (stats.st_mode))
599 lseek (fd, n_bytes, SEEK_CUR);
600 else if (start_bytes (filename, fd, n_bytes))
602 dump_remainder (filename, fd);
606 if (S_ISREG (stats.st_mode))
608 off_t current_pos, end_pos;
609 size_t bytes_remaining;
611 if ((current_pos = lseek (fd, (off_t) 0, SEEK_CUR)) != -1
612 && (end_pos = lseek (fd, (off_t) 0, SEEK_END)) != -1)
615 /* Be careful here. The current position may actually be
616 beyond the end of the file. */
617 bytes_remaining = (diff = end_pos - current_pos) < 0 ? 0 : diff;
621 error (0, errno, "%s", filename);
625 if (bytes_remaining <= n_bytes)
627 /* From the current position to end of file, there are no
628 more bytes than have been requested. So reposition the
629 file pointer to the incoming current position and print
630 everything after that. */
631 lseek (fd, current_pos, SEEK_SET);
635 /* There are more bytes remaining than were requested.
637 lseek (fd, -n_bytes, SEEK_END);
639 dump_remainder (filename, fd);
642 return pipe_bytes (filename, fd, n_bytes);
647 /* Output the last N_LINES lines of file FILENAME open for reading in FD.
648 Return 0 if successful, 1 if an error occurred. */
651 tail_lines (const char *filename, int fd, long int n_lines)
656 if (fstat (fd, &stats))
658 error (0, errno, "%s", filename);
664 if (start_lines (filename, fd, n_lines))
666 dump_remainder (filename, fd);
670 /* Use file_lines only if FD refers to a regular file with
671 its file pointer positioned at beginning of file. */
672 /* FIXME: adding the lseek conjunct is a kludge.
673 Once there's a reasonable test suite, fix the true culprit:
674 file_lines. file_lines shouldn't presume that the input
675 file pointer is initially positioned to beginning of file. */
676 if (S_ISREG (stats.st_mode)
677 && lseek (fd, (off_t) 0, SEEK_CUR) == (off_t) 0)
679 length = lseek (fd, (off_t) 0, SEEK_END);
680 if (length != 0 && file_lines (filename, fd, n_lines, length))
682 dump_remainder (filename, fd);
685 return pipe_lines (filename, fd, n_lines);
690 /* Display the last N_UNITS units of file FILENAME, open for reading
692 Return 0 if successful, 1 if an error occurred. */
695 tail (const char *filename, int fd, off_t n_units)
698 return tail_lines (filename, fd, (long) n_units);
700 return tail_bytes (filename, fd, n_units);
703 /* Display the last N_UNITS units of file FILENAME.
704 "-" for FILENAME means the standard input.
705 FILENUM is this file's index in the list of files the user gave.
706 Return 0 if successful, 1 if an error occurred. */
709 tail_file (const char *filename, off_t n_units, int filenum)
714 if (!strcmp (filename, "-"))
717 filename = "standard input";
719 write_header (filename, NULL);
720 errors = tail (filename, 0, n_units);
721 if (forever_multiple)
723 if (fstat (0, &stats) < 0)
725 error (0, errno, "standard input");
728 else if (!S_ISREG (stats.st_mode))
731 "standard input: cannot follow end of non-regular file");
735 file_descs[filenum] = -1;
738 file_descs[filenum] = 0;
739 file_sizes[filenum] = stats.st_size;
745 /* Not standard input. */
746 fd = open (filename, O_RDONLY);
749 if (forever_multiple)
750 file_descs[filenum] = -1;
751 error (0, errno, "%s", filename);
757 write_header (filename, NULL);
758 errors = tail (filename, fd, n_units);
759 if (forever_multiple)
761 if (fstat (fd, &stats) < 0)
763 error (0, errno, "%s", filename);
766 else if (!S_ISREG (stats.st_mode))
768 error (0, 0, "%s: cannot follow end of non-regular file",
775 file_descs[filenum] = -1;
779 file_descs[filenum] = fd;
780 file_sizes[filenum] = stats.st_size;
787 error (0, errno, "%s", filename);
798 tail_main (int argc, char **argv)
801 enum header_mode header_mode = multiple_files;
803 /* If from_start, the number of items to skip before printing; otherwise,
804 the number of items at the end of the file to print. Initially, -1
805 means the value has not been set. */
810 program_name = argv[0];
813 forever = forever_multiple = from_start = print_headers = 0;
815 /* Parse any options */
816 //fprintf(stderr, "argc=%d, argv=%s\n", argc, *argv);
817 while (--argc > 0 && ( **(++argv) == '-' || **argv == '+' )) {
822 while (stopit == 0 && *(++(*argv))) {
830 n_units = getNum(*(++argv));
844 n_units = atol(*(++argv));
853 header_mode = always;
864 n_units = DEFAULT_N_LINES;
866 /* To start printing with item N_UNITS from the start of the file, skip
867 N_UNITS - 1 items. `tail +0' is actually meaningless, but for Unix
868 compatibility it's treated the same as `tail +1'. */
878 if (n_files > 1 && forever)
880 forever_multiple = 1;
882 file_descs = (int *) xmalloc (n_files * sizeof (int));
883 file_sizes = (off_t *) xmalloc (n_files * sizeof (off_t));
886 if (header_mode == always
887 || (header_mode == multiple_files && n_files > 1))
892 exit_status |= tail_file ("-", n_units, 0);
897 for (i = 0; i < n_files; i++)
898 exit_status |= tail_file (file[i], n_units, i);
900 if (forever_multiple)
901 tail_forever (file, n_files);
904 if (have_read_stdin && close (0) < 0)
905 error (EXIT_FAILURE, errno, "-");
906 if (fclose (stdout) == EOF)
907 error (EXIT_FAILURE, errno, "write error");
908 exit (exit_status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);