1 /* tail -- output the last part of file(s)
2 Copyright (C) 1989, 1990, 1991, 1995 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 /* Can display any amount of data, unlike the Unix version, which uses
19 a fixed size buffer and therefore can only deliver a limited number
23 -b Tail by N 512-byte blocks.
24 -c, --bytes=N[bkm] Tail by N bytes
25 [or 512-byte blocks, kilobytes, or megabytes].
26 -f, --follow Loop forever trying to read more characters at the
27 end of the file, on the assumption that the file
28 is growing. Ignored if reading from a pipe.
29 -n, --lines=N Tail by N lines.
30 -q, --quiet, --silent Never print filename headers.
31 -v, --verbose Always print filename headers.
33 If a number (N) starts with a `+', begin printing with the Nth item
34 from the start of each file, instead of from the end.
36 Reads from standard input if no files are given or when a filename of
38 By default, filename headers are printed only more than one file
40 By default, prints the last 10 lines (tail -n 10).
42 Original version by Paul Rubin <phr@ocf.berkeley.edu>.
43 Extensions by David MacKenzie <djm@gnu.ai.mit.edu>.
44 tail -f for multiple files by Ian Lance Taylor <ian@airs.com>. */
51 #include <sys/types.h>
58 /* Disable assertions. Some systems have broken assert macros. */
61 #define XWRITE(fd, buffer, n_bytes) \
65 assert ((n_bytes) >= 0); \
66 if (n_bytes > 0 && fwrite ((buffer), 1, (n_bytes), stdout) == 0) \
67 error (1, errno, _("write error")); \
71 /* Number of items to tail. */
72 #define DEFAULT_N_LINES 10
74 /* Size of atomic reads. */
76 #define BUFSIZ (512 * 8)
79 /* If nonzero, interpret the numeric argument as the number of lines.
80 Otherwise, interpret it as the number of bytes. */
81 static int count_lines
;
83 /* If nonzero, read from the end of one file until killed. */
86 /* If nonzero, read from the end of multiple files until killed. */
87 static int forever_multiple
;
89 /* Array of file descriptors if forever_multiple is 1. */
90 static int *file_descs
;
92 /* Array of file sizes if forever_multiple is 1. */
93 static off_t
*file_sizes
;
95 /* If nonzero, count from start of file instead of end. */
96 static int from_start
;
98 /* If nonzero, print filename headers. */
99 static int print_headers
;
101 /* When to print the filename banners. */
104 multiple_files
, always
, never
110 /* The name this program was run with. */
113 /* Nonzero if we have ever read standard input. */
114 static int have_read_stdin
;
116 /* If nonzero, display usage information and exit. */
117 static int show_help
;
119 /* If nonzero, print the version on standard output then exit. */
120 static int show_version
;
122 static struct option
const long_options
[] =
124 {"bytes", required_argument
, NULL
, 'c'},
125 {"follow", no_argument
, NULL
, 'f'},
126 {"lines", required_argument
, NULL
, 'n'},
127 {"quiet", no_argument
, NULL
, 'q'},
128 {"silent", no_argument
, NULL
, 'q'},
129 {"verbose", no_argument
, NULL
, 'v'},
130 {"help", no_argument
, &show_help
, 1},
131 {"version", no_argument
, &show_version
, 1},
139 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
144 Usage: %s [OPTION]... [FILE]...\n\
148 Print last 10 lines of each FILE to standard output.\n\
149 With more than one FILE, precede each with a header giving the file name.\n\
150 With no FILE, or when FILE is -, read standard input.\n\
152 -c, --bytes=N output the last N bytes\n\
153 -f, --follow output appended data as the file grows\n\
154 -n, --lines=N output the last N lines, instead of last 10\n\
155 -q, --quiet, --silent never output headers giving file names\n\
156 -v, --verbose always output headers giving file names\n\
157 --help display this help and exit\n\
158 --version output version information and exit\n\
160 If the first character of N (the number of bytes or lines) is a `+',\n\
161 print beginning with the Nth item from the start of each file, otherwise,\n\
162 print the last N items in the file. N may have a multiplier suffix:\n\
163 b for 512, k for 1024, m for 1048576 (1 Meg). A first OPTION of -VALUE\n\
164 or +VALUE is treated like -n VALUE or -n +VALUE unless VALUE has one of\n\
165 the [bkm] suffix multipliers, in which case it is treated like -c VALUE\n\
173 write_header (const char *filename
, const char *comment
)
175 static int first_file
= 1;
177 printf ("%s==> %s%s%s <==\n", (first_file
? "" : "\n"), filename
,
178 (comment
? ": " : ""),
179 (comment
? comment
: ""));
183 /* Print the last N_LINES lines from the end of file FD.
184 Go backward through the file, reading `BUFSIZ' bytes at a time (except
185 probably the first), until we hit the start of the file or have
186 read NUMBER newlines.
187 POS starts out as the length of the file (the offset of the last
188 byte of the file + 1).
189 Return 0 if successful, 1 if an error occurred. */
192 file_lines (const char *filename
, int fd
, long int n_lines
, off_t pos
)
196 int i
; /* Index into `buffer' for scanning. */
201 /* Set `bytes_read' to the size of the last, probably partial, buffer;
202 0 < `bytes_read' <= `BUFSIZ'. */
203 bytes_read
= pos
% BUFSIZ
;
206 /* Make `pos' a multiple of `BUFSIZ' (0 if the file is short), so that all
207 reads will be on block boundaries, which might increase efficiency. */
209 lseek (fd
, pos
, SEEK_SET
);
210 bytes_read
= safe_read (fd
, buffer
, bytes_read
);
211 if (bytes_read
== -1)
213 error (0, errno
, "%s", filename
);
217 /* Count the incomplete line on files that don't end with a newline. */
218 if (bytes_read
&& buffer
[bytes_read
- 1] != '\n')
223 /* Scan backward, counting the newlines in this bufferfull. */
224 for (i
= bytes_read
- 1; i
>= 0; i
--)
226 /* Have we counted the requested number of newlines yet? */
227 if (buffer
[i
] == '\n' && n_lines
-- == 0)
229 /* If this newline wasn't the last character in the buffer,
230 print the text after it. */
231 if (i
!= bytes_read
- 1)
232 XWRITE (STDOUT_FILENO
, &buffer
[i
+ 1], bytes_read
- (i
+ 1));
236 /* Not enough newlines in that bufferfull. */
239 /* Not enough lines in the file; print the entire file. */
240 lseek (fd
, (off_t
) 0, SEEK_SET
);
244 lseek (fd
, pos
, SEEK_SET
);
246 while ((bytes_read
= safe_read (fd
, buffer
, BUFSIZ
)) > 0);
247 if (bytes_read
== -1)
249 error (0, errno
, "%s", filename
);
255 /* Print the last N_LINES lines from the end of the standard input,
256 open for reading as pipe FD.
257 Buffer the text as a linked list of LBUFFERs, adding them as needed.
258 Return 0 if successful, 1 if an error occured. */
261 pipe_lines (const char *filename
, int fd
, long int n_lines
)
267 struct linebuffer
*next
;
269 typedef struct linebuffer LBUFFER
;
270 LBUFFER
*first
, *last
, *tmp
;
271 int i
; /* Index into buffers. */
272 int total_lines
= 0; /* Total number of newlines in all buffers. */
275 first
= last
= (LBUFFER
*) xmalloc (sizeof (LBUFFER
));
276 first
->nbytes
= first
->nlines
= 0;
278 tmp
= (LBUFFER
*) xmalloc (sizeof (LBUFFER
));
280 /* Input is always read into a fresh buffer. */
281 while ((tmp
->nbytes
= safe_read (fd
, tmp
->buffer
, BUFSIZ
)) > 0)
286 /* Count the number of newlines just read. */
287 for (i
= 0; i
< tmp
->nbytes
; i
++)
288 if (tmp
->buffer
[i
] == '\n')
290 total_lines
+= tmp
->nlines
;
292 /* If there is enough room in the last buffer read, just append the new
293 one to it. This is because when reading from a pipe, `nbytes' can
294 often be very small. */
295 if (tmp
->nbytes
+ last
->nbytes
< BUFSIZ
)
297 memcpy (&last
->buffer
[last
->nbytes
], tmp
->buffer
, tmp
->nbytes
);
298 last
->nbytes
+= tmp
->nbytes
;
299 last
->nlines
+= tmp
->nlines
;
303 /* If there's not enough room, link the new buffer onto the end of
304 the list, then either free up the oldest buffer for the next
305 read if that would leave enough lines, or else malloc a new one.
306 Some compaction mechanism is possible but probably not
308 last
= last
->next
= tmp
;
309 if (total_lines
- first
->nlines
> n_lines
)
312 total_lines
-= first
->nlines
;
316 tmp
= (LBUFFER
*) xmalloc (sizeof (LBUFFER
));
319 if (tmp
->nbytes
== -1)
321 error (0, errno
, "%s", filename
);
329 /* This prevents a core dump when the pipe contains no newlines. */
333 /* Count the incomplete line on files that don't end with a newline. */
334 if (last
->buffer
[last
->nbytes
- 1] != '\n')
340 /* Run through the list, printing lines. First, skip over unneeded
342 for (tmp
= first
; total_lines
- tmp
->nlines
> n_lines
; tmp
= tmp
->next
)
343 total_lines
-= tmp
->nlines
;
345 /* Find the correct beginning, then print the rest of the file. */
346 if (total_lines
> n_lines
)
350 /* Skip `total_lines' - `n_lines' newlines. We made sure that
351 `total_lines' - `n_lines' <= `tmp->nlines'. */
353 for (i
= total_lines
- n_lines
; i
; --i
)
354 while (*cp
++ != '\n')
356 i
= cp
- tmp
->buffer
;
360 XWRITE (STDOUT_FILENO
, &tmp
->buffer
[i
], tmp
->nbytes
- i
);
362 for (tmp
= tmp
->next
; tmp
; tmp
= tmp
->next
)
363 XWRITE (STDOUT_FILENO
, tmp
->buffer
, tmp
->nbytes
);
369 free ((char *) first
);
375 /* Print the last N_BYTES characters from the end of pipe FD.
376 This is a stripped down version of pipe_lines.
377 Return 0 if successful, 1 if an error occurred. */
380 pipe_bytes (const char *filename
, int fd
, off_t n_bytes
)
386 struct charbuffer
*next
;
388 typedef struct charbuffer CBUFFER
;
389 CBUFFER
*first
, *last
, *tmp
;
390 int i
; /* Index into buffers. */
391 int total_bytes
= 0; /* Total characters in all buffers. */
394 first
= last
= (CBUFFER
*) xmalloc (sizeof (CBUFFER
));
397 tmp
= (CBUFFER
*) xmalloc (sizeof (CBUFFER
));
399 /* Input is always read into a fresh buffer. */
400 while ((tmp
->nbytes
= safe_read (fd
, tmp
->buffer
, BUFSIZ
)) > 0)
404 total_bytes
+= tmp
->nbytes
;
405 /* If there is enough room in the last buffer read, just append the new
406 one to it. This is because when reading from a pipe, `nbytes' can
407 often be very small. */
408 if (tmp
->nbytes
+ last
->nbytes
< BUFSIZ
)
410 memcpy (&last
->buffer
[last
->nbytes
], tmp
->buffer
, tmp
->nbytes
);
411 last
->nbytes
+= tmp
->nbytes
;
415 /* If there's not enough room, link the new buffer onto the end of
416 the list, then either free up the oldest buffer for the next
417 read if that would leave enough characters, or else malloc a new
418 one. Some compaction mechanism is possible but probably not
420 last
= last
->next
= tmp
;
421 if (total_bytes
- first
->nbytes
> n_bytes
)
424 total_bytes
-= first
->nbytes
;
429 tmp
= (CBUFFER
*) xmalloc (sizeof (CBUFFER
));
433 if (tmp
->nbytes
== -1)
435 error (0, errno
, "%s", filename
);
443 /* Run through the list, printing characters. First, skip over unneeded
445 for (tmp
= first
; total_bytes
- tmp
->nbytes
> n_bytes
; tmp
= tmp
->next
)
446 total_bytes
-= tmp
->nbytes
;
448 /* Find the correct beginning, then print the rest of the file.
449 We made sure that `total_bytes' - `n_bytes' <= `tmp->nbytes'. */
450 if (total_bytes
> n_bytes
)
451 i
= total_bytes
- n_bytes
;
454 XWRITE (STDOUT_FILENO
, &tmp
->buffer
[i
], tmp
->nbytes
- i
);
456 for (tmp
= tmp
->next
; tmp
; tmp
= tmp
->next
)
457 XWRITE (STDOUT_FILENO
, tmp
->buffer
, tmp
->nbytes
);
463 free ((char *) first
);
469 /* Skip N_BYTES characters from the start of pipe FD, and print
470 any extra characters that were read beyond that.
471 Return 1 on error, 0 if ok. */
474 start_bytes (const char *filename
, int fd
, off_t n_bytes
)
479 while (n_bytes
> 0 && (bytes_read
= safe_read (fd
, buffer
, BUFSIZ
)) > 0)
480 n_bytes
-= bytes_read
;
481 if (bytes_read
== -1)
483 error (0, errno
, "%s", filename
);
486 else if (n_bytes
< 0)
487 XWRITE (STDOUT_FILENO
, &buffer
[bytes_read
+ n_bytes
], -n_bytes
);
491 /* Skip N_LINES lines at the start of file or pipe FD, and print
492 any extra characters that were read beyond that.
493 Return 1 on error, 0 if ok. */
496 start_lines (const char *filename
, int fd
, long int n_lines
)
500 int bytes_to_skip
= 0;
502 while (n_lines
&& (bytes_read
= safe_read (fd
, buffer
, BUFSIZ
)) > 0)
505 while (bytes_to_skip
< bytes_read
)
506 if (buffer
[bytes_to_skip
++] == '\n' && --n_lines
== 0)
509 if (bytes_read
== -1)
511 error (0, errno
, "%s", filename
);
514 else if (bytes_to_skip
< bytes_read
)
516 XWRITE (STDOUT_FILENO
, &buffer
[bytes_to_skip
],
517 bytes_read
- bytes_to_skip
);
522 /* Display file FILENAME from the current position in FD to the end.
523 If `forever' is nonzero, keep reading from the end of the file
524 until killed. Return the number of bytes read from the file. */
527 dump_remainder (const char *filename
, int fd
)
535 while ((bytes_read
= safe_read (fd
, buffer
, BUFSIZ
)) > 0)
537 XWRITE (STDOUT_FILENO
, buffer
, bytes_read
);
540 if (bytes_read
== -1)
541 error (1, errno
, "%s", filename
);
551 /* Tail NFILES (>1) files forever until killed. The file names are in
552 NAMES. The open file descriptors are in `file_descs', and the size
553 at which we stopped tailing them is in `file_sizes'. We loop over
554 each of them, doing an fstat to see if they have changed size. If
555 none of them have changed size in one iteration, we sleep for a
556 second and try again. We do this until the user interrupts us. */
559 tail_forever (char **names
, int nfiles
)
571 for (i
= 0; i
< nfiles
; i
++)
575 if (file_descs
[i
] < 0)
577 if (fstat (file_descs
[i
], &stats
) < 0)
579 error (0, errno
, "%s", names
[i
]);
583 if (stats
.st_size
== file_sizes
[i
])
586 /* This file has changed size. Print out what we can, and
587 then keep looping. */
591 if (stats
.st_size
< file_sizes
[i
])
593 write_header (names
[i
], _("file truncated"));
595 lseek (file_descs
[i
], stats
.st_size
, SEEK_SET
);
596 file_sizes
[i
] = stats
.st_size
;
603 write_header (names
[i
], NULL
);
606 file_sizes
[i
] += dump_remainder (names
[i
], file_descs
[i
]);
609 /* If none of the files changed size, sleep. */
615 /* Output the last N_BYTES bytes of file FILENAME open for reading in FD.
616 Return 0 if successful, 1 if an error occurred. */
619 tail_bytes (const char *filename
, int fd
, off_t n_bytes
)
623 /* FIXME: resolve this like in dd.c. */
624 /* Use fstat instead of checking for errno == ESPIPE because
625 lseek doesn't work on some special files but doesn't return an
627 if (fstat (fd
, &stats
))
629 error (0, errno
, "%s", filename
);
635 if (S_ISREG (stats
.st_mode
))
636 lseek (fd
, n_bytes
, SEEK_CUR
);
637 else if (start_bytes (filename
, fd
, n_bytes
))
639 dump_remainder (filename
, fd
);
643 if (S_ISREG (stats
.st_mode
))
645 off_t current_pos
, end_pos
;
646 size_t bytes_remaining
;
648 if ((current_pos
= lseek (fd
, (off_t
) 0, SEEK_CUR
)) != -1
649 && (end_pos
= lseek (fd
, (off_t
) 0, SEEK_END
)) != -1)
652 /* Be careful here. The current position may actually be
653 beyond the end of the file. */
654 bytes_remaining
= (diff
= end_pos
- current_pos
) < 0 ? 0 : diff
;
658 error (0, errno
, "%s", filename
);
662 if (bytes_remaining
<= n_bytes
)
664 /* From the current position to end of file, there are no
665 more bytes than have been requested. So reposition the
666 file pointer to the incoming current position and print
667 everything after that. */
668 lseek (fd
, current_pos
, SEEK_SET
);
672 /* There are more bytes remaining than were requested.
674 lseek (fd
, -n_bytes
, SEEK_END
);
676 dump_remainder (filename
, fd
);
679 return pipe_bytes (filename
, fd
, n_bytes
);
684 /* Output the last N_LINES lines of file FILENAME open for reading in FD.
685 Return 0 if successful, 1 if an error occurred. */
688 tail_lines (const char *filename
, int fd
, long int n_lines
)
693 if (fstat (fd
, &stats
))
695 error (0, errno
, "%s", filename
);
701 if (start_lines (filename
, fd
, n_lines
))
703 dump_remainder (filename
, fd
);
707 /* Use file_lines only if FD refers to a regular file with
708 its file pointer positioned at beginning of file. */
709 /* FIXME: adding the lseek conjunct is a kludge.
710 Once there's a reasonable test suite, fix the true culprit:
711 file_lines. file_lines shouldn't presume that the input
712 file pointer is initially positioned to beginning of file. */
713 if (S_ISREG (stats
.st_mode
)
714 && lseek (fd
, (off_t
) 0, SEEK_CUR
) == (off_t
) 0)
716 length
= lseek (fd
, (off_t
) 0, SEEK_END
);
717 if (length
!= 0 && file_lines (filename
, fd
, n_lines
, length
))
719 dump_remainder (filename
, fd
);
722 return pipe_lines (filename
, fd
, n_lines
);
727 /* Display the last N_UNITS units of file FILENAME, open for reading
729 Return 0 if successful, 1 if an error occurred. */
732 tail (const char *filename
, int fd
, off_t n_units
)
735 return tail_lines (filename
, fd
, (long) n_units
);
737 return tail_bytes (filename
, fd
, n_units
);
740 /* Display the last N_UNITS units of file FILENAME.
741 "-" for FILENAME means the standard input.
742 FILENUM is this file's index in the list of files the user gave.
743 Return 0 if successful, 1 if an error occurred. */
746 tail_file (const char *filename
, off_t n_units
, int filenum
)
751 if (!strcmp (filename
, "-"))
754 filename
= _("standard input");
756 write_header (filename
, NULL
);
757 errors
= tail (filename
, 0, n_units
);
758 if (forever_multiple
)
760 if (fstat (0, &stats
) < 0)
762 error (0, errno
, _("standard input"));
765 else if (!S_ISREG (stats
.st_mode
))
768 _("standard input: cannot follow end of non-regular file"));
772 file_descs
[filenum
] = -1;
775 file_descs
[filenum
] = 0;
776 file_sizes
[filenum
] = stats
.st_size
;
782 /* Not standard input. */
783 fd
= open (filename
, O_RDONLY
);
786 if (forever_multiple
)
787 file_descs
[filenum
] = -1;
788 error (0, errno
, "%s", filename
);
794 write_header (filename
, NULL
);
795 errors
= tail (filename
, fd
, n_units
);
796 if (forever_multiple
)
798 if (fstat (fd
, &stats
) < 0)
800 error (0, errno
, "%s", filename
);
803 else if (!S_ISREG (stats
.st_mode
))
805 error (0, 0, _("%s: cannot follow end of non-regular file"),
812 file_descs
[filenum
] = -1;
816 file_descs
[filenum
] = fd
;
817 file_sizes
[filenum
] = stats
.st_size
;
824 error (0, errno
, "%s", filename
);
835 main (int argc
, char **argv
)
837 enum header_mode header_mode
= multiple_files
;
839 /* If from_start, the number of items to skip before printing; otherwise,
840 the number of items at the end of the file to print. Initially, -1
841 means the value has not been set. */
844 int c
; /* Option character. */
845 int fileind
; /* Index in ARGV of first file name. */
847 program_name
= argv
[0];
850 forever
= forever_multiple
= from_start
= print_headers
= 0;
853 && ((argv
[1][0] == '-' && ISDIGIT (argv
[1][1]))
854 || (argv
[1][0] == '+' && (ISDIGIT (argv
[1][1]) || argv
[1][1] == 0))))
856 /* Old option syntax: a dash or plus, one or more digits (zero digits
857 are acceptable with a plus), and one or more option letters. */
858 if (argv
[1][0] == '+')
860 if (argv
[1][1] != '\0')
865 s_err
= xstrtol (++argv
[1], &p
, 0, &tmp_long
, "bkm");
867 if (s_err
== LONGINT_OVERFLOW
)
869 STRTOL_FATAL_ERROR (argv
[1], _("argument"), s_err
);
871 /* Parse any appended option letters. */
877 /* Interpret N_UNITS as # of bytes. */
894 header_mode
= always
;
898 error (0, 0, _("unrecognized option `-%c'"), *p
);
904 /* Make the options we just parsed invisible to getopt. */
910 while ((c
= getopt_long (argc
, argv
, "c:n:fqv", long_options
, (int *) 0))
932 s_err
= xstrtol (optarg
, NULL
, 0, &tmp_long
, "bkm");
934 tmp_long
= -tmp_long
;
936 if (s_err
!= LONGINT_OK
)
938 STRTOL_FATAL_ERROR (optarg
, (c
== 'n'
939 ? _("number of lines")
940 : _("number of bytes")), s_err
);
953 header_mode
= always
;
963 printf ("tail - %s\n", version_string
);
971 n_units
= DEFAULT_N_LINES
;
973 /* To start printing with item N_UNITS from the start of the file, skip
974 N_UNITS - 1 items. `tail +0' is actually meaningless, but for Unix
975 compatibility it's treated the same as `tail +1'. */
984 if (optind
< argc
- 1 && forever
)
986 forever_multiple
= 1;
988 file_descs
= (int *) xmalloc ((argc
- optind
) * sizeof (int));
989 file_sizes
= (off_t
*) xmalloc ((argc
- optind
) * sizeof (off_t
));
992 if (header_mode
== always
993 || (header_mode
== multiple_files
&& optind
< argc
- 1))
997 exit_status
|= tail_file ("-", n_units
, 0);
999 for (; optind
< argc
; ++optind
)
1000 exit_status
|= tail_file (argv
[optind
], n_units
, optind
- fileind
);
1002 if (forever_multiple
)
1003 tail_forever (argv
+ fileind
, argc
- fileind
);
1005 if (have_read_stdin
&& close (0) < 0)
1006 error (1, errno
, "-");
1007 if (fclose (stdout
) == EOF
)
1008 error (1, errno
, _("write error"));