1 /* vi: set sw=4 ts=4: */
4 /* This file contains _two_ implementations of tail. One is
5 * a bit more full featured, but costs 6k. The other (i.e. the
6 * SIMPLE_TAIL one) is less capable, but is good enough for about
7 * 99% of the things folks want to use tail for, and only costs 2k.
11 #ifdef BB_FEATURE_SIMPLE_TAIL
13 /* tail -- output the last part of file(s)
14 Copyright (C) 89, 90, 91, 95, 1996 Free Software Foundation, Inc.
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
18 the Free Software Foundation; either version 2, or (at your option)
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 Original version by Paul Rubin <phr@ocf.berkeley.edu>.
31 Extensions by David MacKenzie <djm@gnu.ai.mit.edu>.
32 tail -f for multiple files by Ian Lance Taylor <ian@airs.com>.
34 Rewrote the option parser, removed locales support,
35 and generally busyboxed, Erik Andersen <andersen@lineo.com>
37 Removed superfluous options and associated code ("-c", "-n", "-q").
38 Removed "tail -f" support for multiple files.
39 Both changes by Friedrich Vedder <fwv@myrtle.lahn.de>.
46 #include <sys/types.h>
52 #define XWRITE(fd, buffer, n_bytes) \
54 if (n_bytes > 0 && fwrite ((buffer), 1, (n_bytes), stdout) == 0) \
55 errorMsg("write error"); \
58 /* Number of items to tail. */
59 #define DEFAULT_N_LINES 10
61 /* Size of atomic reads. */
63 #define BUFSIZ (512 * 8)
66 /* If nonzero, read from the end of one file until killed. */
69 /* If nonzero, print filename headers. */
70 static int print_headers;
72 const char tail_usage[] =
73 "tail [OPTION] [FILE]...\n\n"
74 "Print last 10 lines of each FILE to standard output.\n"
75 "With more than one FILE, precede each with a header giving the\n"
76 "file name. With no FILE, or when FILE is -, read standard input.\n\n"
78 "\t-n NUM\t\tPrint last NUM lines instead of first 10\n"
80 "\t-f\t\tOutput data as the file grows. This version\n"
81 "\t\t\tof 'tail -f' supports only one file at a time.\n";
84 static void write_header(const char *filename)
86 static int first_file = 1;
88 printf("%s==> %s <==\n", (first_file ? "" : "\n"), filename);
92 /* Print the last N_LINES lines from the end of file FD.
93 Go backward through the file, reading `BUFSIZ' bytes at a time (except
94 probably the first), until we hit the start of the file or have
96 POS starts out as the length of the file (the offset of the last
97 byte of the file + 1).
98 Return 0 if successful, 1 if an error occurred. */
101 file_lines(const char *filename, int fd, long int n_lines, off_t pos)
105 int i; /* Index into `buffer' for scanning. */
110 /* Set `bytes_read' to the size of the last, probably partial, buffer;
111 0 < `bytes_read' <= `BUFSIZ'. */
112 bytes_read = pos % BUFSIZ;
115 /* Make `pos' a multiple of `BUFSIZ' (0 if the file is short), so that all
116 reads will be on block boundaries, which might increase efficiency. */
118 lseek(fd, pos, SEEK_SET);
119 bytes_read = fullRead(fd, buffer, bytes_read);
120 if (bytes_read == -1)
121 errorMsg("read error");
123 /* Count the incomplete line on files that don't end with a newline. */
124 if (bytes_read && buffer[bytes_read - 1] != '\n')
128 /* Scan backward, counting the newlines in this bufferfull. */
129 for (i = bytes_read - 1; i >= 0; i--) {
130 /* Have we counted the requested number of newlines yet? */
131 if (buffer[i] == '\n' && n_lines-- == 0) {
132 /* If this newline wasn't the last character in the buffer,
133 print the text after it. */
134 if (i != bytes_read - 1)
135 XWRITE(STDOUT_FILENO, &buffer[i + 1],
136 bytes_read - (i + 1));
140 /* Not enough newlines in that bufferfull. */
142 /* Not enough lines in the file; print the entire file. */
143 lseek(fd, (off_t) 0, SEEK_SET);
147 lseek(fd, pos, SEEK_SET);
149 while ((bytes_read = fullRead(fd, buffer, BUFSIZ)) > 0);
150 if (bytes_read == -1)
151 errorMsg("read error");
156 /* Print the last N_LINES lines from the end of the standard input,
157 open for reading as pipe FD.
158 Buffer the text as a linked list of LBUFFERs, adding them as needed.
159 Return 0 if successful, 1 if an error occured. */
161 static int pipe_lines(const char *filename, int fd, long int n_lines)
166 struct linebuffer *next;
168 typedef struct linebuffer LBUFFER;
169 LBUFFER *first, *last, *tmp;
170 int i; /* Index into buffers. */
171 int total_lines = 0; /* Total number of newlines in all buffers. */
174 first = last = (LBUFFER *) xmalloc(sizeof(LBUFFER));
175 first->nbytes = first->nlines = 0;
177 tmp = (LBUFFER *) xmalloc(sizeof(LBUFFER));
179 /* Input is always read into a fresh buffer. */
180 while ((tmp->nbytes = fullRead(fd, tmp->buffer, BUFSIZ)) > 0) {
184 /* Count the number of newlines just read. */
185 for (i = 0; i < tmp->nbytes; i++)
186 if (tmp->buffer[i] == '\n')
188 total_lines += tmp->nlines;
190 /* If there is enough room in the last buffer read, just append the new
191 one to it. This is because when reading from a pipe, `nbytes' can
192 often be very small. */
193 if (tmp->nbytes + last->nbytes < BUFSIZ) {
194 memcpy(&last->buffer[last->nbytes], tmp->buffer, tmp->nbytes);
195 last->nbytes += tmp->nbytes;
196 last->nlines += tmp->nlines;
198 /* If there's not enough room, link the new buffer onto the end of
199 the list, then either free up the oldest buffer for the next
200 read if that would leave enough lines, or else malloc a new one.
201 Some compaction mechanism is possible but probably not
203 last = last->next = tmp;
204 if (total_lines - first->nlines > n_lines) {
206 total_lines -= first->nlines;
209 tmp = (LBUFFER *) xmalloc(sizeof(LBUFFER));
212 if (tmp->nbytes == -1)
213 errorMsg("read error");
217 /* This prevents a core dump when the pipe contains no newlines. */
221 /* Count the incomplete line on files that don't end with a newline. */
222 if (last->buffer[last->nbytes - 1] != '\n') {
227 /* Run through the list, printing lines. First, skip over unneeded
229 for (tmp = first; total_lines - tmp->nlines > n_lines; tmp = tmp->next)
230 total_lines -= tmp->nlines;
232 /* Find the correct beginning, then print the rest of the file. */
233 if (total_lines > n_lines) {
236 /* Skip `total_lines' - `n_lines' newlines. We made sure that
237 `total_lines' - `n_lines' <= `tmp->nlines'. */
239 for (i = total_lines - n_lines; i; --i)
240 while (*cp++ != '\n')
242 i = cp - tmp->buffer;
245 XWRITE(STDOUT_FILENO, &tmp->buffer[i], tmp->nbytes - i);
247 for (tmp = tmp->next; tmp; tmp = tmp->next)
248 XWRITE(STDOUT_FILENO, tmp->buffer, tmp->nbytes);
253 free((char *) first);
259 /* Display file FILENAME from the current position in FD to the end.
260 If `forever' is nonzero, keep reading from the end of the file
261 until killed. Return the number of bytes read from the file. */
263 static long dump_remainder(const char *filename, int fd)
271 while ((bytes_read = fullRead(fd, buffer, BUFSIZ)) > 0) {
272 XWRITE(STDOUT_FILENO, buffer, bytes_read);
275 if (bytes_read == -1)
276 errorMsg("read error");
286 /* Output the last N_LINES lines of file FILENAME open for reading in FD.
287 Return 0 if successful, 1 if an error occurred. */
289 static int tail_lines(const char *filename, int fd, long int n_lines)
295 write_header(filename);
297 if (fstat(fd, &stats))
298 errorMsg("fstat error");
300 /* Use file_lines only if FD refers to a regular file with
301 its file pointer positioned at beginning of file. */
302 /* FIXME: adding the lseek conjunct is a kludge.
303 Once there's a reasonable test suite, fix the true culprit:
304 file_lines. file_lines shouldn't presume that the input
305 file pointer is initially positioned to beginning of file. */
306 if (S_ISREG(stats.st_mode)
307 && lseek(fd, (off_t) 0, SEEK_CUR) == (off_t) 0) {
308 length = lseek(fd, (off_t) 0, SEEK_END);
309 if (length != 0 && file_lines(filename, fd, n_lines, length))
311 dump_remainder(filename, fd);
313 return pipe_lines(filename, fd, n_lines);
318 /* Display the last N_UNITS lines of file FILENAME.
319 "-" for FILENAME means the standard input.
320 Return 0 if successful, 1 if an error occurred. */
322 static int tail_file(const char *filename, off_t n_units)
326 if (!strcmp(filename, "-")) {
327 filename = "standard input";
328 errors = tail_lines(filename, 0, (long) n_units);
330 /* Not standard input. */
331 fd = open(filename, O_RDONLY);
333 fatalError("open error");
335 errors = tail_lines(filename, fd, (long) n_units);
342 extern int tail_main(int argc, char **argv)
345 int n_units = DEFAULT_N_LINES;
349 forever = print_headers = 0;
352 for (i = 1; i < argc; i++) {
353 if (argv[i][0] == '-') {
362 n_tmp = atoi(argv[i]);
371 if ((n_units = atoi(&argv[i][1])) < 1) {
372 fprintf(stderr, "tail: invalid option -- %c\n", opt);
384 "tail: option -f is invalid with multiple files\n");
391 exit_status |= tail_file("-", n_units);
393 for (; i < argc; i++)
394 exit_status |= tail_file(argv[i], n_units);
397 exit(exit_status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
402 // Here follows the code for the full featured tail code
405 /* tail -- output the last part of file(s)
406 Copyright (C) 89, 90, 91, 95, 1996 Free Software Foundation, Inc.
408 This program is free software; you can redistribute it and/or modify
409 it under the terms of the GNU General Public License as published by
410 the Free Software Foundation; either version 2, or (at your option)
413 This program is distributed in the hope that it will be useful,
414 but WITHOUT ANY WARRANTY; without even the implied warranty of
415 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
416 GNU General Public License for more details.
418 You should have received a copy of the GNU General Public License
419 along with this program; if not, write to the Free Software
420 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
422 Original version by Paul Rubin <phr@ocf.berkeley.edu>.
423 Extensions by David MacKenzie <djm@gnu.ai.mit.edu>.
424 tail -f for multiple files by Ian Lance Taylor <ian@airs.com>.
426 Rewrote the option parser, removed locales support,
427 and generally busyboxed, Erik Andersen <andersen@lineo.com>
431 #include "internal.h"
437 #include <sys/types.h>
438 #include <sys/types.h>
439 #include <sys/stat.h>
445 /* Disable assertions. Some systems have broken assert macros. */
449 static void detailed_error(int i, int errnum, char *fmt, ...)
450 __attribute__ ((format (printf, 3, 4)));
451 static void detailed_error(int i, int errnum, char *fmt, ...)
455 va_start(arguments, fmt);
456 vfprintf(stderr, fmt, arguments);
457 fprintf(stderr, "\n%s\n", strerror(errnum));
463 #define XWRITE(fd, buffer, n_bytes) \
466 assert ((fd) == 1); \
467 assert ((n_bytes) >= 0); \
468 if (n_bytes > 0 && fwrite ((buffer), 1, (n_bytes), stdout) == 0) \
469 detailed_error (EXIT_FAILURE, errno, "write error"); \
473 /* Number of items to tail. */
474 #define DEFAULT_N_LINES 10
476 /* Size of atomic reads. */
478 #define BUFSIZ (512 * 8)
481 /* If nonzero, interpret the numeric argument as the number of lines.
482 Otherwise, interpret it as the number of bytes. */
483 static int count_lines;
485 /* If nonzero, read from the end of one file until killed. */
488 /* If nonzero, read from the end of multiple files until killed. */
489 static int forever_multiple;
491 /* Array of file descriptors if forever_multiple is 1. */
492 static int *file_descs;
494 /* Array of file sizes if forever_multiple is 1. */
495 static off_t *file_sizes;
497 /* If nonzero, count from start of file instead of end. */
498 static int from_start;
500 /* If nonzero, print filename headers. */
501 static int print_headers;
503 /* When to print the filename banners. */
505 multiple_files, always, never
508 /* The name this program was run with. */
511 /* Nonzero if we have ever read standard input. */
512 static int have_read_stdin;
515 static const char tail_usage[] = "tail [OPTION]... [FILE]...\n\
517 Print last 10 lines of each FILE to standard output.\n\
518 With more than one FILE, precede each with a header giving the file name.\n\
519 With no FILE, or when FILE is -, read standard input.\n\
521 -c=N[kbm] output the last N bytes\n\
522 -f output appended data as the file grows\n\
523 -n=N output the last N lines, instead of last 10\n\
524 -q never output headers giving file names\n\
525 -v always output headers giving file names\n\
526 --help display this help and exit\n\
528 If the first character of N (bytes or lines) is a `+', output begins with \n\
529 the Nth item from the start of each file, otherwise, print the last N items\n\
530 in the file. N bytes may be suffixed by k (x1024), b (x512), or m (1024^2).\n\n";
532 static void write_header(const char *filename, const char *comment)
534 static int first_file = 1;
536 printf("%s==> %s%s%s <==\n", (first_file ? "" : "\n"), filename,
537 (comment ? ": " : ""), (comment ? comment : ""));
541 /* Print the last N_LINES lines from the end of file FD.
542 Go backward through the file, reading `BUFSIZ' bytes at a time (except
543 probably the first), until we hit the start of the file or have
544 read NUMBER newlines.
545 POS starts out as the length of the file (the offset of the last
546 byte of the file + 1).
547 Return 0 if successful, 1 if an error occurred. */
550 file_lines(const char *filename, int fd, long int n_lines, off_t pos)
554 int i; /* Index into `buffer' for scanning. */
559 /* Set `bytes_read' to the size of the last, probably partial, buffer;
560 0 < `bytes_read' <= `BUFSIZ'. */
561 bytes_read = pos % BUFSIZ;
564 /* Make `pos' a multiple of `BUFSIZ' (0 if the file is short), so that all
565 reads will be on block boundaries, which might increase efficiency. */
567 lseek(fd, pos, SEEK_SET);
568 bytes_read = fullRead(fd, buffer, bytes_read);
569 if (bytes_read == -1) {
570 detailed_error(0, errno, "%s", filename);
574 /* Count the incomplete line on files that don't end with a newline. */
575 if (bytes_read && buffer[bytes_read - 1] != '\n')
579 /* Scan backward, counting the newlines in this bufferfull. */
580 for (i = bytes_read - 1; i >= 0; i--) {
581 /* Have we counted the requested number of newlines yet? */
582 if (buffer[i] == '\n' && n_lines-- == 0) {
583 /* If this newline wasn't the last character in the buffer,
584 print the text after it. */
585 if (i != bytes_read - 1)
586 XWRITE(STDOUT_FILENO, &buffer[i + 1],
587 bytes_read - (i + 1));
591 /* Not enough newlines in that bufferfull. */
593 /* Not enough lines in the file; print the entire file. */
594 lseek(fd, (off_t) 0, SEEK_SET);
598 lseek(fd, pos, SEEK_SET);
600 while ((bytes_read = fullRead(fd, buffer, BUFSIZ)) > 0);
601 if (bytes_read == -1) {
602 detailed_error(0, errno, "%s", filename);
608 /* Print the last N_LINES lines from the end of the standard input,
609 open for reading as pipe FD.
610 Buffer the text as a linked list of LBUFFERs, adding them as needed.
611 Return 0 if successful, 1 if an error occured. */
613 static int pipe_lines(const char *filename, int fd, long int n_lines)
618 struct linebuffer *next;
620 typedef struct linebuffer LBUFFER;
621 LBUFFER *first, *last, *tmp;
622 int i; /* Index into buffers. */
623 int total_lines = 0; /* Total number of newlines in all buffers. */
626 first = last = (LBUFFER *) xmalloc(sizeof(LBUFFER));
627 first->nbytes = first->nlines = 0;
629 tmp = (LBUFFER *) xmalloc(sizeof(LBUFFER));
631 /* Input is always read into a fresh buffer. */
632 while ((tmp->nbytes = fullRead(fd, tmp->buffer, BUFSIZ)) > 0) {
636 /* Count the number of newlines just read. */
637 for (i = 0; i < tmp->nbytes; i++)
638 if (tmp->buffer[i] == '\n')
640 total_lines += tmp->nlines;
642 /* If there is enough room in the last buffer read, just append the new
643 one to it. This is because when reading from a pipe, `nbytes' can
644 often be very small. */
645 if (tmp->nbytes + last->nbytes < BUFSIZ) {
646 memcpy(&last->buffer[last->nbytes], tmp->buffer, tmp->nbytes);
647 last->nbytes += tmp->nbytes;
648 last->nlines += tmp->nlines;
650 /* If there's not enough room, link the new buffer onto the end of
651 the list, then either free up the oldest buffer for the next
652 read if that would leave enough lines, or else malloc a new one.
653 Some compaction mechanism is possible but probably not
655 last = last->next = tmp;
656 if (total_lines - first->nlines > n_lines) {
658 total_lines -= first->nlines;
661 tmp = (LBUFFER *) xmalloc(sizeof(LBUFFER));
664 if (tmp->nbytes == -1) {
665 detailed_error(0, errno, "%s", filename);
673 /* This prevents a core dump when the pipe contains no newlines. */
677 /* Count the incomplete line on files that don't end with a newline. */
678 if (last->buffer[last->nbytes - 1] != '\n') {
683 /* Run through the list, printing lines. First, skip over unneeded
685 for (tmp = first; total_lines - tmp->nlines > n_lines; tmp = tmp->next)
686 total_lines -= tmp->nlines;
688 /* Find the correct beginning, then print the rest of the file. */
689 if (total_lines > n_lines) {
692 /* Skip `total_lines' - `n_lines' newlines. We made sure that
693 `total_lines' - `n_lines' <= `tmp->nlines'. */
695 for (i = total_lines - n_lines; i; --i)
696 while (*cp++ != '\n')
698 i = cp - tmp->buffer;
701 XWRITE(STDOUT_FILENO, &tmp->buffer[i], tmp->nbytes - i);
703 for (tmp = tmp->next; tmp; tmp = tmp->next)
704 XWRITE(STDOUT_FILENO, tmp->buffer, tmp->nbytes);
709 free((char *) first);
715 /* Print the last N_BYTES characters from the end of pipe FD.
716 This is a stripped down version of pipe_lines.
717 Return 0 if successful, 1 if an error occurred. */
719 static int pipe_bytes(const char *filename, int fd, off_t n_bytes)
724 struct charbuffer *next;
726 typedef struct charbuffer CBUFFER;
727 CBUFFER *first, *last, *tmp;
728 int i; /* Index into buffers. */
729 int total_bytes = 0; /* Total characters in all buffers. */
732 first = last = (CBUFFER *) xmalloc(sizeof(CBUFFER));
735 tmp = (CBUFFER *) xmalloc(sizeof(CBUFFER));
737 /* Input is always read into a fresh buffer. */
738 while ((tmp->nbytes = fullRead(fd, tmp->buffer, BUFSIZ)) > 0) {
741 total_bytes += tmp->nbytes;
742 /* If there is enough room in the last buffer read, just append the new
743 one to it. This is because when reading from a pipe, `nbytes' can
744 often be very small. */
745 if (tmp->nbytes + last->nbytes < BUFSIZ) {
746 memcpy(&last->buffer[last->nbytes], tmp->buffer, tmp->nbytes);
747 last->nbytes += tmp->nbytes;
749 /* If there's not enough room, link the new buffer onto the end of
750 the list, then either free up the oldest buffer for the next
751 read if that would leave enough characters, or else malloc a new
752 one. Some compaction mechanism is possible but probably not
754 last = last->next = tmp;
755 if (total_bytes - first->nbytes > n_bytes) {
757 total_bytes -= first->nbytes;
760 tmp = (CBUFFER *) xmalloc(sizeof(CBUFFER));
764 if (tmp->nbytes == -1) {
765 detailed_error(0, errno, "%s", filename);
773 /* Run through the list, printing characters. First, skip over unneeded
775 for (tmp = first; total_bytes - tmp->nbytes > n_bytes; tmp = tmp->next)
776 total_bytes -= tmp->nbytes;
778 /* Find the correct beginning, then print the rest of the file.
779 We made sure that `total_bytes' - `n_bytes' <= `tmp->nbytes'. */
780 if (total_bytes > n_bytes)
781 i = total_bytes - n_bytes;
784 XWRITE(STDOUT_FILENO, &tmp->buffer[i], tmp->nbytes - i);
786 for (tmp = tmp->next; tmp; tmp = tmp->next)
787 XWRITE(STDOUT_FILENO, tmp->buffer, tmp->nbytes);
792 free((char *) first);
798 /* Skip N_BYTES characters from the start of pipe FD, and print
799 any extra characters that were read beyond that.
800 Return 1 on error, 0 if ok. */
802 static int start_bytes(const char *filename, int fd, off_t n_bytes)
807 while (n_bytes > 0 && (bytes_read = fullRead(fd, buffer, BUFSIZ)) > 0)
808 n_bytes -= bytes_read;
809 if (bytes_read == -1) {
810 detailed_error(0, errno, "%s", filename);
812 } else if (n_bytes < 0)
813 XWRITE(STDOUT_FILENO, &buffer[bytes_read + n_bytes], -n_bytes);
817 /* Skip N_LINES lines at the start of file or pipe FD, and print
818 any extra characters that were read beyond that.
819 Return 1 on error, 0 if ok. */
821 static int start_lines(const char *filename, int fd, long int n_lines)
825 int bytes_to_skip = 0;
827 while (n_lines && (bytes_read = fullRead(fd, buffer, BUFSIZ)) > 0) {
829 while (bytes_to_skip < bytes_read)
830 if (buffer[bytes_to_skip++] == '\n' && --n_lines == 0)
833 if (bytes_read == -1) {
834 detailed_error(0, errno, "%s", filename);
836 } else if (bytes_to_skip < bytes_read) {
837 XWRITE(STDOUT_FILENO, &buffer[bytes_to_skip],
838 bytes_read - bytes_to_skip);
843 /* Display file FILENAME from the current position in FD to the end.
844 If `forever' is nonzero, keep reading from the end of the file
845 until killed. Return the number of bytes read from the file. */
847 static long dump_remainder(const char *filename, int fd)
855 while ((bytes_read = fullRead(fd, buffer, BUFSIZ)) > 0) {
856 XWRITE(STDOUT_FILENO, buffer, bytes_read);
859 if (bytes_read == -1)
860 detailed_error(EXIT_FAILURE, errno, "%s", filename);
866 if (forever_multiple)
873 /* Tail NFILES (>1) files forever until killed. The file names are in
874 NAMES. The open file descriptors are in `file_descs', and the size
875 at which we stopped tailing them is in `file_sizes'. We loop over
876 each of them, doing an fstat to see if they have changed size. If
877 none of them have changed size in one iteration, we sleep for a
878 second and try again. We do this until the user interrupts us. */
880 static void tail_forever(char **names, int nfiles)
891 for (i = 0; i < nfiles; i++) {
894 if (file_descs[i] < 0)
896 if (fstat(file_descs[i], &stats) < 0) {
897 detailed_error(0, errno, "%s", names[i]);
901 if (stats.st_size == file_sizes[i])
904 /* This file has changed size. Print out what we can, and
905 then keep looping. */
909 if (stats.st_size < file_sizes[i]) {
910 write_header(names[i], "file truncated");
912 lseek(file_descs[i], stats.st_size, SEEK_SET);
913 file_sizes[i] = stats.st_size;
919 write_header(names[i], NULL);
922 file_sizes[i] += dump_remainder(names[i], file_descs[i]);
925 /* If none of the files changed size, sleep. */
931 /* Output the last N_BYTES bytes of file FILENAME open for reading in FD.
932 Return 0 if successful, 1 if an error occurred. */
934 static int tail_bytes(const char *filename, int fd, off_t n_bytes)
938 /* FIXME: resolve this like in dd.c. */
939 /* Use fstat instead of checking for errno == ESPIPE because
940 lseek doesn't work on some special files but doesn't return an
942 if (fstat(fd, &stats)) {
943 detailed_error(0, errno, "%s", filename);
948 if (S_ISREG(stats.st_mode))
949 lseek(fd, n_bytes, SEEK_CUR);
950 else if (start_bytes(filename, fd, n_bytes))
952 dump_remainder(filename, fd);
954 if (S_ISREG(stats.st_mode)) {
955 off_t current_pos, end_pos;
956 size_t bytes_remaining;
958 if ((current_pos = lseek(fd, (off_t) 0, SEEK_CUR)) != -1
959 && (end_pos = lseek(fd, (off_t) 0, SEEK_END)) != -1) {
962 /* Be careful here. The current position may actually be
963 beyond the end of the file. */
964 bytes_remaining = (diff =
965 end_pos - current_pos) < 0 ? 0 : diff;
967 detailed_error(0, errno, "%s", filename);
971 if (bytes_remaining <= n_bytes) {
972 /* From the current position to end of file, there are no
973 more bytes than have been requested. So reposition the
974 file pointer to the incoming current position and print
975 everything after that. */
976 lseek(fd, current_pos, SEEK_SET);
978 /* There are more bytes remaining than were requested.
980 lseek(fd, -n_bytes, SEEK_END);
982 dump_remainder(filename, fd);
984 return pipe_bytes(filename, fd, n_bytes);
989 /* Output the last N_LINES lines of file FILENAME open for reading in FD.
990 Return 0 if successful, 1 if an error occurred. */
992 static int tail_lines(const char *filename, int fd, long int n_lines)
997 if (fstat(fd, &stats)) {
998 detailed_error(0, errno, "%s", filename);
1003 if (start_lines(filename, fd, n_lines))
1005 dump_remainder(filename, fd);
1007 /* Use file_lines only if FD refers to a regular file with
1008 its file pointer positioned at beginning of file. */
1009 /* FIXME: adding the lseek conjunct is a kludge.
1010 Once there's a reasonable test suite, fix the true culprit:
1011 file_lines. file_lines shouldn't presume that the input
1012 file pointer is initially positioned to beginning of file. */
1013 if (S_ISREG(stats.st_mode)
1014 && lseek(fd, (off_t) 0, SEEK_CUR) == (off_t) 0) {
1015 length = lseek(fd, (off_t) 0, SEEK_END);
1016 if (length != 0 && file_lines(filename, fd, n_lines, length))
1018 dump_remainder(filename, fd);
1020 return pipe_lines(filename, fd, n_lines);
1025 /* Display the last N_UNITS units of file FILENAME, open for reading
1027 Return 0 if successful, 1 if an error occurred. */
1029 static int tail(const char *filename, int fd, off_t n_units)
1032 return tail_lines(filename, fd, (long) n_units);
1034 return tail_bytes(filename, fd, n_units);
1037 /* Display the last N_UNITS units of file FILENAME.
1038 "-" for FILENAME means the standard input.
1039 FILENUM is this file's index in the list of files the user gave.
1040 Return 0 if successful, 1 if an error occurred. */
1042 static int tail_file(const char *filename, off_t n_units, int filenum)
1047 if (!strcmp(filename, "-")) {
1048 have_read_stdin = 1;
1049 filename = "standard input";
1051 write_header(filename, NULL);
1052 errors = tail(filename, 0, n_units);
1053 if (forever_multiple) {
1054 if (fstat(0, &stats) < 0) {
1055 detailed_error(0, errno, "standard input");
1057 } else if (!S_ISREG(stats.st_mode)) {
1058 detailed_error(0, 0,
1059 "standard input: cannot follow end of non-regular file");
1063 file_descs[filenum] = -1;
1065 file_descs[filenum] = 0;
1066 file_sizes[filenum] = stats.st_size;
1070 /* Not standard input. */
1071 fd = open(filename, O_RDONLY);
1073 if (forever_multiple)
1074 file_descs[filenum] = -1;
1075 detailed_error(0, errno, "%s", filename);
1079 write_header(filename, NULL);
1080 errors = tail(filename, fd, n_units);
1081 if (forever_multiple) {
1082 if (fstat(fd, &stats) < 0) {
1083 detailed_error(0, errno, "%s", filename);
1085 } else if (!S_ISREG(stats.st_mode)) {
1086 detailed_error(0, 0,
1087 "%s: cannot follow end of non-regular file",
1093 file_descs[filenum] = -1;
1095 file_descs[filenum] = fd;
1096 file_sizes[filenum] = stats.st_size;
1100 detailed_error(0, errno, "%s", filename);
1110 extern int tail_main(int argc, char **argv)
1113 enum header_mode header_mode = multiple_files;
1114 int exit_status = 0;
1116 /* If from_start, the number of items to skip before printing; otherwise,
1117 the number of items at the end of the file to print. Initially, -1
1118 means the value has not been set. */
1123 program_name = argv[0];
1124 have_read_stdin = 0;
1126 forever = forever_multiple = from_start = print_headers = 0;
1128 /* Parse any options */
1129 //fprintf(stderr, "argc=%d, argv=%s\n", argc, *argv);
1130 while (--argc > 0 && (**(++argv) == '-' || **argv == '+')) {
1131 if (**argv == '+') {
1135 while (stopit == 0 && *(++(*argv))) {
1143 n_units = getNum(*(++argv));
1157 n_units = atol(*(++argv));
1162 header_mode = never;
1166 header_mode = always;
1177 n_units = DEFAULT_N_LINES;
1179 /* To start printing with item N_UNITS from the start of the file, skip
1180 N_UNITS - 1 items. `tail +0' is actually meaningless, but for Unix
1181 compatibility it's treated the same as `tail +1'. */
1190 if (n_files > 1 && forever) {
1191 forever_multiple = 1;
1193 file_descs = (int *) xmalloc(n_files * sizeof(int));
1195 file_sizes = (off_t *) xmalloc(n_files * sizeof(off_t));
1198 if (header_mode == always
1199 || (header_mode == multiple_files && n_files > 1))
1203 exit_status |= tail_file("-", n_units, 0);
1207 for (i = 0; i < n_files; i++)
1208 exit_status |= tail_file(file[i], n_units, i);
1210 if (forever_multiple)
1211 tail_forever(file, n_files);
1214 if (have_read_stdin && close(0) < 0)
1215 detailed_error(EXIT_FAILURE, errno, "-");
1216 if (fclose(stdout) == EOF)
1217 detailed_error(EXIT_FAILURE, errno, "write error");
1218 exit(exit_status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);