.
[coreutils.git] / src / tail.c
blobdad6da4848eae0cb55038466d01ca47a339b71dd
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 "inttostr.h"
39 #include "posixver.h"
40 #include "safe-read.h"
41 #include "xnanosleep.h"
42 #include "xstrtol.h"
43 #include "xstrtod.h"
45 /* The official name of this program (e.g., no `g' prefix). */
46 #define PROGRAM_NAME "tail"
48 #define AUTHORS \
49 N_ ("Paul Rubin, David MacKenzie, Ian Lance Taylor, and Jim Meyering")
51 #ifndef ENOSYS
52 /* Some systems don't have ENOSYS -- this should be a big enough
53 value that no valid errno value will match it. */
54 # define ENOSYS 99999
55 #endif
57 /* Number of items to tail. */
58 #define DEFAULT_N_LINES 10
60 /* Size of atomic reads. */
61 #ifndef BUFSIZ
62 # define BUFSIZ (512 * 8)
63 #endif
65 /* A special value for dump_remainder's N_BYTES parameter. */
66 #define COPY_TO_EOF OFF_T_MAX
68 /* FIXME: make Follow_name the default? */
69 #define DEFAULT_FOLLOW_MODE Follow_descriptor
71 enum Follow_mode
73 /* Follow the name of each file: if the file is renamed, try to reopen
74 that name and track the end of the new file if/when it's recreated.
75 This is useful for tracking logs that are occasionally rotated. */
76 Follow_name = 1,
78 /* Follow each descriptor obtained upon opening a file.
79 That means we'll continue to follow the end of a file even after
80 it has been renamed or unlinked. */
81 Follow_descriptor = 2
84 /* The types of files for which tail works. */
85 #define IS_TAILABLE_FILE_TYPE(Mode) \
86 (S_ISREG (Mode) || S_ISFIFO (Mode) || S_ISCHR (Mode))
88 static char const *const follow_mode_string[] =
90 "descriptor", "name", 0
93 static enum Follow_mode const follow_mode_map[] =
95 Follow_descriptor, Follow_name,
98 struct File_spec
100 /* The actual file name, or "-" for stdin. */
101 char *name;
103 /* File descriptor on which the file is open; -1 if it's not open. */
104 int fd;
106 /* The size of the file the last time we checked. */
107 off_t size;
109 /* The device and inode of the file the last time we checked. */
110 dev_t dev;
111 ino_t ino;
113 /* The specified name initially referred to a directory or some other
114 type for which tail isn't meaningful. Unlike for a permission problem
115 (tailable, below) once this is set, the name is not checked ever again. */
116 int ignore;
118 /* See description of DEFAULT_MAX_N_... below. */
119 unsigned int n_unchanged_stats;
121 /* See description of DEFAULT_MAX_N_... below. */
122 unsigned int n_consecutive_size_changes;
124 /* A file is tailable if it exists, is readable, and is of type
125 IS_TAILABLE_FILE_TYPE. */
126 int tailable;
128 /* The value of errno seen last time we checked this file. */
129 int errnum;
133 /* Keep trying to open a file even if it is inaccessible when tail starts
134 or if it becomes inaccessible later -- useful only with -f. */
135 static int reopen_inaccessible_files;
137 /* If nonzero, interpret the numeric argument as the number of lines.
138 Otherwise, interpret it as the number of bytes. */
139 static int count_lines;
141 /* Whether we follow the name of each file or the file descriptor
142 that is initially associated with each name. */
143 static enum Follow_mode follow_mode = Follow_descriptor;
145 /* If nonzero, read from the ends of all specified files until killed. */
146 static int forever;
148 /* If nonzero, count from start of file instead of end. */
149 static int from_start;
151 /* If nonzero, print filename headers. */
152 static int print_headers;
154 /* When to print the filename banners. */
155 enum header_mode
157 multiple_files, always, never
160 /* When tailing a file by name, if there have been this many consecutive
161 iterations for which the size has remained the same, then open/fstat
162 the file to determine if that file name is still associated with the
163 same device/inode-number pair as before. This option is meaningful only
164 when following by name. --max-unchanged-stats=N */
165 #define DEFAULT_MAX_N_UNCHANGED_STATS_BETWEEN_OPENS 5
166 static unsigned long max_n_unchanged_stats_between_opens =
167 DEFAULT_MAX_N_UNCHANGED_STATS_BETWEEN_OPENS;
169 /* This variable is used to ensure that a file that is unlinked or moved
170 aside, yet always growing will be recognized as having been renamed.
171 After detecting this many consecutive size changes for a file, open/fstat
172 the file to determine if that file name is still associated with the
173 same device/inode-number pair as before. This option is meaningful only
174 when following by name. --max-consecutive-size-changes=N */
175 #define DEFAULT_MAX_N_CONSECUTIVE_SIZE_CHANGES 200
176 static unsigned long max_n_consecutive_size_changes_between_opens =
177 DEFAULT_MAX_N_CONSECUTIVE_SIZE_CHANGES;
179 /* The name this program was run with. */
180 char *program_name;
182 /* The process ID of the process (presumably on the current host)
183 that is writing to all followed files. */
184 static pid_t pid;
186 /* Nonzero if we have ever read standard input. */
187 static int have_read_stdin;
189 /* For long options that have no equivalent short option, use a
190 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
191 enum
193 RETRY_OPTION = CHAR_MAX + 1,
194 MAX_UNCHANGED_STATS_OPTION,
196 /* FIXME: remove this in 2001, unless someone can show a good
197 reason to keep it. */
198 MAX_CONSECUTIVE_SIZE_CHANGES_OPTION,
200 PID_OPTION,
201 LONG_FOLLOW_OPTION
204 static struct option const long_options[] =
206 /* --allow-missing is deprecated; use --retry instead
207 FIXME: remove it some day */
208 {"allow-missing", no_argument, NULL, RETRY_OPTION},
209 {"bytes", required_argument, NULL, 'c'},
210 {"follow", optional_argument, NULL, LONG_FOLLOW_OPTION},
211 {"lines", required_argument, NULL, 'n'},
212 {"max-unchanged-stats", required_argument, NULL, MAX_UNCHANGED_STATS_OPTION},
213 {"max-consecutive-size-changes", required_argument, NULL,
214 MAX_CONSECUTIVE_SIZE_CHANGES_OPTION},
215 {"pid", required_argument, NULL, PID_OPTION},
216 {"quiet", no_argument, NULL, 'q'},
217 {"retry", no_argument, NULL, RETRY_OPTION},
218 {"silent", no_argument, NULL, 'q'},
219 {"sleep-interval", required_argument, NULL, 's'},
220 {"verbose", no_argument, NULL, 'v'},
221 {GETOPT_HELP_OPTION_DECL},
222 {GETOPT_VERSION_OPTION_DECL},
223 {NULL, 0, NULL, 0}
226 void
227 usage (int status)
229 if (status != 0)
230 fprintf (stderr, _("Try `%s --help' for more information.\n"),
231 program_name);
232 else
234 printf (_("\
235 Usage: %s [OPTION]... [FILE]...\n\
237 program_name);
238 printf (_("\
239 Print the last %d lines of each FILE to standard output.\n\
240 With more than one FILE, precede each with a header giving the file name.\n\
241 With no FILE, or when FILE is -, read standard input.\n\
243 "), DEFAULT_N_LINES);
244 fputs (_("\
245 Mandatory arguments to long options are mandatory for short options too.\n\
246 "), stdout);
247 fputs (_("\
248 --retry keep trying to open a file even if it is\n\
249 inaccessible when tail starts or if it becomes\n\
250 inaccessible later -- useful only with -f\n\
251 -c, --bytes=N output the last N bytes\n\
252 "), stdout);
253 fputs (_("\
254 -f, --follow[={name|descriptor}]\n\
255 output appended data as the file grows;\n\
256 -f, --follow, and --follow=descriptor are\n\
257 equivalent\n\
258 -F same as --follow=name --retry\n\
259 "), stdout);
260 printf (_("\
261 -n, --lines=N output the last N lines, instead of the last %d\n\
262 --max-unchanged-stats=N\n\
263 with --follow=name, reopen a FILE which has not\n\
264 changed size after N (default %d) iterations\n\
265 to see if it has been unlinked or renamed\n\
266 (this is the usual case of rotated log files)\n\
268 DEFAULT_N_LINES,
269 DEFAULT_MAX_N_UNCHANGED_STATS_BETWEEN_OPENS
271 fputs (_("\
272 --pid=PID with -f, terminate after process ID, PID dies\n\
273 -q, --quiet, --silent never output headers giving file names\n\
274 -s, --sleep-interval=S with -f, sleep for approximately S seconds\n\
275 (default 1.0) between iterations.\n\
276 -v, --verbose always output headers giving file names\n\
277 "), stdout);
278 fputs (HELP_OPTION_DESCRIPTION, stdout);
279 fputs (VERSION_OPTION_DESCRIPTION, stdout);
280 fputs (_("\
282 If the first character of N (the number of bytes or lines) is a `+',\n\
283 print beginning with the Nth item from the start of each file, otherwise,\n\
284 print the last N items in the file. N may have a multiplier suffix:\n\
285 b for 512, k for 1024, m for 1048576 (1 Meg).\n\
287 "), stdout);
288 fputs (_("\
289 With --follow (-f), tail defaults to following the file descriptor, which\n\
290 means that even if a tail'ed file is renamed, tail will continue to track\n\
291 its end. \
292 "), stdout);
293 fputs (_("\
294 This default behavior is not desirable when you really want to\n\
295 track the actual name of the file, not the file descriptor (e.g., log\n\
296 rotation). Use --follow=name in that case. That causes tail to track the\n\
297 named file by reopening it periodically to see if it has been removed and\n\
298 recreated by some other program.\n\
299 "), stdout);
300 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
302 exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
305 static int
306 valid_file_spec (struct File_spec const *f)
308 /* Exactly one of the following subexpressions must be true. */
309 return ((f->fd == -1) ^ (f->errnum == 0));
312 static char *
313 pretty_name (struct File_spec const *f)
315 return (STREQ (f->name, "-") ? "standard input" : f->name);
318 static void
319 xwrite (int fd, char const *buffer, size_t n_bytes)
321 assert (fd == STDOUT_FILENO);
322 if (n_bytes > 0 && fwrite (buffer, 1, n_bytes, stdout) == 0)
323 error (EXIT_FAILURE, errno, _("write error"));
326 static void
327 close_fd (int fd, const char *filename)
329 if (fd != -1 && fd != STDIN_FILENO && close (fd))
331 error (0, errno, _("closing %s (fd=%d)"), filename, fd);
335 static void
336 write_header (const char *pretty_filename)
338 static int first_file = 1;
340 printf ("%s==> %s <==\n", (first_file ? "" : "\n"), pretty_filename);
341 first_file = 0;
344 /* Read and output N_BYTES of file PRETTY_FILENAME starting at the current
345 position in FD. If N_BYTES is COPY_TO_EOF, then copy until end of file.
346 Return the number of bytes read from the file. */
348 static long
349 dump_remainder (const char *pretty_filename, int fd, off_t n_bytes)
351 char buffer[BUFSIZ];
352 size_t bytes_read;
353 long n_written;
354 off_t n_remaining = n_bytes;
356 n_written = 0;
357 while (1)
359 long n = MIN (n_remaining, (off_t) BUFSIZ);
360 bytes_read = safe_read (fd, buffer, n);
361 if (bytes_read == SAFE_READ_ERROR)
362 error (EXIT_FAILURE, errno, "%s", pretty_filename);
363 if (bytes_read == 0)
364 break;
365 xwrite (STDOUT_FILENO, buffer, bytes_read);
366 n_remaining -= bytes_read;
367 n_written += bytes_read;
370 return n_written;
373 /* Call lseek with the specified arguments, where file descriptor FD
374 corresponds to the file, FILENAME.
375 Give a diagnostic and exit nonzero if lseek fails. */
377 static void
378 xlseek (int fd, off_t offset, int whence, char const *filename)
380 off_t new_offset = lseek (fd, offset, whence);
381 char buf[INT_BUFSIZE_BOUND (off_t)];
382 char *s;
384 if (0 <= new_offset)
385 return;
387 s = offtostr (offset, buf);
388 switch (whence)
390 case SEEK_SET:
391 error (EXIT_FAILURE, errno, _("%s: cannot seek to offset %s"),
392 filename, s);
393 break;
394 case SEEK_CUR:
395 error (EXIT_FAILURE, errno, _("%s: cannot seek to relative offset %s"),
396 filename, s);
397 break;
398 case SEEK_END:
399 error (EXIT_FAILURE, errno,
400 _("%s: cannot seek to end-relative offset %s"),
401 filename, s);
402 break;
403 default:
404 abort ();
408 /* Print the last N_LINES lines from the end of file FD.
409 Go backward through the file, reading `BUFSIZ' bytes at a time (except
410 probably the first), until we hit the start of the file or have
411 read NUMBER newlines.
412 START_POS is the starting position of the read pointer for the file
413 associated with FD (may be nonzero).
414 FILE_LENGTH is the length of the file (one more than the offset of the
415 last byte of the file).
416 Return 0 if successful, 1 if an error occurred. */
418 static int
419 file_lines (const char *pretty_filename, int fd, long int n_lines,
420 off_t start_pos, off_t file_length)
422 char buffer[BUFSIZ];
423 size_t bytes_read;
424 off_t pos = file_length;
426 if (n_lines == 0)
427 return 0;
429 /* Set `bytes_read' to the size of the last, probably partial, buffer;
430 0 < `bytes_read' <= `BUFSIZ'. */
431 bytes_read = (pos - start_pos) % BUFSIZ;
432 if (bytes_read == 0)
433 bytes_read = BUFSIZ;
434 /* Make `pos' a multiple of `BUFSIZ' (0 if the file is short), so that all
435 reads will be on block boundaries, which might increase efficiency. */
436 pos -= bytes_read;
437 xlseek (fd, pos, SEEK_SET, pretty_filename);
438 bytes_read = safe_read (fd, buffer, bytes_read);
439 if (bytes_read == SAFE_READ_ERROR)
441 error (0, errno, "%s", pretty_filename);
442 return 1;
445 /* Count the incomplete line on files that don't end with a newline. */
446 if (bytes_read && buffer[bytes_read - 1] != '\n')
447 --n_lines;
451 /* Scan backward, counting the newlines in this bufferfull. */
453 size_t n = bytes_read;
454 while (n)
456 char const *nl;
457 nl = memrchr (buffer, '\n', n);
458 if (nl == NULL)
459 break;
460 n = nl - buffer;
461 if (n_lines-- == 0)
463 /* If this newline isn't the last character in the buffer,
464 output the part that is after it. */
465 if (n != bytes_read - 1)
466 xwrite (STDOUT_FILENO, nl + 1, bytes_read - (n + 1));
467 dump_remainder (pretty_filename, fd,
468 file_length - (pos + bytes_read));
469 return 0;
473 /* Not enough newlines in that bufferfull. */
474 if (pos == start_pos)
476 /* Not enough lines in the file; print the entire file. */
477 xlseek (fd, start_pos, SEEK_SET, pretty_filename);
478 dump_remainder (pretty_filename, fd, file_length);
479 return 0;
481 pos -= BUFSIZ;
482 xlseek (fd, pos, SEEK_SET, pretty_filename);
484 bytes_read = safe_read (fd, buffer, BUFSIZ);
485 if (bytes_read == SAFE_READ_ERROR)
487 error (0, errno, "%s", pretty_filename);
488 return 1;
491 while (bytes_read > 0);
493 return 0;
496 /* Print the last N_LINES lines from the end of the standard input,
497 open for reading as pipe FD.
498 Buffer the text as a linked list of LBUFFERs, adding them as needed.
499 Return 0 if successful, 1 if an error occured. */
501 static int
502 pipe_lines (const char *pretty_filename, int fd, long int n_lines)
504 struct linebuffer
506 int nbytes, nlines;
507 char buffer[BUFSIZ];
508 struct linebuffer *next;
510 typedef struct linebuffer LBUFFER;
511 LBUFFER *first, *last, *tmp;
512 int i; /* Index into buffers. */
513 int total_lines = 0; /* Total number of newlines in all buffers. */
514 int errors = 0;
515 int nbytes; /* Size of most recent read */
517 first = last = (LBUFFER *) xmalloc (sizeof (LBUFFER));
518 first->nbytes = first->nlines = 0;
519 first->next = NULL;
520 tmp = (LBUFFER *) xmalloc (sizeof (LBUFFER));
522 /* Input is always read into a fresh buffer. */
523 while ((nbytes = tmp->nbytes = safe_read (fd, tmp->buffer, BUFSIZ)) > 0)
525 tmp->nlines = 0;
526 tmp->next = NULL;
528 /* Count the number of newlines just read. */
529 for (i = 0; i < tmp->nbytes; i++)
530 if (tmp->buffer[i] == '\n')
531 ++tmp->nlines;
532 total_lines += tmp->nlines;
534 /* If there is enough room in the last buffer read, just append the new
535 one to it. This is because when reading from a pipe, `nbytes' can
536 often be very small. */
537 if (tmp->nbytes + last->nbytes < BUFSIZ)
539 memcpy (&last->buffer[last->nbytes], tmp->buffer, tmp->nbytes);
540 last->nbytes += tmp->nbytes;
541 last->nlines += tmp->nlines;
543 else
545 /* If there's not enough room, link the new buffer onto the end of
546 the list, then either free up the oldest buffer for the next
547 read if that would leave enough lines, or else malloc a new one.
548 Some compaction mechanism is possible but probably not
549 worthwhile. */
550 last = last->next = tmp;
551 if (total_lines - first->nlines > n_lines)
553 tmp = first;
554 total_lines -= first->nlines;
555 first = first->next;
557 else
558 tmp = (LBUFFER *) xmalloc (sizeof (LBUFFER));
562 free ((char *) tmp);
564 if (nbytes < 0)
566 error (0, errno, "%s", pretty_filename);
567 errors = 1;
568 goto free_lbuffers;
571 /* If the file is empty, then bail out. */
572 if (last->nbytes == 0)
573 goto free_lbuffers;
575 /* This prevents a core dump when the pipe contains no newlines. */
576 if (n_lines == 0)
577 goto free_lbuffers;
579 /* Count the incomplete line on files that don't end with a newline. */
580 if (last->buffer[last->nbytes - 1] != '\n')
582 ++last->nlines;
583 ++total_lines;
586 /* Run through the list, printing lines. First, skip over unneeded
587 buffers. */
588 for (tmp = first; total_lines - tmp->nlines > n_lines; tmp = tmp->next)
589 total_lines -= tmp->nlines;
591 /* Find the correct beginning, then print the rest of the file. */
592 if (total_lines > n_lines)
594 const char *cp;
596 /* Skip `total_lines' - `n_lines' newlines. We made sure that
597 `total_lines' - `n_lines' <= `tmp->nlines'. */
598 cp = tmp->buffer;
599 for (i = total_lines - n_lines; i; --i)
600 while (*cp++ != '\n')
601 /* Do nothing. */ ;
602 i = cp - tmp->buffer;
604 else
605 i = 0;
606 xwrite (STDOUT_FILENO, &tmp->buffer[i], tmp->nbytes - i);
608 for (tmp = tmp->next; tmp; tmp = tmp->next)
609 xwrite (STDOUT_FILENO, tmp->buffer, tmp->nbytes);
611 free_lbuffers:
612 while (first)
614 tmp = first->next;
615 free ((char *) first);
616 first = tmp;
618 return errors;
621 /* Print the last N_BYTES characters from the end of pipe FD.
622 This is a stripped down version of pipe_lines.
623 Return 0 if successful, 1 if an error occurred. */
625 static int
626 pipe_bytes (const char *pretty_filename, int fd, off_t n_bytes)
628 struct charbuffer
630 int nbytes;
631 char buffer[BUFSIZ];
632 struct charbuffer *next;
634 typedef struct charbuffer CBUFFER;
635 CBUFFER *first, *last, *tmp;
636 int i; /* Index into buffers. */
637 int total_bytes = 0; /* Total characters in all buffers. */
638 int errors = 0;
640 first = last = (CBUFFER *) xmalloc (sizeof (CBUFFER));
641 first->nbytes = 0;
642 first->next = NULL;
643 tmp = (CBUFFER *) xmalloc (sizeof (CBUFFER));
645 /* Input is always read into a fresh buffer. */
646 while ((tmp->nbytes = safe_read (fd, tmp->buffer, BUFSIZ)) > 0)
648 tmp->next = NULL;
650 total_bytes += tmp->nbytes;
651 /* If there is enough room in the last buffer read, just append the new
652 one to it. This is because when reading from a pipe, `nbytes' can
653 often be very small. */
654 if (tmp->nbytes + last->nbytes < BUFSIZ)
656 memcpy (&last->buffer[last->nbytes], tmp->buffer, tmp->nbytes);
657 last->nbytes += tmp->nbytes;
659 else
661 /* If there's not enough room, link the new buffer onto the end of
662 the list, then either free up the oldest buffer for the next
663 read if that would leave enough characters, or else malloc a new
664 one. Some compaction mechanism is possible but probably not
665 worthwhile. */
666 last = last->next = tmp;
667 if (total_bytes - first->nbytes > n_bytes)
669 tmp = first;
670 total_bytes -= first->nbytes;
671 first = first->next;
673 else
675 tmp = (CBUFFER *) xmalloc (sizeof (CBUFFER));
679 if (tmp->nbytes == -1)
681 error (0, errno, "%s", pretty_filename);
682 errors = 1;
683 free ((char *) tmp);
684 goto free_cbuffers;
687 free ((char *) tmp);
689 /* Run through the list, printing characters. First, skip over unneeded
690 buffers. */
691 for (tmp = first; total_bytes - tmp->nbytes > n_bytes; tmp = tmp->next)
692 total_bytes -= tmp->nbytes;
694 /* Find the correct beginning, then print the rest of the file.
695 We made sure that `total_bytes' - `n_bytes' <= `tmp->nbytes'. */
696 if (total_bytes > n_bytes)
697 i = total_bytes - n_bytes;
698 else
699 i = 0;
700 xwrite (STDOUT_FILENO, &tmp->buffer[i], tmp->nbytes - i);
702 for (tmp = tmp->next; tmp; tmp = tmp->next)
703 xwrite (STDOUT_FILENO, tmp->buffer, tmp->nbytes);
705 free_cbuffers:
706 while (first)
708 tmp = first->next;
709 free ((char *) first);
710 first = tmp;
712 return errors;
715 /* Skip N_BYTES characters from the start of pipe FD, and print
716 any extra characters that were read beyond that.
717 Return 1 on error, 0 if ok, -1 if EOF. */
719 static int
720 start_bytes (const char *pretty_filename, int fd, off_t n_bytes)
722 char buffer[BUFSIZ];
723 size_t bytes_read = 0;
725 while (0 < n_bytes)
727 bytes_read = safe_read (fd, buffer, BUFSIZ);
728 if (bytes_read == 0)
729 return -1;
730 if (bytes_read == SAFE_READ_ERROR)
732 error (0, errno, "%s", pretty_filename);
733 return 1;
735 n_bytes -= bytes_read;
738 if (n_bytes < 0)
739 xwrite (STDOUT_FILENO, &buffer[bytes_read + n_bytes], -n_bytes);
741 return 0;
744 /* Skip N_LINES lines at the start of file or pipe FD, and print
745 any extra characters that were read beyond that.
746 Return 1 on error, 0 if ok, -1 if EOF. */
748 static int
749 start_lines (const char *pretty_filename, int fd, long int n_lines)
751 char buffer[BUFSIZ];
752 size_t bytes_read = 0;
753 size_t bytes_to_skip = 0;
755 if (n_lines == 0)
756 return 0;
758 while (n_lines)
760 bytes_to_skip = 0;
761 bytes_read = safe_read (fd, buffer, BUFSIZ);
762 if (bytes_read == 0) /* EOF */
763 return -1;
764 if (bytes_read == SAFE_READ_ERROR) /* error */
766 error (0, errno, "%s", pretty_filename);
767 return 1;
770 while (bytes_to_skip < bytes_read)
771 if (buffer[bytes_to_skip++] == '\n' && --n_lines == 0)
772 break;
775 if (bytes_to_skip < bytes_read)
777 xwrite (STDOUT_FILENO, &buffer[bytes_to_skip],
778 bytes_read - bytes_to_skip);
781 return 0;
784 /* FIXME: describe */
786 static void
787 recheck (struct File_spec *f)
789 /* open/fstat the file and announce if dev/ino have changed */
790 struct stat new_stats;
791 int fd;
792 int fail = 0;
793 int is_stdin = (STREQ (f->name, "-"));
794 int was_tailable = f->tailable;
795 int prev_errnum = f->errnum;
796 int new_file;
798 assert (valid_file_spec (f));
800 fd = (is_stdin ? STDIN_FILENO : open (f->name, O_RDONLY));
802 /* If the open fails because the file doesn't exist,
803 then mark the file as not tailable. */
804 f->tailable = !(reopen_inaccessible_files && fd == -1);
806 if (fd == -1 || fstat (fd, &new_stats) < 0)
808 fail = 1;
809 f->errnum = errno;
810 if (!f->tailable)
812 if (was_tailable)
814 /* FIXME-maybe: detect the case in which the file first becomes
815 unreadable (perms), and later becomes readable again and can
816 be seen to be the same file (dev/ino). Otherwise, tail prints
817 the entire contents of the file when it becomes readable. */
818 error (0, f->errnum, _("`%s' has become inaccessible"),
819 pretty_name (f));
821 else
823 /* say nothing... it's still not tailable */
826 else if (prev_errnum != errno)
828 error (0, errno, "%s", pretty_name (f));
831 else if (!IS_TAILABLE_FILE_TYPE (new_stats.st_mode))
833 fail = 1;
834 f->errnum = -1;
835 error (0, 0, _("`%s' has been replaced with an untailable file;\
836 giving up on this name"),
837 pretty_name (f));
838 f->ignore = 1;
840 else
842 f->errnum = 0;
845 new_file = 0;
846 if (fail)
848 close_fd (fd, pretty_name (f));
849 close_fd (f->fd, pretty_name (f));
850 f->fd = -1;
852 else if (prev_errnum && prev_errnum != ENOENT)
854 new_file = 1;
855 assert (f->fd == -1);
856 error (0, 0, _("`%s' has become accessible"), pretty_name (f));
858 else if (f->ino != new_stats.st_ino || f->dev != new_stats.st_dev)
860 new_file = 1;
861 if (f->fd == -1)
863 error (0, 0,
864 _("`%s' has appeared; following end of new file"),
865 pretty_name (f));
867 else
869 /* Close the old one. */
870 close_fd (f->fd, pretty_name (f));
872 /* File has been replaced (e.g., via log rotation) --
873 tail the new one. */
874 error (0, 0,
875 _("`%s' has been replaced; following end of new file"),
876 pretty_name (f));
879 else
881 if (f->fd == -1)
883 /* This happens when one iteration finds the file missing,
884 then the preceding <dev,inode> pair is reused as the
885 file is recreated. */
886 new_file = 1;
888 else
890 close_fd (fd, pretty_name (f));
894 if (new_file)
896 /* Record new file info in f. */
897 f->fd = fd;
898 f->size = 0; /* Start at the beginning of the file... */
899 f->dev = new_stats.st_dev;
900 f->ino = new_stats.st_ino;
901 f->n_unchanged_stats = 0;
902 f->n_consecutive_size_changes = 0;
903 f->ignore = 0;
904 xlseek (f->fd, f->size, SEEK_SET, pretty_name (f));
908 /* FIXME: describe */
910 static unsigned int
911 n_live_files (const struct File_spec *f, int n_files)
913 int i;
914 unsigned int n_live = 0;
916 for (i = 0; i < n_files; i++)
918 if (f[i].fd >= 0)
919 ++n_live;
921 return n_live;
924 /* Tail NFILES files forever, or until killed.
925 The pertinent information for each file is stored in an entry of F.
926 Loop over each of them, doing an fstat to see if they have changed size,
927 and an occasional open/fstat to see if any dev/ino pair has changed.
928 If none of them have changed size in one iteration, sleep for a
929 while and try again. Continue until the user interrupts us. */
931 static void
932 tail_forever (struct File_spec *f, int nfiles, double sleep_interval)
934 int last;
935 int writer_is_dead = 0;
937 last = nfiles - 1;
939 while (1)
941 int i;
942 int any_changed;
944 any_changed = 0;
945 for (i = 0; i < nfiles; i++)
947 struct stat stats;
949 if (f[i].ignore)
950 continue;
952 if (f[i].fd < 0)
954 recheck (&f[i]);
955 continue;
958 if (fstat (f[i].fd, &stats) < 0)
960 f[i].fd = -1;
961 f[i].errnum = errno;
962 error (0, errno, "%s", pretty_name (&f[i]));
963 continue;
966 if (stats.st_size == f[i].size)
968 f[i].n_consecutive_size_changes = 0;
969 if ((max_n_unchanged_stats_between_opens
970 <= f[i].n_unchanged_stats++)
971 && follow_mode == Follow_name)
973 recheck (&f[i]);
974 f[i].n_unchanged_stats = 0;
976 continue;
979 /* Ensure that a file that's unlinked or moved aside, yet always
980 growing will be recognized as having been renamed. */
981 if ((max_n_consecutive_size_changes_between_opens
982 <= f[i].n_consecutive_size_changes++)
983 && follow_mode == Follow_name)
985 f[i].n_consecutive_size_changes = 0;
986 recheck (&f[i]);
987 continue;
990 /* This file has changed size. Print out what we can, and
991 then keep looping. */
993 any_changed = 1;
995 /* reset counter */
996 f[i].n_unchanged_stats = 0;
998 if (stats.st_size < f[i].size)
1000 error (0, 0, _("%s: file truncated"), pretty_name (&f[i]));
1001 last = i;
1002 xlseek (f[i].fd, (off_t) stats.st_size, SEEK_SET,
1003 pretty_name (&f[i]));
1004 f[i].size = stats.st_size;
1005 continue;
1008 if (i != last)
1010 if (print_headers)
1011 write_header (pretty_name (&f[i]));
1012 last = i;
1014 f[i].size += dump_remainder (pretty_name (&f[i]), f[i].fd,
1015 COPY_TO_EOF);
1018 if (n_live_files (f, nfiles) == 0 && ! reopen_inaccessible_files)
1020 error (0, 0, _("no files remaining"));
1021 break;
1024 /* If none of the files changed size, sleep. */
1025 if (!any_changed)
1027 if (writer_is_dead)
1028 break;
1030 if (xnanosleep (sleep_interval))
1031 error (EXIT_FAILURE, errno, _("cannot read realtime clock"));
1033 /* Once the writer is dead, read the files once more to
1034 avoid a race condition. */
1035 writer_is_dead = (pid != 0
1036 && kill (pid, 0) != 0
1037 /* Handle the case in which you cannot send a
1038 signal to the writer, so kill fails and sets
1039 errno to EPERM. */
1040 && errno != EPERM);
1045 /* Output the last N_BYTES bytes of file FILENAME open for reading in FD.
1046 Return 0 if successful, 1 if an error occurred. */
1048 static int
1049 tail_bytes (const char *pretty_filename, int fd, off_t n_bytes)
1051 struct stat stats;
1053 /* We need binary input, since `tail' relies on `lseek' and byte counts,
1054 while binary output will preserve the style (Unix/DOS) of text file. */
1055 SET_BINARY2 (fd, STDOUT_FILENO);
1057 if (fstat (fd, &stats))
1059 error (0, errno, "%s", pretty_filename);
1060 return 1;
1063 if (from_start)
1065 if (S_ISREG (stats.st_mode))
1067 xlseek (fd, n_bytes, SEEK_CUR, pretty_filename);
1069 else
1071 int t;
1072 if ((t = start_bytes (pretty_filename, fd, n_bytes)) < 0)
1073 return 0;
1074 if (t)
1075 return 1;
1077 dump_remainder (pretty_filename, fd, COPY_TO_EOF);
1079 else
1081 if (S_ISREG (stats.st_mode))
1083 off_t current_pos, end_pos;
1084 off_t bytes_remaining;
1086 if ((current_pos = lseek (fd, (off_t) 0, SEEK_CUR)) != -1
1087 && (end_pos = lseek (fd, (off_t) 0, SEEK_END)) != -1)
1089 off_t diff;
1090 /* Be careful here. The current position may actually be
1091 beyond the end of the file. */
1092 bytes_remaining = (diff = end_pos - current_pos) < 0 ? 0 : diff;
1094 else
1096 error (0, errno, "%s", pretty_filename);
1097 return 1;
1100 if (bytes_remaining <= n_bytes)
1102 /* From the current position to end of file, there are no
1103 more bytes than have been requested. So reposition the
1104 file pointer to the incoming current position and print
1105 everything after that. */
1106 xlseek (fd, current_pos, SEEK_SET, pretty_filename);
1108 else
1110 /* There are more bytes remaining than were requested.
1111 Back up. */
1112 xlseek (fd, -n_bytes, SEEK_END, pretty_filename);
1114 dump_remainder (pretty_filename, fd, n_bytes);
1116 else
1117 return pipe_bytes (pretty_filename, fd, n_bytes);
1119 return 0;
1122 /* Output the last N_LINES lines of file FILENAME open for reading in FD.
1123 Return 0 if successful, 1 if an error occurred. */
1125 static int
1126 tail_lines (const char *pretty_filename, int fd, long int n_lines)
1128 struct stat stats;
1130 /* We need binary input, since `tail' relies on `lseek' and byte counts,
1131 while binary output will preserve the style (Unix/DOS) of text file. */
1132 SET_BINARY2 (fd, STDOUT_FILENO);
1134 if (fstat (fd, &stats))
1136 error (0, errno, "%s", pretty_filename);
1137 return 1;
1140 if (from_start)
1142 int t;
1143 if ((t = start_lines (pretty_filename, fd, n_lines)) < 0)
1144 return 0;
1145 if (t)
1146 return 1;
1147 dump_remainder (pretty_filename, fd, COPY_TO_EOF);
1149 else
1151 off_t length;
1152 off_t start_pos;
1154 /* Use file_lines only if FD refers to a regular file for
1155 which lseek (... SEEK_END) works. */
1156 if (S_ISREG (stats.st_mode)
1157 && (start_pos = lseek (fd, (off_t) 0, SEEK_CUR)) != -1
1158 && start_pos < (length = lseek (fd, (off_t) 0, SEEK_END)))
1160 if (length != 0 && file_lines (pretty_filename, fd, n_lines,
1161 start_pos, length))
1162 return 1;
1164 else
1165 return pipe_lines (pretty_filename, fd, n_lines);
1167 return 0;
1170 /* Display the last N_UNITS units of file FILENAME, open for reading
1171 in FD.
1172 Return 0 if successful, 1 if an error occurred. */
1174 static int
1175 tail (const char *pretty_filename, int fd, off_t n_units)
1177 if (count_lines)
1178 return tail_lines (pretty_filename, fd, (long) n_units);
1179 else
1180 return tail_bytes (pretty_filename, fd, n_units);
1183 /* Display the last N_UNITS units of the file described by F.
1184 Return 0 if successful, 1 if an error occurred. */
1186 static int
1187 tail_file (struct File_spec *f, off_t n_units)
1189 int fd, errors;
1191 int is_stdin = (STREQ (f->name, "-"));
1193 if (is_stdin)
1195 have_read_stdin = 1;
1196 fd = STDIN_FILENO;
1198 else
1200 fd = open (f->name, O_RDONLY);
1203 f->tailable = !(reopen_inaccessible_files && fd == -1);
1205 if (fd == -1)
1207 if (forever)
1209 f->fd = -1;
1210 f->errnum = errno;
1211 f->ignore = 0;
1212 f->ino = 0;
1213 f->dev = 0;
1215 error (0, errno, "%s", pretty_name (f));
1216 errors = 1;
1218 else
1220 if (print_headers)
1221 write_header (pretty_name (f));
1222 errors = tail (pretty_name (f), fd, n_units);
1223 if (forever)
1225 struct stat stats;
1227 f->errnum = 0;
1228 if (fstat (fd, &stats) < 0)
1230 errors = 1;
1231 f->errnum = errno;
1232 error (0, errno, "%s", pretty_name (f));
1234 else if (!IS_TAILABLE_FILE_TYPE (stats.st_mode))
1236 error (0, 0, _("%s: cannot follow end of this type of file;\
1237 giving up on this name"),
1238 pretty_name (f));
1239 errors = 1;
1240 f->errnum = -1;
1241 f->ignore = 1;
1244 if (errors)
1246 close_fd (fd, pretty_name (f));
1247 f->fd = -1;
1249 else
1251 f->fd = fd;
1252 f->size = stats.st_size;
1253 f->dev = stats.st_dev;
1254 f->ino = stats.st_ino;
1255 f->n_unchanged_stats = 0;
1256 f->n_consecutive_size_changes = 0;
1257 f->ignore = 0;
1260 else
1262 if (!is_stdin && close (fd))
1264 error (0, errno, "%s", pretty_name (f));
1265 errors = 1;
1270 return errors;
1273 /* If the command line arguments are of the obsolescent form and the
1274 option string is well-formed, set *FAIL to zero, set *N_UNITS, the
1275 globals COUNT_LINES, FOREVER, and FROM_START, and return non-zero.
1276 Otherwise, if the command line arguments appear to be of the
1277 obsolescent form but the option string is malformed, set *FAIL to
1278 non-zero, don't modify any other parameter or global variable, and
1279 return non-zero. Otherwise, return zero and don't modify any parameter
1280 or global variable. */
1282 static int
1283 parse_obsolescent_option (int argc, const char *const *argv,
1284 off_t *n_units, int *fail)
1286 const char *p = argv[1];
1287 const char *n_string = NULL;
1288 const char *n_string_end;
1289 bool obsolete_usage;
1291 int t_from_start;
1292 int t_count_lines;
1293 int t_forever;
1295 /* With the obsolescent form, there is one option string and
1296 (technically) at most one file argument. But we allow two or more
1297 by default. */
1298 if (argc < 2)
1299 return 0;
1301 obsolete_usage = (posix2_version () < 200112);
1303 /* If P starts with `+' and the POSIX version predates 1003.1-2001,
1304 or if P starts with `-N' (where N is a digit), or `-l', then it
1305 is obsolescent. Return zero otherwise. */
1306 if (! ((p[0] == '+' && obsolete_usage)
1307 || (p[0] == '-' && (p[1] == 'l' || ISDIGIT (p[1])))))
1308 return 0;
1310 if (*p == '+')
1311 t_from_start = 1;
1312 else if (*p == '-')
1313 t_from_start = 0;
1314 else
1315 return 0;
1317 ++p;
1318 if (ISDIGIT (*p))
1320 n_string = p;
1323 ++p;
1325 while (ISDIGIT (*p));
1327 n_string_end = p;
1329 t_count_lines = 1;
1330 if (*p == 'c' || *p == 'b')
1332 t_count_lines = 0;
1333 ++p;
1335 else if (*p == 'l')
1337 ++p;
1340 t_forever = 0;
1341 if (*p == 'f')
1343 t_forever = 1;
1344 ++p;
1347 if (*p != '\0')
1349 /* If (argv[1] begins with a `+' or if it begins with `-' followed
1350 by a digit), but has an invalid suffix character, give a diagnostic
1351 and indicate to caller that this *is* of the obsolescent form,
1352 but that it's an invalid option. */
1353 if (t_from_start || n_string)
1355 error (0, 0,
1356 _("%c: invalid suffix character in obsolescent option" ), *p);
1357 *fail = 1;
1358 return 1;
1361 /* Otherwise, it might be a valid non-obsolescent option like -n. */
1362 return 0;
1365 *fail = 0;
1366 if (n_string == NULL)
1367 *n_units = DEFAULT_N_LINES;
1368 else
1370 strtol_error s_err;
1371 uintmax_t tmp;
1372 char *end;
1374 s_err = xstrtoumax (n_string, &end, 10, &tmp,
1375 *n_string_end == 'b' ? "b" : NULL);
1376 if (s_err == LONGINT_OK && tmp <= OFF_T_MAX)
1377 *n_units = (off_t) tmp;
1378 else
1380 /* Extract a NUL-terminated string for the error message. */
1381 size_t len = n_string_end - n_string;
1382 char *n_string_tmp = xmalloc (len + 1);
1384 strncpy (n_string_tmp, n_string, len);
1385 n_string_tmp[len] = '\0';
1387 error (0, 0,
1388 _("%s: %s is so large that it is not representable"),
1389 n_string_tmp, (t_count_lines
1390 ? _("number of lines")
1391 : _("number of bytes")));
1392 free (n_string_tmp);
1393 *fail = 1;
1397 if (!*fail)
1399 if (argc > 3)
1401 /* When POSIXLY_CORRECT is set, enforce the `at most one
1402 file argument' requirement. */
1403 if (getenv ("POSIXLY_CORRECT"))
1405 error (0, 0, _("\
1406 too many arguments; When using tail's obsolescent option syntax (%s)\n\
1407 there may be no more than one file argument. Use the equivalent -n or -c\n\
1408 option instead."), argv[1]);
1409 *fail = 1;
1410 return 1;
1413 #if DISABLED /* FIXME: enable or remove this warning. */
1414 error (0, 0, _("\
1415 Warning: it is not portable to use two or more file arguments with\n\
1416 tail's obsolescent option syntax (%s). Use the equivalent -n or -c\n\
1417 option instead."), argv[1]);
1418 #endif
1421 if (! obsolete_usage)
1423 error (0, 0, _("`%s' option is obsolete; use `%s-%c %.*s'"),
1424 argv[1], t_forever ? " -f" : "", t_count_lines ? 'n' : 'c',
1425 (int) (n_string_end - n_string), n_string);
1426 usage (EXIT_FAILURE);
1429 /* Set globals. */
1430 from_start = t_from_start;
1431 count_lines = t_count_lines;
1432 forever = t_forever;
1435 return 1;
1438 static void
1439 parse_options (int argc, char **argv,
1440 off_t *n_units, enum header_mode *header_mode,
1441 double *sleep_interval)
1443 int c;
1445 count_lines = 1;
1446 forever = from_start = print_headers = 0;
1448 while ((c = getopt_long (argc, argv, "c:n:fFqs:v", long_options, NULL))
1449 != -1)
1451 switch (c)
1453 case 0:
1454 break;
1456 case 'F':
1457 forever = 1;
1458 follow_mode = Follow_name;
1459 reopen_inaccessible_files = 1;
1460 break;
1462 case 'c':
1463 case 'n':
1464 count_lines = (c == 'n');
1465 if (*optarg == '+')
1466 from_start = 1;
1467 else if (*optarg == '-')
1468 ++optarg;
1471 strtol_error s_err;
1472 uintmax_t n;
1473 s_err = xstrtoumax (optarg, NULL, 10, &n, "bkm");
1474 if (s_err != LONGINT_OK)
1476 error (EXIT_FAILURE, 0, "%s: %s", optarg,
1477 (c == 'n'
1478 ? _("invalid number of lines")
1479 : _("invalid number of bytes")));
1482 if (OFF_T_MAX < n)
1483 error (EXIT_FAILURE, 0,
1484 _("%s is larger than the maximum file size on this system"),
1485 optarg);
1486 *n_units = (off_t) n;
1488 break;
1490 case 'f':
1491 case LONG_FOLLOW_OPTION:
1492 forever = 1;
1493 if (optarg == NULL)
1494 follow_mode = DEFAULT_FOLLOW_MODE;
1495 else
1496 follow_mode = XARGMATCH ("--follow", optarg,
1497 follow_mode_string, follow_mode_map);
1498 break;
1500 case RETRY_OPTION:
1501 reopen_inaccessible_files = 1;
1502 break;
1504 case MAX_UNCHANGED_STATS_OPTION:
1505 /* --max-unchanged-stats=N */
1506 if (xstrtoul (optarg, NULL, 10,
1507 &max_n_unchanged_stats_between_opens, "") != LONGINT_OK)
1509 error (EXIT_FAILURE, 0,
1510 _("%s: invalid maximum number of unchanged stats between opens"),
1511 optarg);
1513 break;
1515 case MAX_CONSECUTIVE_SIZE_CHANGES_OPTION:
1516 /* --max-consecutive-size-changes=N */
1517 if (xstrtoul (optarg, NULL, 10,
1518 &max_n_consecutive_size_changes_between_opens, "")
1519 != LONGINT_OK)
1521 error (EXIT_FAILURE, 0,
1522 _("%s: invalid maximum number of consecutive size changes"),
1523 optarg);
1525 break;
1527 case PID_OPTION:
1529 strtol_error s_err;
1530 unsigned long int tmp_ulong;
1531 s_err = xstrtoul (optarg, NULL, 10, &tmp_ulong, "");
1532 if (s_err != LONGINT_OK || tmp_ulong > PID_T_MAX)
1534 error (EXIT_FAILURE, 0, _("%s: invalid PID"), optarg);
1536 pid = tmp_ulong;
1538 break;
1540 case 'q':
1541 *header_mode = never;
1542 break;
1544 case 's':
1546 double s;
1547 if (xstrtod (optarg, NULL, &s) || ! (0 <= s))
1548 error (EXIT_FAILURE, 0,
1549 _("%s: invalid number of seconds"), optarg);
1550 *sleep_interval = s;
1552 break;
1554 case 'v':
1555 *header_mode = always;
1556 break;
1558 case_GETOPT_HELP_CHAR;
1560 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
1562 default:
1563 usage (EXIT_FAILURE);
1567 if (reopen_inaccessible_files && follow_mode != Follow_name)
1568 error (0, 0, _("warning: --retry is useful only when following by name"));
1570 if (pid && !forever)
1571 error (0, 0,
1572 _("warning: PID ignored; --pid=PID is useful only when following"));
1573 else if (pid && kill (pid, 0) != 0 && errno == ENOSYS)
1575 error (0, 0, _("warning: --pid=PID is not supported on this system"));
1576 pid = 0;
1581 main (int argc, char **argv)
1583 enum header_mode header_mode = multiple_files;
1584 int exit_status = 0;
1585 /* If from_start, the number of items to skip before printing; otherwise,
1586 the number of items at the end of the file to print. Although the type
1587 is signed, the value is never negative. */
1588 off_t n_units = DEFAULT_N_LINES;
1589 int n_files;
1590 char **file;
1591 struct File_spec *F;
1592 int i;
1594 /* The number of seconds to sleep between iterations.
1595 During one iteration, every file name or descriptor is checked to
1596 see if it has changed. */
1597 double sleep_interval = 1.0;
1599 program_name = argv[0];
1600 setlocale (LC_ALL, "");
1601 bindtextdomain (PACKAGE, LOCALEDIR);
1602 textdomain (PACKAGE);
1604 atexit (close_stdout);
1606 have_read_stdin = 0;
1609 int fail;
1611 if (parse_obsolescent_option (argc,
1612 (const char *const *) argv,
1613 &n_units, &fail))
1615 if (fail)
1616 exit (EXIT_FAILURE);
1617 optind = 2;
1619 else
1621 parse_options (argc, argv, &n_units, &header_mode, &sleep_interval);
1625 /* To start printing with item N_UNITS from the start of the file, skip
1626 N_UNITS - 1 items. `tail -n +0' is actually meaningless, but for Unix
1627 compatibility it's treated the same as `tail -n +1'. */
1628 if (from_start)
1630 if (n_units)
1631 --n_units;
1634 n_files = argc - optind;
1635 file = argv + optind;
1637 if (n_files == 0)
1639 static char *dummy_stdin = "-";
1640 n_files = 1;
1641 file = &dummy_stdin;
1644 F = (struct File_spec *) xmalloc (n_files * sizeof (F[0]));
1645 for (i = 0; i < n_files; i++)
1646 F[i].name = file[i];
1648 if (header_mode == always
1649 || (header_mode == multiple_files && n_files > 1))
1650 print_headers = 1;
1652 for (i = 0; i < n_files; i++)
1653 exit_status |= tail_file (&F[i], n_units);
1655 if (forever)
1657 /* This fflush appears to be required only on Solaris2.7. */
1658 if (fflush (stdout) < 0)
1659 error (EXIT_FAILURE, errno, _("write error"));
1661 SETVBUF (stdout, NULL, _IONBF, 0);
1662 tail_forever (F, n_files, sleep_interval);
1665 if (have_read_stdin && close (STDIN_FILENO) < 0)
1666 error (EXIT_FAILURE, errno, "-");
1667 exit (exit_status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);