*** empty log message ***
[coreutils.git] / src / tail.c
blobc6203b1b975b92a6bf39e82e02a5671ede272df7
1 /* tail -- output the last part of file(s)
2 Copyright (C) 1989, 90, 91, 1995-2002 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)
7 any later version.
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 Foundation,
16 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
20 of lines.
22 Original version by Paul Rubin <phr@ocf.berkeley.edu>.
23 Extensions by David MacKenzie <djm@gnu.ai.mit.edu>.
24 tail -f for multiple files by Ian Lance Taylor <ian@airs.com>. */
26 #include <config.h>
28 #include <stdio.h>
29 #include <assert.h>
30 #include <getopt.h>
31 #include <sys/types.h>
32 #include <signal.h>
34 #include "system.h"
35 #include "closeout.h"
36 #include "argmatch.h"
37 #include "error.h"
38 #include "human.h"
39 #include "posixver.h"
40 #include "safe-read.h"
41 #include "xstrtol.h"
43 /* The official name of this program (e.g., no `g' prefix). */
44 #define PROGRAM_NAME "tail"
46 #define AUTHORS \
47 "Paul Rubin, David MacKenzie, Ian Lance Taylor, and Jim Meyering"
49 #ifndef ENOSYS
50 /* Some systems don't have ENOSYS -- this should be a big enough
51 value that no valid errno value will match it. */
52 # define ENOSYS 99999
53 #endif
55 /* Number of items to tail. */
56 #define DEFAULT_N_LINES 10
58 /* Size of atomic reads. */
59 #ifndef BUFSIZ
60 # define BUFSIZ (512 * 8)
61 #endif
63 /* A special value for dump_remainder's N_BYTES parameter. */
64 #define COPY_TO_EOF OFF_T_MAX
66 /* FIXME: make Follow_name the default? */
67 #define DEFAULT_FOLLOW_MODE Follow_descriptor
69 enum Follow_mode
71 /* Follow the name of each file: if the file is renamed, try to reopen
72 that name and track the end of the new file if/when it's recreated.
73 This is useful for tracking logs that are occasionally rotated. */
74 Follow_name = 1,
76 /* Follow each descriptor obtained upon opening a file.
77 That means we'll continue to follow the end of a file even after
78 it has been renamed or unlinked. */
79 Follow_descriptor = 2
82 /* The types of files for which tail works. */
83 #define IS_TAILABLE_FILE_TYPE(Mode) \
84 (S_ISREG (Mode) || S_ISFIFO (Mode) || S_ISCHR (Mode))
86 static char const *const follow_mode_string[] =
88 "descriptor", "name", 0
91 static enum Follow_mode const follow_mode_map[] =
93 Follow_descriptor, Follow_name,
96 struct File_spec
98 /* The actual file name, or "-" for stdin. */
99 char *name;
101 /* File descriptor on which the file is open; -1 if it's not open. */
102 int fd;
104 /* The size of the file the last time we checked. */
105 off_t size;
107 /* The device and inode of the file the last time we checked. */
108 dev_t dev;
109 ino_t ino;
111 /* The specified name initially referred to a directory or some other
112 type for which tail isn't meaningful. Unlike for a permission problem
113 (tailable, below) once this is set, the name is not checked ever again. */
114 int ignore;
116 /* See description of DEFAULT_MAX_N_... below. */
117 unsigned int n_unchanged_stats;
119 /* See description of DEFAULT_MAX_N_... below. */
120 unsigned int n_consecutive_size_changes;
122 /* A file is tailable if it exists, is readable, and is of type
123 IS_TAILABLE_FILE_TYPE. */
124 int tailable;
126 /* The value of errno seen last time we checked this file. */
127 int errnum;
131 /* Keep trying to open a file even if it is inaccessible when tail starts
132 or if it becomes inaccessible later -- useful only with -f. */
133 static int reopen_inaccessible_files;
135 /* If nonzero, interpret the numeric argument as the number of lines.
136 Otherwise, interpret it as the number of bytes. */
137 static int count_lines;
139 /* Whether we follow the name of each file or the file descriptor
140 that is initially associated with each name. */
141 static enum Follow_mode follow_mode = Follow_descriptor;
143 /* If nonzero, read from the ends of all specified files until killed. */
144 static int forever;
146 /* If nonzero, count from start of file instead of end. */
147 static int from_start;
149 /* If nonzero, print filename headers. */
150 static int print_headers;
152 /* When to print the filename banners. */
153 enum header_mode
155 multiple_files, always, never
158 /* When tailing a file by name, if there have been this many consecutive
159 iterations for which the size has remained the same, then open/fstat
160 the file to determine if that file name is still associated with the
161 same device/inode-number pair as before. This option is meaningful only
162 when following by name. --max-unchanged-stats=N */
163 #define DEFAULT_MAX_N_UNCHANGED_STATS_BETWEEN_OPENS 5
164 static unsigned long max_n_unchanged_stats_between_opens =
165 DEFAULT_MAX_N_UNCHANGED_STATS_BETWEEN_OPENS;
167 /* This variable is used to ensure that a file that is unlinked or moved
168 aside, yet always growing will be recognized as having been renamed.
169 After detecting this many consecutive size changes for a file, open/fstat
170 the file to determine if that file name is still associated with the
171 same device/inode-number pair as before. This option is meaningful only
172 when following by name. --max-consecutive-size-changes=N */
173 #define DEFAULT_MAX_N_CONSECUTIVE_SIZE_CHANGES 200
174 static unsigned long max_n_consecutive_size_changes_between_opens =
175 DEFAULT_MAX_N_CONSECUTIVE_SIZE_CHANGES;
177 /* The name this program was run with. */
178 char *program_name;
180 /* The number of seconds to sleep between iterations.
181 During one iteration, every file name or descriptor is checked to
182 see if it has changed. */
183 /* FIXME: allow fractional seconds */
184 static unsigned int sleep_interval = 1;
186 /* The process ID of the process (presumably on the current host)
187 that is writing to all followed files. */
188 static pid_t pid;
190 /* Nonzero if we have ever read standard input. */
191 static int have_read_stdin;
193 /* For long options that have no equivalent short option, use a
194 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
195 enum
197 RETRY_OPTION = CHAR_MAX + 1,
198 MAX_UNCHANGED_STATS_OPTION,
200 /* FIXME: remove this in 2001, unless someone can show a good
201 reason to keep it. */
202 MAX_CONSECUTIVE_SIZE_CHANGES_OPTION,
204 PID_OPTION,
205 LONG_FOLLOW_OPTION
208 static struct option const long_options[] =
210 /* --allow-missing is deprecated; use --retry instead
211 FIXME: remove it some day */
212 {"allow-missing", no_argument, NULL, RETRY_OPTION},
213 {"bytes", required_argument, NULL, 'c'},
214 {"follow", optional_argument, NULL, LONG_FOLLOW_OPTION},
215 {"lines", required_argument, NULL, 'n'},
216 {"max-unchanged-stats", required_argument, NULL, MAX_UNCHANGED_STATS_OPTION},
217 {"max-consecutive-size-changes", required_argument, NULL,
218 MAX_CONSECUTIVE_SIZE_CHANGES_OPTION},
219 {"pid", required_argument, NULL, PID_OPTION},
220 {"quiet", no_argument, NULL, 'q'},
221 {"retry", no_argument, NULL, RETRY_OPTION},
222 {"silent", no_argument, NULL, 'q'},
223 {"sleep-interval", required_argument, NULL, 's'},
224 {"verbose", no_argument, NULL, 'v'},
225 {GETOPT_HELP_OPTION_DECL},
226 {GETOPT_VERSION_OPTION_DECL},
227 {NULL, 0, NULL, 0}
230 void
231 usage (int status)
233 if (status != 0)
234 fprintf (stderr, _("Try `%s --help' for more information.\n"),
235 program_name);
236 else
238 printf (_("\
239 Usage: %s [OPTION]... [FILE]...\n\
241 program_name);
242 printf (_("\
243 Print the last %d lines of each FILE to standard output.\n\
244 With more than one FILE, precede each with a header giving the file name.\n\
245 With no FILE, or when FILE is -, read standard input.\n\
247 "), DEFAULT_N_LINES);
248 fputs (_("\
249 Mandatory arguments to long options are mandatory for short options too.\n\
250 "), stdout);
251 fputs (_("\
252 --retry keep trying to open a file even if it is\n\
253 inaccessible when tail starts or if it becomes\n\
254 inaccessible later -- useful only with -f\n\
255 -c, --bytes=N output the last N bytes\n\
256 "), stdout);
257 fputs (_("\
258 -f, --follow[={name|descriptor}]\n\
259 output appended data as the file grows;\n\
260 -f, --follow, and --follow=descriptor are\n\
261 equivalent\n\
262 -F same as --follow=name --retry\n\
263 "), stdout);
264 printf (_("\
265 -n, --lines=N output the last N lines, instead of the last %d\n\
266 --max-unchanged-stats=N\n\
267 with --follow=name, reopen a FILE which has not\n\
268 changed size after N (default %d) iterations\n\
269 to see if it has been unlinked or renamed\n\
270 (this is the usual case of rotated log files)\n\
272 DEFAULT_N_LINES,
273 DEFAULT_MAX_N_UNCHANGED_STATS_BETWEEN_OPENS
275 fputs (_("\
276 --pid=PID with -f, terminate after process ID, PID dies\n\
277 -q, --quiet, --silent never output headers giving file names\n\
278 -s, --sleep-interval=S with -f, each iteration lasts approximately S\n\
279 (default 1) seconds\n\
280 -v, --verbose always output headers giving file names\n\
281 "), stdout);
282 fputs (HELP_OPTION_DESCRIPTION, stdout);
283 fputs (VERSION_OPTION_DESCRIPTION, stdout);
284 fputs (_("\
286 If the first character of N (the number of bytes or lines) is a `+',\n\
287 print beginning with the Nth item from the start of each file, otherwise,\n\
288 print the last N items in the file. N may have a multiplier suffix:\n\
289 b for 512, k for 1024, m for 1048576 (1 Meg).\n\
291 "), stdout);
292 fputs (_("\
293 With --follow (-f), tail defaults to following the file descriptor, which\n\
294 means that even if a tail'ed file is renamed, tail will continue to track\n\
295 its end. \
296 "), stdout);
297 fputs (_("\
298 This default behavior is not desirable when you really want to\n\
299 track the actual name of the file, not the file descriptor (e.g., log\n\
300 rotation). Use --follow=name in that case. That causes tail to track the\n\
301 named file by reopening it periodically to see if it has been removed and\n\
302 recreated by some other program.\n\
303 "), stdout);
304 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
306 exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
309 static int
310 valid_file_spec (struct File_spec const *f)
312 /* Exactly one of the following subexpressions must be true. */
313 return ((f->fd == -1) ^ (f->errnum == 0));
316 static char *
317 pretty_name (struct File_spec const *f)
319 return (STREQ (f->name, "-") ? "standard input" : f->name);
322 static void
323 xwrite (int fd, char *const buffer, size_t n_bytes)
325 assert (fd == STDOUT_FILENO);
326 if (n_bytes > 0 && fwrite (buffer, 1, n_bytes, stdout) == 0)
327 error (EXIT_FAILURE, errno, _("write error"));
330 static void
331 close_fd (int fd, const char *filename)
333 if (fd != -1 && fd != STDIN_FILENO && close (fd))
335 error (0, errno, _("closing %s (fd=%d)"), filename, fd);
339 static void
340 write_header (const char *pretty_filename)
342 static int first_file = 1;
344 printf ("%s==> %s <==\n", (first_file ? "" : "\n"), pretty_filename);
345 first_file = 0;
348 /* Read and output N_BYTES of file PRETTY_FILENAME starting at the current
349 position in FD. If N_BYTES is COPY_TO_EOF, then copy until end of file.
350 Return the number of bytes read from the file. */
352 static long
353 dump_remainder (const char *pretty_filename, int fd, off_t n_bytes)
355 char buffer[BUFSIZ];
356 int bytes_read;
357 long n_written;
358 off_t n_remaining = n_bytes;
360 n_written = 0;
361 while (1)
363 long n = MIN (n_remaining, (off_t) BUFSIZ);
364 bytes_read = safe_read (fd, buffer, n);
365 if (bytes_read <= 0)
366 break;
367 xwrite (STDOUT_FILENO, buffer, bytes_read);
368 n_remaining -= bytes_read;
369 n_written += bytes_read;
371 if (bytes_read == -1)
372 error (EXIT_FAILURE, errno, "%s", pretty_filename);
374 return n_written;
377 /* Call lseek with the specified arguments, where file descriptor FD
378 corresponds to the file, FILENAME.
379 Give a diagnostic and exit nonzero if lseek fails. */
381 static void
382 xlseek (int fd, off_t offset, int whence, char const *filename)
384 off_t new_offset = lseek (fd, offset, whence);
385 char buf[LONGEST_HUMAN_READABLE + 1];
386 char *s;
387 char const *sign;
389 if (0 <= new_offset)
390 return;
392 sign = offset < 0 ? "-" : "";
393 if (offset < 0)
394 offset = -offset;
396 s = human_readable ((uintmax_t) offset, buf, 1, 1);
397 switch (whence)
399 case SEEK_SET:
400 error (1, errno, _("%s: cannot seek to offset %s%s"),
401 filename, sign, s);
402 break;
403 case SEEK_CUR:
404 error (1, errno, _("%s: cannot seek to relative offset %s%s"),
405 filename, sign, s);
406 break;
407 case SEEK_END:
408 error (1, errno, _("%s: cannot seek to end-relative offset %s%s"),
409 filename, sign, s);
410 break;
411 default:
412 abort ();
416 /* Print the last N_LINES lines from the end of file FD.
417 Go backward through the file, reading `BUFSIZ' bytes at a time (except
418 probably the first), until we hit the start of the file or have
419 read NUMBER newlines.
420 START_POS is the starting position of the read pointer for the file
421 associated with FD (may be nonzero).
422 FILE_LENGTH is the length of the file (one more than the offset of the
423 last byte of the file).
424 Return 0 if successful, 1 if an error occurred. */
426 static int
427 file_lines (const char *pretty_filename, int fd, long int n_lines,
428 off_t start_pos, off_t file_length)
430 char buffer[BUFSIZ];
431 int bytes_read;
432 off_t pos = file_length;
434 if (n_lines == 0)
435 return 0;
437 /* Set `bytes_read' to the size of the last, probably partial, buffer;
438 0 < `bytes_read' <= `BUFSIZ'. */
439 bytes_read = (pos - start_pos) % BUFSIZ;
440 if (bytes_read == 0)
441 bytes_read = BUFSIZ;
442 /* Make `pos' a multiple of `BUFSIZ' (0 if the file is short), so that all
443 reads will be on block boundaries, which might increase efficiency. */
444 pos -= bytes_read;
445 xlseek (fd, pos, SEEK_SET, pretty_filename);
446 bytes_read = safe_read (fd, buffer, bytes_read);
447 if (bytes_read == -1)
449 error (0, errno, "%s", pretty_filename);
450 return 1;
453 /* Count the incomplete line on files that don't end with a newline. */
454 if (bytes_read && buffer[bytes_read - 1] != '\n')
455 --n_lines;
459 int i; /* Index into `buffer' for scanning. */
460 /* Scan backward, counting the newlines in this bufferfull. */
461 for (i = bytes_read - 1; i >= 0; i--)
463 /* Have we counted the requested number of newlines yet? */
464 if (buffer[i] == '\n' && n_lines-- == 0)
466 /* If this newline wasn't the last character in the buffer,
467 print the text after it. */
468 if (i != bytes_read - 1)
469 xwrite (STDOUT_FILENO, &buffer[i + 1], bytes_read - (i + 1));
470 dump_remainder (pretty_filename, fd,
471 file_length - (pos + bytes_read));
472 return 0;
475 /* Not enough newlines in that bufferfull. */
476 if (pos == start_pos)
478 /* Not enough lines in the file; print the entire file. */
479 xlseek (fd, start_pos, SEEK_SET, pretty_filename);
480 dump_remainder (pretty_filename, fd, file_length);
481 return 0;
483 pos -= BUFSIZ;
484 xlseek (fd, pos, SEEK_SET, pretty_filename);
486 while ((bytes_read = safe_read (fd, buffer, BUFSIZ)) > 0);
488 if (bytes_read == -1)
490 error (0, errno, "%s", pretty_filename);
491 return 1;
494 return 0;
497 /* Print the last N_LINES lines from the end of the standard input,
498 open for reading as pipe FD.
499 Buffer the text as a linked list of LBUFFERs, adding them as needed.
500 Return 0 if successful, 1 if an error occured. */
502 static int
503 pipe_lines (const char *pretty_filename, int fd, long int n_lines)
505 struct linebuffer
507 int nbytes, nlines;
508 char buffer[BUFSIZ];
509 struct linebuffer *next;
511 typedef struct linebuffer LBUFFER;
512 LBUFFER *first, *last, *tmp;
513 int i; /* Index into buffers. */
514 int total_lines = 0; /* Total number of newlines in all buffers. */
515 int errors = 0;
516 int nbytes; /* Size of most recent read */
518 first = last = (LBUFFER *) xmalloc (sizeof (LBUFFER));
519 first->nbytes = first->nlines = 0;
520 first->next = NULL;
521 tmp = (LBUFFER *) xmalloc (sizeof (LBUFFER));
523 /* Input is always read into a fresh buffer. */
524 while ((nbytes = tmp->nbytes = safe_read (fd, tmp->buffer, BUFSIZ)) > 0)
526 tmp->nlines = 0;
527 tmp->next = NULL;
529 /* Count the number of newlines just read. */
530 for (i = 0; i < tmp->nbytes; i++)
531 if (tmp->buffer[i] == '\n')
532 ++tmp->nlines;
533 total_lines += tmp->nlines;
535 /* If there is enough room in the last buffer read, just append the new
536 one to it. This is because when reading from a pipe, `nbytes' can
537 often be very small. */
538 if (tmp->nbytes + last->nbytes < BUFSIZ)
540 memcpy (&last->buffer[last->nbytes], tmp->buffer, tmp->nbytes);
541 last->nbytes += tmp->nbytes;
542 last->nlines += tmp->nlines;
544 else
546 /* If there's not enough room, link the new buffer onto the end of
547 the list, then either free up the oldest buffer for the next
548 read if that would leave enough lines, or else malloc a new one.
549 Some compaction mechanism is possible but probably not
550 worthwhile. */
551 last = last->next = tmp;
552 if (total_lines - first->nlines > n_lines)
554 tmp = first;
555 total_lines -= first->nlines;
556 first = first->next;
558 else
559 tmp = (LBUFFER *) xmalloc (sizeof (LBUFFER));
563 free ((char *) tmp);
565 if (nbytes < 0)
567 error (0, errno, "%s", pretty_filename);
568 errors = 1;
569 goto free_lbuffers;
572 /* If the file is empty, then bail out. */
573 if (last->nbytes == 0)
574 goto free_lbuffers;
576 /* This prevents a core dump when the pipe contains no newlines. */
577 if (n_lines == 0)
578 goto free_lbuffers;
580 /* Count the incomplete line on files that don't end with a newline. */
581 if (last->buffer[last->nbytes - 1] != '\n')
583 ++last->nlines;
584 ++total_lines;
587 /* Run through the list, printing lines. First, skip over unneeded
588 buffers. */
589 for (tmp = first; total_lines - tmp->nlines > n_lines; tmp = tmp->next)
590 total_lines -= tmp->nlines;
592 /* Find the correct beginning, then print the rest of the file. */
593 if (total_lines > n_lines)
595 const char *cp;
597 /* Skip `total_lines' - `n_lines' newlines. We made sure that
598 `total_lines' - `n_lines' <= `tmp->nlines'. */
599 cp = tmp->buffer;
600 for (i = total_lines - n_lines; i; --i)
601 while (*cp++ != '\n')
602 /* Do nothing. */ ;
603 i = cp - tmp->buffer;
605 else
606 i = 0;
607 xwrite (STDOUT_FILENO, &tmp->buffer[i], tmp->nbytes - i);
609 for (tmp = tmp->next; tmp; tmp = tmp->next)
610 xwrite (STDOUT_FILENO, tmp->buffer, tmp->nbytes);
612 free_lbuffers:
613 while (first)
615 tmp = first->next;
616 free ((char *) first);
617 first = tmp;
619 return errors;
622 /* Print the last N_BYTES characters from the end of pipe FD.
623 This is a stripped down version of pipe_lines.
624 Return 0 if successful, 1 if an error occurred. */
626 static int
627 pipe_bytes (const char *pretty_filename, int fd, off_t n_bytes)
629 struct charbuffer
631 int nbytes;
632 char buffer[BUFSIZ];
633 struct charbuffer *next;
635 typedef struct charbuffer CBUFFER;
636 CBUFFER *first, *last, *tmp;
637 int i; /* Index into buffers. */
638 int total_bytes = 0; /* Total characters in all buffers. */
639 int errors = 0;
641 first = last = (CBUFFER *) xmalloc (sizeof (CBUFFER));
642 first->nbytes = 0;
643 first->next = NULL;
644 tmp = (CBUFFER *) xmalloc (sizeof (CBUFFER));
646 /* Input is always read into a fresh buffer. */
647 while ((tmp->nbytes = safe_read (fd, tmp->buffer, BUFSIZ)) > 0)
649 tmp->next = NULL;
651 total_bytes += tmp->nbytes;
652 /* If there is enough room in the last buffer read, just append the new
653 one to it. This is because when reading from a pipe, `nbytes' can
654 often be very small. */
655 if (tmp->nbytes + last->nbytes < BUFSIZ)
657 memcpy (&last->buffer[last->nbytes], tmp->buffer, tmp->nbytes);
658 last->nbytes += tmp->nbytes;
660 else
662 /* If there's not enough room, link the new buffer onto the end of
663 the list, then either free up the oldest buffer for the next
664 read if that would leave enough characters, or else malloc a new
665 one. Some compaction mechanism is possible but probably not
666 worthwhile. */
667 last = last->next = tmp;
668 if (total_bytes - first->nbytes > n_bytes)
670 tmp = first;
671 total_bytes -= first->nbytes;
672 first = first->next;
674 else
676 tmp = (CBUFFER *) xmalloc (sizeof (CBUFFER));
680 if (tmp->nbytes == -1)
682 error (0, errno, "%s", pretty_filename);
683 errors = 1;
684 free ((char *) tmp);
685 goto free_cbuffers;
688 free ((char *) tmp);
690 /* Run through the list, printing characters. First, skip over unneeded
691 buffers. */
692 for (tmp = first; total_bytes - tmp->nbytes > n_bytes; tmp = tmp->next)
693 total_bytes -= tmp->nbytes;
695 /* Find the correct beginning, then print the rest of the file.
696 We made sure that `total_bytes' - `n_bytes' <= `tmp->nbytes'. */
697 if (total_bytes > n_bytes)
698 i = total_bytes - n_bytes;
699 else
700 i = 0;
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);
706 free_cbuffers:
707 while (first)
709 tmp = first->next;
710 free ((char *) first);
711 first = tmp;
713 return errors;
716 /* Skip N_BYTES characters from the start of pipe FD, and print
717 any extra characters that were read beyond that.
718 Return 1 on error, 0 if ok. */
720 static int
721 start_bytes (const char *pretty_filename, int fd, off_t n_bytes)
723 char buffer[BUFSIZ];
724 int bytes_read = 0;
726 while (n_bytes > 0 && (bytes_read = safe_read (fd, buffer, BUFSIZ)) > 0)
727 n_bytes -= bytes_read;
728 if (bytes_read == -1)
730 error (0, errno, "%s", pretty_filename);
731 return 1;
733 else if (n_bytes < 0)
734 xwrite (STDOUT_FILENO, &buffer[bytes_read + n_bytes], -n_bytes);
735 return 0;
738 /* Skip N_LINES lines at the start of file or pipe FD, and print
739 any extra characters that were read beyond that.
740 Return 1 on error, 0 if ok, -1 if EOF. */
742 static int
743 start_lines (const char *pretty_filename, int fd, long int n_lines)
745 char buffer[BUFSIZ];
746 int bytes_read = 0;
747 int bytes_to_skip = 0;
749 if (n_lines == 0)
750 return 0;
752 while (n_lines && (bytes_read = safe_read (fd, buffer, BUFSIZ)) > 0)
754 bytes_to_skip = 0;
755 while (bytes_to_skip < bytes_read)
756 if (buffer[bytes_to_skip++] == '\n' && --n_lines == 0)
757 break;
760 /* EOF */
761 if (bytes_read == 0)
762 return -1;
764 /* error */
765 if (bytes_read == -1)
767 error (0, errno, "%s", pretty_filename);
768 return 1;
771 if (bytes_to_skip < bytes_read)
773 xwrite (STDOUT_FILENO, &buffer[bytes_to_skip],
774 bytes_read - bytes_to_skip);
777 return 0;
780 /* FIXME: describe */
782 static void
783 recheck (struct File_spec *f)
785 /* open/fstat the file and announce if dev/ino have changed */
786 struct stat new_stats;
787 int fd;
788 int fail = 0;
789 int is_stdin = (STREQ (f->name, "-"));
790 int was_tailable = f->tailable;
791 int prev_errnum = f->errnum;
792 int new_file;
794 assert (valid_file_spec (f));
796 fd = (is_stdin ? STDIN_FILENO : open (f->name, O_RDONLY));
798 /* If the open fails because the file doesn't exist,
799 then mark the file as not tailable. */
800 f->tailable = !(reopen_inaccessible_files && fd == -1);
802 if (fd == -1 || fstat (fd, &new_stats) < 0)
804 fail = 1;
805 f->errnum = errno;
806 if (!f->tailable)
808 if (was_tailable)
810 /* FIXME-maybe: detect the case in which the file first becomes
811 unreadable (perms), and later becomes readable again and can
812 be seen to be the same file (dev/ino). Otherwise, tail prints
813 the entire contents of the file when it becomes readable. */
814 error (0, f->errnum, _("`%s' has become inaccessible"),
815 pretty_name (f));
817 else
819 /* say nothing... it's still not tailable */
822 else if (prev_errnum != errno)
824 error (0, errno, "%s", pretty_name (f));
827 else if (!IS_TAILABLE_FILE_TYPE (new_stats.st_mode))
829 fail = 1;
830 f->errnum = -1;
831 error (0, 0, _("`%s' has been replaced with an untailable file;\
832 giving up on this name"),
833 pretty_name (f));
834 f->ignore = 1;
836 else
838 f->errnum = 0;
841 new_file = 0;
842 if (fail)
844 close_fd (fd, pretty_name (f));
845 close_fd (f->fd, pretty_name (f));
846 f->fd = -1;
848 else if (prev_errnum && prev_errnum != ENOENT)
850 new_file = 1;
851 assert (f->fd == -1);
852 error (0, 0, _("`%s' has become accessible"), pretty_name (f));
854 else if (f->ino != new_stats.st_ino || f->dev != new_stats.st_dev)
856 new_file = 1;
857 if (f->fd == -1)
859 error (0, 0,
860 _("`%s' has appeared; following end of new file"),
861 pretty_name (f));
863 else
865 /* Close the old one. */
866 close_fd (f->fd, pretty_name (f));
868 /* File has been replaced (e.g., via log rotation) --
869 tail the new one. */
870 error (0, 0,
871 _("`%s' has been replaced; following end of new file"),
872 pretty_name (f));
875 else
877 if (f->fd == -1)
879 /* This happens when one iteration finds the file missing,
880 then the preceding <dev,inode> pair is reused as the
881 file is recreated. */
882 new_file = 1;
884 else
886 close_fd (fd, pretty_name (f));
890 if (new_file)
892 /* Record new file info in f. */
893 f->fd = fd;
894 f->size = 0; /* Start at the beginning of the file... */
895 f->dev = new_stats.st_dev;
896 f->ino = new_stats.st_ino;
897 f->n_unchanged_stats = 0;
898 f->n_consecutive_size_changes = 0;
899 f->ignore = 0;
900 xlseek (f->fd, f->size, SEEK_SET, pretty_name (f));
904 /* FIXME: describe */
906 static unsigned int
907 n_live_files (const struct File_spec *f, int n_files)
909 int i;
910 unsigned int n_live = 0;
912 for (i = 0; i < n_files; i++)
914 if (f[i].fd >= 0)
915 ++n_live;
917 return n_live;
920 /* Tail NFILES files forever, or until killed.
921 The pertinent information for each file is stored in an entry of F.
922 Loop over each of them, doing an fstat to see if they have changed size,
923 and an occasional open/fstat to see if any dev/ino pair has changed.
924 If none of them have changed size in one iteration, sleep for a
925 while and try again. Continue until the user interrupts us. */
927 static void
928 tail_forever (struct File_spec *f, int nfiles)
930 int last;
931 int writer_is_dead = 0;
933 last = nfiles - 1;
935 while (1)
937 int i;
938 int any_changed;
940 any_changed = 0;
941 for (i = 0; i < nfiles; i++)
943 struct stat stats;
945 if (f[i].ignore)
946 continue;
948 if (f[i].fd < 0)
950 recheck (&f[i]);
951 continue;
954 if (fstat (f[i].fd, &stats) < 0)
956 f[i].fd = -1;
957 f[i].errnum = errno;
958 error (0, errno, "%s", pretty_name (&f[i]));
959 continue;
962 if (stats.st_size == f[i].size)
964 f[i].n_consecutive_size_changes = 0;
965 if ((max_n_unchanged_stats_between_opens
966 <= f[i].n_unchanged_stats++)
967 && follow_mode == Follow_name)
969 recheck (&f[i]);
970 f[i].n_unchanged_stats = 0;
972 continue;
975 /* Ensure that a file that's unlinked or moved aside, yet always
976 growing will be recognized as having been renamed. */
977 if ((max_n_consecutive_size_changes_between_opens
978 <= f[i].n_consecutive_size_changes++)
979 && follow_mode == Follow_name)
981 f[i].n_consecutive_size_changes = 0;
982 recheck (&f[i]);
983 continue;
986 /* This file has changed size. Print out what we can, and
987 then keep looping. */
989 any_changed = 1;
991 /* reset counter */
992 f[i].n_unchanged_stats = 0;
994 if (stats.st_size < f[i].size)
996 error (0, 0, _("%s: file truncated"), pretty_name (&f[i]));
997 last = i;
998 xlseek (f[i].fd, (off_t) stats.st_size, SEEK_SET,
999 pretty_name (&f[i]));
1000 f[i].size = stats.st_size;
1001 continue;
1004 if (i != last)
1006 if (print_headers)
1007 write_header (pretty_name (&f[i]));
1008 last = i;
1010 f[i].size += dump_remainder (pretty_name (&f[i]), f[i].fd,
1011 COPY_TO_EOF);
1014 if (n_live_files (f, nfiles) == 0 && ! reopen_inaccessible_files)
1016 error (0, 0, _("no files remaining"));
1017 break;
1020 /* If none of the files changed size, sleep. */
1021 if (!any_changed)
1023 if (writer_is_dead)
1024 break;
1025 sleep (sleep_interval);
1027 /* Once the writer is dead, read the files once more to
1028 avoid a race condition. */
1029 writer_is_dead = (pid != 0
1030 && kill (pid, 0) != 0
1031 /* Handle the case in which you cannot send a
1032 signal to the writer, so kill fails and sets
1033 errno to EPERM. */
1034 && errno != EPERM);
1039 /* Output the last N_BYTES bytes of file FILENAME open for reading in FD.
1040 Return 0 if successful, 1 if an error occurred. */
1042 static int
1043 tail_bytes (const char *pretty_filename, int fd, off_t n_bytes)
1045 struct stat stats;
1047 /* We need binary input, since `tail' relies on `lseek' and byte counts,
1048 while binary output will preserve the style (Unix/DOS) of text file. */
1049 SET_BINARY2 (fd, STDOUT_FILENO);
1051 if (fstat (fd, &stats))
1053 error (0, errno, "%s", pretty_filename);
1054 return 1;
1057 if (from_start)
1059 if (S_ISREG (stats.st_mode))
1061 xlseek (fd, n_bytes, SEEK_CUR, pretty_filename);
1063 else if (start_bytes (pretty_filename, fd, n_bytes))
1065 return 1;
1067 dump_remainder (pretty_filename, fd, COPY_TO_EOF);
1069 else
1071 if (S_ISREG (stats.st_mode))
1073 off_t current_pos, end_pos;
1074 size_t bytes_remaining;
1076 if ((current_pos = lseek (fd, (off_t) 0, SEEK_CUR)) != -1
1077 && (end_pos = lseek (fd, (off_t) 0, SEEK_END)) != -1)
1079 off_t diff;
1080 /* Be careful here. The current position may actually be
1081 beyond the end of the file. */
1082 bytes_remaining = (diff = end_pos - current_pos) < 0 ? 0 : diff;
1084 else
1086 error (0, errno, "%s", pretty_filename);
1087 return 1;
1090 if (bytes_remaining <= n_bytes)
1092 /* From the current position to end of file, there are no
1093 more bytes than have been requested. So reposition the
1094 file pointer to the incoming current position and print
1095 everything after that. */
1096 xlseek (fd, current_pos, SEEK_SET, pretty_filename);
1098 else
1100 /* There are more bytes remaining than were requested.
1101 Back up. */
1102 xlseek (fd, -n_bytes, SEEK_END, pretty_filename);
1104 dump_remainder (pretty_filename, fd, n_bytes);
1106 else
1107 return pipe_bytes (pretty_filename, fd, n_bytes);
1109 return 0;
1112 /* Output the last N_LINES lines of file FILENAME open for reading in FD.
1113 Return 0 if successful, 1 if an error occurred. */
1115 static int
1116 tail_lines (const char *pretty_filename, int fd, long int n_lines)
1118 struct stat stats;
1120 /* We need binary input, since `tail' relies on `lseek' and byte counts,
1121 while binary output will preserve the style (Unix/DOS) of text file. */
1122 SET_BINARY2 (fd, STDOUT_FILENO);
1124 if (fstat (fd, &stats))
1126 error (0, errno, "%s", pretty_filename);
1127 return 1;
1130 if (from_start)
1132 int t;
1133 if ((t = start_lines (pretty_filename, fd, n_lines)) < 0)
1134 return 0;
1135 if (t)
1136 return 1;
1137 dump_remainder (pretty_filename, fd, COPY_TO_EOF);
1139 else
1141 off_t length;
1142 off_t start_pos;
1144 /* Use file_lines only if FD refers to a regular file for
1145 which lseek (... SEEK_END) works. */
1146 if (S_ISREG (stats.st_mode)
1147 && (start_pos = lseek (fd, (off_t) 0, SEEK_CUR)) != -1
1148 && start_pos < (length = lseek (fd, (off_t) 0, SEEK_END)))
1150 if (length != 0 && file_lines (pretty_filename, fd, n_lines,
1151 start_pos, length))
1152 return 1;
1154 else
1155 return pipe_lines (pretty_filename, fd, n_lines);
1157 return 0;
1160 /* Display the last N_UNITS units of file FILENAME, open for reading
1161 in FD.
1162 Return 0 if successful, 1 if an error occurred. */
1164 static int
1165 tail (const char *pretty_filename, int fd, off_t n_units)
1167 if (count_lines)
1168 return tail_lines (pretty_filename, fd, (long) n_units);
1169 else
1170 return tail_bytes (pretty_filename, fd, n_units);
1173 /* Display the last N_UNITS units of the file described by F.
1174 Return 0 if successful, 1 if an error occurred. */
1176 static int
1177 tail_file (struct File_spec *f, off_t n_units)
1179 int fd, errors;
1181 int is_stdin = (STREQ (f->name, "-"));
1183 if (is_stdin)
1185 have_read_stdin = 1;
1186 fd = STDIN_FILENO;
1188 else
1190 fd = open (f->name, O_RDONLY);
1193 f->tailable = !(reopen_inaccessible_files && fd == -1);
1195 if (fd == -1)
1197 if (forever)
1199 f->fd = -1;
1200 f->errnum = errno;
1201 f->ignore = 0;
1202 f->ino = 0;
1203 f->dev = 0;
1205 error (0, errno, "%s", pretty_name (f));
1206 errors = 1;
1208 else
1210 if (print_headers)
1211 write_header (pretty_name (f));
1212 errors = tail (pretty_name (f), fd, n_units);
1213 if (forever)
1215 struct stat stats;
1217 f->errnum = 0;
1218 if (fstat (fd, &stats) < 0)
1220 errors = 1;
1221 f->errnum = errno;
1222 error (0, errno, "%s", pretty_name (f));
1224 else if (!IS_TAILABLE_FILE_TYPE (stats.st_mode))
1226 error (0, 0, _("%s: cannot follow end of this type of file;\
1227 giving up on this name"),
1228 pretty_name (f));
1229 errors = 1;
1230 f->errnum = -1;
1231 f->ignore = 1;
1234 if (errors)
1236 close_fd (fd, pretty_name (f));
1237 f->fd = -1;
1239 else
1241 f->fd = fd;
1242 f->size = stats.st_size;
1243 f->dev = stats.st_dev;
1244 f->ino = stats.st_ino;
1245 f->n_unchanged_stats = 0;
1246 f->n_consecutive_size_changes = 0;
1247 f->ignore = 0;
1250 else
1252 if (!is_stdin && close (fd))
1254 error (0, errno, "%s", pretty_name (f));
1255 errors = 1;
1260 return errors;
1263 /* If the command line arguments are of the obsolescent form and the
1264 option string is well-formed, set *FAIL to zero, set *N_UNITS, the
1265 globals COUNT_LINES, FOREVER, and FROM_START, and return non-zero.
1266 Otherwise, if the command line arguments appear to be of the
1267 obsolescent form but the option string is malformed, set *FAIL to
1268 non-zero, don't modify any other parameter or global variable, and
1269 return non-zero. Otherwise, return zero and don't modify any parameter
1270 or global variable. */
1272 static int
1273 parse_obsolescent_option (int argc, const char *const *argv,
1274 off_t *n_units, int *fail)
1276 const char *p = argv[1];
1277 const char *n_string = NULL;
1278 const char *n_string_end;
1279 bool obsolete_usage;
1281 int t_from_start;
1282 int t_count_lines;
1283 int t_forever;
1285 /* With the obsolescent form, there is one option string and
1286 (technically) at most one file argument. But we allow two or more
1287 by default. */
1288 if (argc < 2)
1289 return 0;
1291 obsolete_usage = (posix2_version () < 200112);
1293 /* If P starts with `+' and the POSIX version predates 1003.1-2001,
1294 or if P starts with `-N' (where N is a digit), or `-l', then it
1295 is obsolescent. Return zero otherwise. */
1296 if (! ((p[0] == '+' && obsolete_usage)
1297 || (p[0] == '-' && (p[1] == 'l' || ISDIGIT (p[1])))))
1298 return 0;
1300 if (*p == '+')
1301 t_from_start = 1;
1302 else if (*p == '-')
1303 t_from_start = 0;
1304 else
1305 return 0;
1307 ++p;
1308 if (ISDIGIT (*p))
1310 n_string = p;
1313 ++p;
1315 while (ISDIGIT (*p));
1317 n_string_end = p;
1319 t_count_lines = 1;
1320 if (*p == 'c' || *p == 'b')
1322 t_count_lines = 0;
1323 ++p;
1325 else if (*p == 'l')
1327 ++p;
1330 t_forever = 0;
1331 if (*p == 'f')
1333 t_forever = 1;
1334 ++p;
1337 if (*p != '\0')
1339 /* If (argv[1] begins with a `+' or if it begins with `-' followed
1340 by a digit), but has an invalid suffix character, give a diagnostic
1341 and indicate to caller that this *is* of the obsolescent form,
1342 but that it's an invalid option. */
1343 if (t_from_start || n_string)
1345 error (0, 0,
1346 _("%c: invalid suffix character in obsolescent option" ), *p);
1347 *fail = 1;
1348 return 1;
1351 /* Otherwise, it might be a valid non-obsolescent option like -n. */
1352 return 0;
1355 *fail = 0;
1356 if (n_string == NULL)
1357 *n_units = DEFAULT_N_LINES;
1358 else
1360 strtol_error s_err;
1361 uintmax_t tmp;
1362 char *end;
1364 s_err = xstrtoumax (n_string, &end, 10, &tmp,
1365 *n_string_end == 'b' ? "b" : NULL);
1366 if (s_err == LONGINT_OK && tmp <= OFF_T_MAX)
1367 *n_units = (off_t) tmp;
1368 else
1370 /* Extract a NUL-terminated string for the error message. */
1371 size_t len = n_string_end - n_string;
1372 char *n_string_tmp = xmalloc (len + 1);
1374 strncpy (n_string_tmp, n_string, len);
1375 n_string_tmp[len] = '\0';
1377 error (0, 0,
1378 _("%s: %s is so large that it is not representable"),
1379 n_string_tmp, (t_count_lines
1380 ? _("number of lines")
1381 : _("number of bytes")));
1382 free (n_string_tmp);
1383 *fail = 1;
1387 if (!*fail)
1389 if (argc > 3)
1391 /* When POSIXLY_CORRECT is set, enforce the `at most one
1392 file argument' requirement. */
1393 if (getenv ("POSIXLY_CORRECT"))
1395 error (0, 0, _("\
1396 too many arguments; When using tail's obsolescent option syntax (%s)\n\
1397 there may be no more than one file argument. Use the equivalent -n or -c\n\
1398 option instead."), argv[1]);
1399 *fail = 1;
1400 return 1;
1403 #if DISABLED /* FIXME: enable or remove this warning. */
1404 error (0, 0, _("\
1405 Warning: it is not portable to use two or more file arguments with\n\
1406 tail's obsolescent option syntax (%s). Use the equivalent -n or -c\n\
1407 option instead."), argv[1]);
1408 #endif
1411 if (! obsolete_usage)
1413 error (0, 0, _("`%s' option is obsolete; use `%s-%c %.*s'"),
1414 argv[1], t_forever ? " -f" : "", t_count_lines ? 'n' : 'c',
1415 (int) (n_string_end - n_string), n_string);
1416 usage (EXIT_FAILURE);
1419 /* Set globals. */
1420 from_start = t_from_start;
1421 count_lines = t_count_lines;
1422 forever = t_forever;
1425 return 1;
1428 static void
1429 parse_options (int argc, char **argv,
1430 off_t *n_units, enum header_mode *header_mode)
1432 int c;
1434 count_lines = 1;
1435 forever = from_start = print_headers = 0;
1437 while ((c = getopt_long (argc, argv, "c:n:fFqs:v", long_options, NULL))
1438 != -1)
1440 switch (c)
1442 case 0:
1443 break;
1445 case 'F':
1446 forever = 1;
1447 follow_mode = Follow_name;
1448 reopen_inaccessible_files = 1;
1449 break;
1451 case 'c':
1452 case 'n':
1453 count_lines = (c == 'n');
1454 if (*optarg == '+')
1455 from_start = 1;
1456 else if (*optarg == '-')
1457 ++optarg;
1460 strtol_error s_err;
1461 uintmax_t n;
1462 s_err = xstrtoumax (optarg, NULL, 10, &n, "bkm");
1463 if (s_err == LONGINT_INVALID)
1465 error (EXIT_FAILURE, 0, "%s: %s", optarg,
1466 (c == 'n'
1467 ? _("invalid number of lines")
1468 : _("invalid number of bytes")));
1471 if (s_err != LONGINT_OK)
1472 error (EXIT_FAILURE, 0,
1473 _("%s: is so large that it is not representable"), optarg);
1475 if (OFF_T_MAX < n)
1476 error (EXIT_FAILURE, 0,
1477 _("%s is larger than the maximum file size on this system"),
1478 optarg);
1479 *n_units = (off_t) n;
1481 break;
1483 case 'f':
1484 case LONG_FOLLOW_OPTION:
1485 forever = 1;
1486 if (optarg == NULL)
1487 follow_mode = DEFAULT_FOLLOW_MODE;
1488 else
1489 follow_mode = XARGMATCH ("--follow", optarg,
1490 follow_mode_string, follow_mode_map);
1491 break;
1493 case RETRY_OPTION:
1494 reopen_inaccessible_files = 1;
1495 break;
1497 case MAX_UNCHANGED_STATS_OPTION:
1498 /* --max-unchanged-stats=N */
1499 if (xstrtoul (optarg, NULL, 10,
1500 &max_n_unchanged_stats_between_opens, "") != LONGINT_OK)
1502 error (EXIT_FAILURE, 0,
1503 _("%s: invalid maximum number of unchanged stats between opens"),
1504 optarg);
1506 break;
1508 case MAX_CONSECUTIVE_SIZE_CHANGES_OPTION:
1509 /* --max-consecutive-size-changes=N */
1510 if (xstrtoul (optarg, NULL, 10,
1511 &max_n_consecutive_size_changes_between_opens, "")
1512 != LONGINT_OK)
1514 error (EXIT_FAILURE, 0,
1515 _("%s: invalid maximum number of consecutive size changes"),
1516 optarg);
1518 break;
1520 case PID_OPTION:
1522 strtol_error s_err;
1523 unsigned long int tmp_ulong;
1524 s_err = xstrtoul (optarg, NULL, 10, &tmp_ulong, "");
1525 if (s_err != LONGINT_OK || tmp_ulong > PID_T_MAX)
1527 error (EXIT_FAILURE, 0, _("%s: invalid PID"), optarg);
1529 pid = tmp_ulong;
1531 break;
1533 case 'q':
1534 *header_mode = never;
1535 break;
1537 case 's':
1539 strtol_error s_err;
1540 unsigned long int tmp_ulong;
1541 s_err = xstrtoul (optarg, NULL, 10, &tmp_ulong, "");
1542 if (s_err != LONGINT_OK || tmp_ulong > UINT_MAX)
1544 error (EXIT_FAILURE, 0,
1545 _("%s: invalid number of seconds"), optarg);
1547 sleep_interval = tmp_ulong;
1549 break;
1551 case 'v':
1552 *header_mode = always;
1553 break;
1555 case_GETOPT_HELP_CHAR;
1557 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
1559 default:
1560 usage (1);
1564 if (reopen_inaccessible_files && follow_mode != Follow_name)
1565 error (0, 0, _("warning: --retry is useful only when following by name"));
1567 if (pid && !forever)
1568 error (0, 0,
1569 _("warning: PID ignored; --pid=PID is useful only when following"));
1570 else if (pid && kill (pid, 0) != 0 && errno == ENOSYS)
1572 error (0, 0, _("warning: --pid=PID is not supported on this system"));
1573 pid = 0;
1578 main (int argc, char **argv)
1580 enum header_mode header_mode = multiple_files;
1581 int exit_status = 0;
1582 /* If from_start, the number of items to skip before printing; otherwise,
1583 the number of items at the end of the file to print. Although the type
1584 is signed, the value is never negative. */
1585 off_t n_units = DEFAULT_N_LINES;
1586 int n_files;
1587 char **file;
1588 struct File_spec *F;
1589 int i;
1591 program_name = argv[0];
1592 setlocale (LC_ALL, "");
1593 bindtextdomain (PACKAGE, LOCALEDIR);
1594 textdomain (PACKAGE);
1596 atexit (close_stdout);
1598 have_read_stdin = 0;
1601 int fail;
1603 if (parse_obsolescent_option (argc,
1604 (const char *const *) argv,
1605 &n_units, &fail))
1607 if (fail)
1608 exit (EXIT_FAILURE);
1609 optind = 2;
1611 else
1613 parse_options (argc, argv, &n_units, &header_mode);
1617 /* To start printing with item N_UNITS from the start of the file, skip
1618 N_UNITS - 1 items. `tail -n +0' is actually meaningless, but for Unix
1619 compatibility it's treated the same as `tail -n +1'. */
1620 if (from_start)
1622 if (n_units)
1623 --n_units;
1626 n_files = argc - optind;
1627 file = argv + optind;
1629 if (n_files == 0)
1631 static char *dummy_stdin = "-";
1632 n_files = 1;
1633 file = &dummy_stdin;
1636 F = (struct File_spec *) xmalloc (n_files * sizeof (F[0]));
1637 for (i = 0; i < n_files; i++)
1638 F[i].name = file[i];
1640 if (header_mode == always
1641 || (header_mode == multiple_files && n_files > 1))
1642 print_headers = 1;
1644 for (i = 0; i < n_files; i++)
1645 exit_status |= tail_file (&F[i], n_units);
1647 if (forever)
1649 /* This fflush appears to be required only on Solaris2.7. */
1650 if (fflush (stdout) < 0)
1651 error (EXIT_FAILURE, errno, _("write error"));
1653 SETVBUF (stdout, NULL, _IONBF, 0);
1654 tail_forever (F, n_files);
1657 if (have_read_stdin && close (0) < 0)
1658 error (EXIT_FAILURE, errno, "-");
1659 exit (exit_status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);