1 /* tail -- output the last part of file(s)
2 Copyright (C) 89, 90, 91, 95, 1996 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software 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
23 -b Tail by N 512-byte blocks.
24 -c, --bytes=N[bkm] Tail by N bytes
25 [or 512-byte blocks, kilobytes, or megabytes].
26 -f, --follow Loop forever trying to read more characters at the
27 end of the file, on the assumption that the file
28 is growing. Ignored if reading from a pipe.
29 -n, --lines=N Tail by N lines.
30 -q, --quiet, --silent Never print filename headers.
31 -v, --verbose Always print filename headers.
33 If a number (N) starts with a `+', begin printing with the Nth item
34 from the start of each file, instead of from the end.
36 Reads from standard input if no files are given or when a filename of
38 By default, filename headers are printed only more than one file
40 By default, prints the last 10 lines (tail -n 10).
42 Original version by Paul Rubin <phr@ocf.berkeley.edu>.
43 Extensions by David MacKenzie <djm@gnu.ai.mit.edu>.
44 tail -f for multiple files by Ian Lance Taylor <ian@airs.com>. */
51 #include <sys/types.h>
65 # define OFF_T_MIN (0 < (off_t) -1 ? (off_t) 0 \
66 : ~ (off_t) 0 << (sizeof (off_t) * CHAR_BIT - 1))
70 # define OFF_T_MAX (~ (off_t) 0 - OFF_T_MIN)
73 /* Disable assertions. Some systems have broken assert macros. */
76 #define XWRITE(fd, buffer, n_bytes) \
80 assert ((n_bytes) >= 0); \
81 if (n_bytes > 0 && fwrite ((buffer), 1, (n_bytes), stdout) == 0) \
82 error (EXIT_FAILURE, errno, _("write error")); \
86 /* Number of items to tail. */
87 #define DEFAULT_N_LINES 10
89 /* Size of atomic reads. */
91 #define BUFSIZ (512 * 8)
94 /* If nonzero, interpret the numeric argument as the number of lines.
95 Otherwise, interpret it as the number of bytes. */
96 static int count_lines;
98 /* If nonzero, read from the end of one file until killed. */
101 /* If nonzero, read from the end of multiple files until killed. */
102 static int forever_multiple;
104 /* Array of file descriptors if forever_multiple is 1. */
105 static int *file_descs;
107 /* Array of file sizes if forever_multiple is 1. */
108 static off_t *file_sizes;
110 /* If nonzero, count from start of file instead of end. */
111 static int from_start;
113 /* If nonzero, print filename headers. */
114 static int print_headers;
116 /* When to print the filename banners. */
119 multiple_files, always, never
125 /* The name this program was run with. */
128 /* Nonzero if we have ever read standard input. */
129 static int have_read_stdin;
131 /* If nonzero, display usage information and exit. */
132 static int show_help;
134 /* If nonzero, print the version on standard output then exit. */
135 static int show_version;
137 static struct option const long_options[] =
139 {"bytes", required_argument, NULL, 'c'},
140 {"follow", no_argument, NULL, 'f'},
141 {"lines", required_argument, NULL, 'n'},
142 {"quiet", no_argument, NULL, 'q'},
143 {"silent", no_argument, NULL, 'q'},
144 {"verbose", no_argument, NULL, 'v'},
145 {"help", no_argument, &show_help, 1},
146 {"version", no_argument, &show_version, 1},
154 fprintf (stderr, _("Try `%s --help' for more information.\n"),
159 Usage: %s [OPTION]... [FILE]...\n\
163 Print last 10 lines of each FILE to standard output.\n\
164 With more than one FILE, precede each with a header giving the file name.\n\
165 With no FILE, or when FILE is -, read standard input.\n\
167 -c, --bytes=N output the last N bytes\n\
168 -f, --follow output appended data as the file grows\n\
169 -n, --lines=N output the last N lines, instead of last 10\n\
170 -q, --quiet, --silent never output headers giving file names\n\
171 -v, --verbose always output headers giving file names\n\
172 --help display this help and exit\n\
173 --version output version information and exit\n\
175 If the first character of N (the number of bytes or lines) is a `+',\n\
176 print beginning with the Nth item from the start of each file, otherwise,\n\
177 print the last N items in the file. N may have a multiplier suffix:\n\
178 b for 512, k for 1024, m for 1048576 (1 Meg). A first OPTION of -VALUE\n\
179 or +VALUE is treated like -n VALUE or -n +VALUE unless VALUE has one of\n\
180 the [bkm] suffix multipliers, in which case it is treated like -c VALUE\n\
183 puts (_("\nReport bugs to textutils-bugs@gnu.ai.mit.edu"));
185 exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
189 write_header (const char *filename, const char *comment)
191 static int first_file = 1;
193 printf ("%s==> %s%s%s <==\n", (first_file ? "" : "\n"), filename,
194 (comment ? ": " : ""),
195 (comment ? comment : ""));
199 /* Print the last N_LINES lines from the end of file FD.
200 Go backward through the file, reading `BUFSIZ' bytes at a time (except
201 probably the first), until we hit the start of the file or have
202 read NUMBER newlines.
203 POS starts out as the length of the file (the offset of the last
204 byte of the file + 1).
205 Return 0 if successful, 1 if an error occurred. */
208 file_lines (const char *filename, int fd, long int n_lines, off_t pos)
212 int i; /* Index into `buffer' for scanning. */
217 /* Set `bytes_read' to the size of the last, probably partial, buffer;
218 0 < `bytes_read' <= `BUFSIZ'. */
219 bytes_read = pos % BUFSIZ;
222 /* Make `pos' a multiple of `BUFSIZ' (0 if the file is short), so that all
223 reads will be on block boundaries, which might increase efficiency. */
225 lseek (fd, pos, SEEK_SET);
226 bytes_read = safe_read (fd, buffer, bytes_read);
227 if (bytes_read == -1)
229 error (0, errno, "%s", filename);
233 /* Count the incomplete line on files that don't end with a newline. */
234 if (bytes_read && buffer[bytes_read - 1] != '\n')
239 /* Scan backward, counting the newlines in this bufferfull. */
240 for (i = bytes_read - 1; i >= 0; i--)
242 /* Have we counted the requested number of newlines yet? */
243 if (buffer[i] == '\n' && n_lines-- == 0)
245 /* If this newline wasn't the last character in the buffer,
246 print the text after it. */
247 if (i != bytes_read - 1)
248 XWRITE (STDOUT_FILENO, &buffer[i + 1], bytes_read - (i + 1));
252 /* Not enough newlines in that bufferfull. */
255 /* Not enough lines in the file; print the entire file. */
256 lseek (fd, (off_t) 0, SEEK_SET);
260 lseek (fd, pos, SEEK_SET);
262 while ((bytes_read = safe_read (fd, buffer, BUFSIZ)) > 0);
263 if (bytes_read == -1)
265 error (0, errno, "%s", filename);
271 /* Print the last N_LINES lines from the end of the standard input,
272 open for reading as pipe FD.
273 Buffer the text as a linked list of LBUFFERs, adding them as needed.
274 Return 0 if successful, 1 if an error occured. */
277 pipe_lines (const char *filename, int fd, long int n_lines)
283 struct linebuffer *next;
285 typedef struct linebuffer LBUFFER;
286 LBUFFER *first, *last, *tmp;
287 int i; /* Index into buffers. */
288 int total_lines = 0; /* Total number of newlines in all buffers. */
291 first = last = (LBUFFER *) xmalloc (sizeof (LBUFFER));
292 first->nbytes = first->nlines = 0;
294 tmp = (LBUFFER *) xmalloc (sizeof (LBUFFER));
296 /* Input is always read into a fresh buffer. */
297 while ((tmp->nbytes = safe_read (fd, tmp->buffer, BUFSIZ)) > 0)
302 /* Count the number of newlines just read. */
303 for (i = 0; i < tmp->nbytes; i++)
304 if (tmp->buffer[i] == '\n')
306 total_lines += tmp->nlines;
308 /* If there is enough room in the last buffer read, just append the new
309 one to it. This is because when reading from a pipe, `nbytes' can
310 often be very small. */
311 if (tmp->nbytes + last->nbytes < BUFSIZ)
313 memcpy (&last->buffer[last->nbytes], tmp->buffer, tmp->nbytes);
314 last->nbytes += tmp->nbytes;
315 last->nlines += tmp->nlines;
319 /* If there's not enough room, link the new buffer onto the end of
320 the list, then either free up the oldest buffer for the next
321 read if that would leave enough lines, or else malloc a new one.
322 Some compaction mechanism is possible but probably not
324 last = last->next = tmp;
325 if (total_lines - first->nlines > n_lines)
328 total_lines -= first->nlines;
332 tmp = (LBUFFER *) xmalloc (sizeof (LBUFFER));
335 if (tmp->nbytes == -1)
337 error (0, errno, "%s", filename);
345 /* This prevents a core dump when the pipe contains no newlines. */
349 /* Count the incomplete line on files that don't end with a newline. */
350 if (last->buffer[last->nbytes - 1] != '\n')
356 /* Run through the list, printing lines. First, skip over unneeded
358 for (tmp = first; total_lines - tmp->nlines > n_lines; tmp = tmp->next)
359 total_lines -= tmp->nlines;
361 /* Find the correct beginning, then print the rest of the file. */
362 if (total_lines > n_lines)
366 /* Skip `total_lines' - `n_lines' newlines. We made sure that
367 `total_lines' - `n_lines' <= `tmp->nlines'. */
369 for (i = total_lines - n_lines; i; --i)
370 while (*cp++ != '\n')
372 i = cp - tmp->buffer;
376 XWRITE (STDOUT_FILENO, &tmp->buffer[i], tmp->nbytes - i);
378 for (tmp = tmp->next; tmp; tmp = tmp->next)
379 XWRITE (STDOUT_FILENO, tmp->buffer, tmp->nbytes);
385 free ((char *) first);
391 /* Print the last N_BYTES characters from the end of pipe FD.
392 This is a stripped down version of pipe_lines.
393 Return 0 if successful, 1 if an error occurred. */
396 pipe_bytes (const char *filename, int fd, off_t n_bytes)
402 struct charbuffer *next;
404 typedef struct charbuffer CBUFFER;
405 CBUFFER *first, *last, *tmp;
406 int i; /* Index into buffers. */
407 int total_bytes = 0; /* Total characters in all buffers. */
410 first = last = (CBUFFER *) xmalloc (sizeof (CBUFFER));
413 tmp = (CBUFFER *) xmalloc (sizeof (CBUFFER));
415 /* Input is always read into a fresh buffer. */
416 while ((tmp->nbytes = safe_read (fd, tmp->buffer, BUFSIZ)) > 0)
420 total_bytes += tmp->nbytes;
421 /* If there is enough room in the last buffer read, just append the new
422 one to it. This is because when reading from a pipe, `nbytes' can
423 often be very small. */
424 if (tmp->nbytes + last->nbytes < BUFSIZ)
426 memcpy (&last->buffer[last->nbytes], tmp->buffer, tmp->nbytes);
427 last->nbytes += tmp->nbytes;
431 /* If there's not enough room, link the new buffer onto the end of
432 the list, then either free up the oldest buffer for the next
433 read if that would leave enough characters, or else malloc a new
434 one. Some compaction mechanism is possible but probably not
436 last = last->next = tmp;
437 if (total_bytes - first->nbytes > n_bytes)
440 total_bytes -= first->nbytes;
445 tmp = (CBUFFER *) xmalloc (sizeof (CBUFFER));
449 if (tmp->nbytes == -1)
451 error (0, errno, "%s", filename);
459 /* Run through the list, printing characters. First, skip over unneeded
461 for (tmp = first; total_bytes - tmp->nbytes > n_bytes; tmp = tmp->next)
462 total_bytes -= tmp->nbytes;
464 /* Find the correct beginning, then print the rest of the file.
465 We made sure that `total_bytes' - `n_bytes' <= `tmp->nbytes'. */
466 if (total_bytes > n_bytes)
467 i = total_bytes - n_bytes;
470 XWRITE (STDOUT_FILENO, &tmp->buffer[i], tmp->nbytes - i);
472 for (tmp = tmp->next; tmp; tmp = tmp->next)
473 XWRITE (STDOUT_FILENO, tmp->buffer, tmp->nbytes);
479 free ((char *) first);
485 /* Skip N_BYTES characters from the start of pipe FD, and print
486 any extra characters that were read beyond that.
487 Return 1 on error, 0 if ok. */
490 start_bytes (const char *filename, int fd, off_t n_bytes)
495 while (n_bytes > 0 && (bytes_read = safe_read (fd, buffer, BUFSIZ)) > 0)
496 n_bytes -= bytes_read;
497 if (bytes_read == -1)
499 error (0, errno, "%s", filename);
502 else if (n_bytes < 0)
503 XWRITE (STDOUT_FILENO, &buffer[bytes_read + n_bytes], -n_bytes);
507 /* Skip N_LINES lines at the start of file or pipe FD, and print
508 any extra characters that were read beyond that.
509 Return 1 on error, 0 if ok. */
512 start_lines (const char *filename, int fd, long int n_lines)
516 int bytes_to_skip = 0;
518 while (n_lines && (bytes_read = safe_read (fd, buffer, BUFSIZ)) > 0)
521 while (bytes_to_skip < bytes_read)
522 if (buffer[bytes_to_skip++] == '\n' && --n_lines == 0)
525 if (bytes_read == -1)
527 error (0, errno, "%s", filename);
530 else if (bytes_to_skip < bytes_read)
532 XWRITE (STDOUT_FILENO, &buffer[bytes_to_skip],
533 bytes_read - bytes_to_skip);
538 /* Display file FILENAME from the current position in FD to the end.
539 If `forever' is nonzero, keep reading from the end of the file
540 until killed. Return the number of bytes read from the file. */
543 dump_remainder (const char *filename, int fd)
551 while ((bytes_read = safe_read (fd, buffer, BUFSIZ)) > 0)
553 XWRITE (STDOUT_FILENO, buffer, bytes_read);
556 if (bytes_read == -1)
557 error (EXIT_FAILURE, errno, "%s", filename);
566 if (forever_multiple)
573 /* Tail NFILES (>1) files forever until killed. The file names are in
574 NAMES. The open file descriptors are in `file_descs', and the size
575 at which we stopped tailing them is in `file_sizes'. We loop over
576 each of them, doing an fstat to see if they have changed size. If
577 none of them have changed size in one iteration, we sleep for a
578 second and try again. We do this until the user interrupts us. */
581 tail_forever (char **names, int nfiles)
593 for (i = 0; i < nfiles; i++)
597 if (file_descs[i] < 0)
599 if (fstat (file_descs[i], &stats) < 0)
601 error (0, errno, "%s", names[i]);
605 if (stats.st_size == file_sizes[i])
608 /* This file has changed size. Print out what we can, and
609 then keep looping. */
613 if (stats.st_size < file_sizes[i])
615 write_header (names[i], _("file truncated"));
617 lseek (file_descs[i], stats.st_size, SEEK_SET);
618 file_sizes[i] = stats.st_size;
625 write_header (names[i], NULL);
628 file_sizes[i] += dump_remainder (names[i], file_descs[i]);
631 /* If none of the files changed size, sleep. */
637 /* Output the last N_BYTES bytes of file FILENAME open for reading in FD.
638 Return 0 if successful, 1 if an error occurred. */
641 tail_bytes (const char *filename, int fd, off_t n_bytes)
645 /* FIXME: resolve this like in dd.c. */
646 /* Use fstat instead of checking for errno == ESPIPE because
647 lseek doesn't work on some special files but doesn't return an
649 if (fstat (fd, &stats))
651 error (0, errno, "%s", filename);
657 if (S_ISREG (stats.st_mode))
658 lseek (fd, n_bytes, SEEK_CUR);
659 else if (start_bytes (filename, fd, n_bytes))
661 dump_remainder (filename, fd);
665 if (S_ISREG (stats.st_mode))
667 off_t current_pos, end_pos;
668 size_t bytes_remaining;
670 if ((current_pos = lseek (fd, (off_t) 0, SEEK_CUR)) != -1
671 && (end_pos = lseek (fd, (off_t) 0, SEEK_END)) != -1)
674 /* Be careful here. The current position may actually be
675 beyond the end of the file. */
676 bytes_remaining = (diff = end_pos - current_pos) < 0 ? 0 : diff;
680 error (0, errno, "%s", filename);
684 if (bytes_remaining <= n_bytes)
686 /* From the current position to end of file, there are no
687 more bytes than have been requested. So reposition the
688 file pointer to the incoming current position and print
689 everything after that. */
690 lseek (fd, current_pos, SEEK_SET);
694 /* There are more bytes remaining than were requested.
696 lseek (fd, -n_bytes, SEEK_END);
698 dump_remainder (filename, fd);
701 return pipe_bytes (filename, fd, n_bytes);
706 /* Output the last N_LINES lines of file FILENAME open for reading in FD.
707 Return 0 if successful, 1 if an error occurred. */
710 tail_lines (const char *filename, int fd, long int n_lines)
715 if (fstat (fd, &stats))
717 error (0, errno, "%s", filename);
723 if (start_lines (filename, fd, n_lines))
725 dump_remainder (filename, fd);
729 /* Use file_lines only if FD refers to a regular file with
730 its file pointer positioned at beginning of file. */
731 /* FIXME: adding the lseek conjunct is a kludge.
732 Once there's a reasonable test suite, fix the true culprit:
733 file_lines. file_lines shouldn't presume that the input
734 file pointer is initially positioned to beginning of file. */
735 if (S_ISREG (stats.st_mode)
736 && lseek (fd, (off_t) 0, SEEK_CUR) == (off_t) 0)
738 length = lseek (fd, (off_t) 0, SEEK_END);
739 if (length != 0 && file_lines (filename, fd, n_lines, length))
741 dump_remainder (filename, fd);
744 return pipe_lines (filename, fd, n_lines);
749 /* Display the last N_UNITS units of file FILENAME, open for reading
751 Return 0 if successful, 1 if an error occurred. */
754 tail (const char *filename, int fd, off_t n_units)
757 return tail_lines (filename, fd, (long) n_units);
759 return tail_bytes (filename, fd, n_units);
762 /* Display the last N_UNITS units of file FILENAME.
763 "-" for FILENAME means the standard input.
764 FILENUM is this file's index in the list of files the user gave.
765 Return 0 if successful, 1 if an error occurred. */
768 tail_file (const char *filename, off_t n_units, int filenum)
773 if (!strcmp (filename, "-"))
776 filename = _("standard input");
778 write_header (filename, NULL);
779 errors = tail (filename, 0, n_units);
780 if (forever_multiple)
782 if (fstat (0, &stats) < 0)
784 error (0, errno, _("standard input"));
787 else if (!S_ISREG (stats.st_mode))
790 _("standard input: cannot follow end of non-regular file"));
794 file_descs[filenum] = -1;
797 file_descs[filenum] = 0;
798 file_sizes[filenum] = stats.st_size;
804 /* Not standard input. */
805 fd = open (filename, O_RDONLY);
808 if (forever_multiple)
809 file_descs[filenum] = -1;
810 error (0, errno, "%s", filename);
816 write_header (filename, NULL);
817 errors = tail (filename, fd, n_units);
818 if (forever_multiple)
820 if (fstat (fd, &stats) < 0)
822 error (0, errno, "%s", filename);
825 else if (!S_ISREG (stats.st_mode))
827 error (0, 0, _("%s: cannot follow end of non-regular file"),
834 file_descs[filenum] = -1;
838 file_descs[filenum] = fd;
839 file_sizes[filenum] = stats.st_size;
846 error (0, errno, "%s", filename);
856 /* If the command line arguments are of the obsolescent form and the
857 option string is well-formed, set *FAIL to zero, set *N_UNITS, the
858 globals COUNT_LINES, FOREVER, and FROM_START, and return non-zero.
859 Otherwise, if the command line arguments appear to be of the
860 obsolescent form but the option string is malformed, set *FAIL to
861 non-zero, don't modify any other parameter or global variable, and
862 return non-zero. Otherwise, return zero and don't modify any parameter
863 or global variable. */
866 parse_obsolescent_option (int argc, const char *const *argv,
867 off_t *n_units, int *fail)
869 const char *p = argv[1];
870 const char *n_string = NULL;
871 const char *n_string_end;
877 /* With the obsolescent form, there is one option string and at most
878 one file argument. */
879 if (argc < 2 || argc > 3)
882 /* If I were implementing this in Perl, the rest of this function
883 would be essentially this single statement:
884 return $p ne '-' && $p ne '-c' && $p =~ /^[+-]\d*[cl]?f?$/; */
887 if (STREQ (p, "-") || STREQ (p, "-c"))
888 but without using strcmp. */
889 if (p[0] == '-' && (p[1] == 0 || (p[1] == 'c' && p[2] == 0)))
907 while (ISDIGIT (*p));
931 /* If (argv[1] begins with a `+' or if it begins with `-' followed
932 by a digit), but has an invalid suffix character, give a diagnostic
933 and indicate to caller that this *is* of the obsolescent form,
934 but that it's an invalid option. */
935 if (t_from_start || n_string)
938 _("%c: invalid suffix character in obsolescent option" ), *p);
943 /* Otherwise, it might be a valid non-obsolescent option like -n. */
948 if (n_string == NULL)
949 *n_units = DEFAULT_N_LINES;
953 unsigned long int tmp_ulong;
955 s_err = xstrtoul (n_string, &end, 0, &tmp_ulong, NULL);
956 if (s_err == LONGINT_OK && tmp_ulong <= OFF_T_MAX)
957 *n_units = (off_t) tmp_ulong;
960 /* Extract a NUL-terminated string for the error message. */
961 size_t len = n_string_end - n_string;
962 char *n_string_tmp = xmalloc (len + 1);
964 strncpy (n_string_tmp, n_string, len);
965 n_string_tmp[len] = '\0';
968 _("%s: `%s' is so large that it is not representable"),
969 count_lines ? _("number of lines") : _("number of bytes"),
979 from_start = t_from_start;
980 count_lines = t_count_lines;
988 parse_options (int argc, char **argv,
989 off_t *n_units, enum header_mode *header_mode)
994 forever = forever_multiple = from_start = print_headers = 0;
996 while ((c = getopt_long (argc, argv, "c:n:fqv", long_options, NULL)) != -1)
1005 count_lines = (c == 'n');
1008 else if (*optarg == '-')
1013 unsigned long int tmp_ulong;
1014 s_err = xstrtoul (optarg, NULL, 0, &tmp_ulong, "bkm");
1015 if (s_err == LONGINT_INVALID)
1017 error (EXIT_FAILURE, 0, "%s: %s", optarg,
1019 ? _("invalid number of lines")
1020 : _("invalid number of bytes")));
1022 if (s_err != LONGINT_OK || tmp_ulong > OFF_T_MAX)
1024 error (EXIT_FAILURE, 0,
1025 _("%s: `%s' is so large that it is not representable"),
1027 c == 'n' ? _("number of lines") : _("number of bytes"));
1029 *n_units = (off_t) tmp_ulong;
1038 *header_mode = never;
1042 *header_mode = always;
1052 main (int argc, char **argv)
1054 enum header_mode header_mode = multiple_files;
1055 int exit_status = 0;
1056 /* If from_start, the number of items to skip before printing; otherwise,
1057 the number of items at the end of the file to print. Although the type
1058 is signed, the value is never negative. */
1059 off_t n_units = DEFAULT_N_LINES;
1063 program_name = argv[0];
1064 setlocale (LC_ALL, "");
1065 bindtextdomain (PACKAGE, LOCALEDIR);
1066 textdomain (PACKAGE);
1068 have_read_stdin = 0;
1071 int found_obsolescent;
1073 found_obsolescent = parse_obsolescent_option (argc,
1074 (const char *const *) argv,
1076 if (found_obsolescent)
1079 exit (EXIT_FAILURE);
1084 parse_options (argc, argv, &n_units, &header_mode);
1090 printf ("tail (%s) %s\n", GNU_PACKAGE, VERSION);
1091 exit (EXIT_SUCCESS);
1097 /* To start printing with item N_UNITS from the start of the file, skip
1098 N_UNITS - 1 items. `tail +0' is actually meaningless, but for Unix
1099 compatibility it's treated the same as `tail +1'. */
1106 n_files = argc - optind;
1107 file = argv + optind;
1109 if (n_files > 1 && forever)
1111 forever_multiple = 1;
1113 file_descs = (int *) xmalloc (n_files * sizeof (int));
1114 file_sizes = (off_t *) xmalloc (n_files * sizeof (off_t));
1117 if (header_mode == always
1118 || (header_mode == multiple_files && n_files > 1))
1123 exit_status |= tail_file ("-", n_units, 0);
1128 for (i = 0; i < n_files; i++)
1129 exit_status |= tail_file (file[i], n_units, i);
1131 if (forever_multiple)
1132 tail_forever (file, n_files);
1135 if (have_read_stdin && close (0) < 0)
1136 error (EXIT_FAILURE, errno, "-");
1137 if (fclose (stdout) == EOF)
1138 error (EXIT_FAILURE, errno, _("write error"));
1139 exit (exit_status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);