1 /* tail -- output the last part of file(s)
2 Copyright (C) 89, 90, 91, 95, 96, 1997, 1998 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>
58 # define OFF_T_MIN TYPE_MINIMUM (off_t)
62 # define OFF_T_MAX TYPE_MAXIMUM (off_t)
65 /* Disable assertions. Some systems have broken assert macros. */
68 #define XWRITE(fd, buffer, n_bytes) \
72 assert ((n_bytes) >= 0); \
73 if (n_bytes > 0 && fwrite ((buffer), 1, (n_bytes), stdout) == 0) \
74 error (EXIT_FAILURE, errno, _("write error")); \
78 /* Number of items to tail. */
79 #define DEFAULT_N_LINES 10
81 /* Size of atomic reads. */
83 # define BUFSIZ (512 * 8)
86 /* If nonzero, interpret the numeric argument as the number of lines.
87 Otherwise, interpret it as the number of bytes. */
88 static int count_lines
;
90 /* If nonzero, read from the end of one file until killed. */
93 /* If nonzero, read from the end of multiple files until killed. */
94 static int forever_multiple
;
96 /* Array of file descriptors if forever_multiple is 1. */
97 static int *file_descs
;
99 /* Array of file sizes if forever_multiple is 1. */
100 static off_t
*file_sizes
;
102 /* If nonzero, count from start of file instead of end. */
103 static int from_start
;
105 /* If nonzero, print filename headers. */
106 static int print_headers
;
108 /* When to print the filename banners. */
111 multiple_files
, always
, never
116 /* The name this program was run with. */
119 /* Nonzero if we have ever read standard input. */
120 static int have_read_stdin
;
122 /* If nonzero, display usage information and exit. */
123 static int show_help
;
125 /* If nonzero, print the version on standard output then exit. */
126 static int show_version
;
128 static struct option
const long_options
[] =
130 {"bytes", required_argument
, NULL
, 'c'},
131 {"follow", no_argument
, NULL
, 'f'},
132 {"lines", required_argument
, NULL
, 'n'},
133 {"quiet", no_argument
, NULL
, 'q'},
134 {"silent", no_argument
, NULL
, 'q'},
135 {"verbose", no_argument
, NULL
, 'v'},
136 {"help", no_argument
, &show_help
, 1},
137 {"version", no_argument
, &show_version
, 1},
145 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
150 Usage: %s [OPTION]... [FILE]...\n\
154 Print last 10 lines of each FILE to standard output.\n\
155 With more than one FILE, precede each with a header giving the file name.\n\
156 With no FILE, or when FILE is -, read standard input.\n\
158 -c, --bytes=N output the last N bytes\n\
159 -f, --follow output appended data as the file grows\n\
160 -n, --lines=N output the last N lines, instead of last 10\n\
161 -q, --quiet, --silent never output headers giving file names\n\
162 -v, --verbose always output headers giving file names\n\
163 --help display this help and exit\n\
164 --version output version information and exit\n\
166 If the first character of N (the number of bytes or lines) is a `+',\n\
167 print beginning with the Nth item from the start of each file, otherwise,\n\
168 print the last N items in the file. N may have a multiplier suffix:\n\
169 b for 512, k for 1024, m for 1048576 (1 Meg). A first OPTION of -VALUE\n\
170 or +VALUE is treated like -n VALUE or -n +VALUE unless VALUE has one of\n\
171 the [bkm] suffix multipliers, in which case it is treated like -c VALUE\n\
174 puts (_("\nReport bugs to <textutils-bugs@gnu.org>."));
176 exit (status
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
);
180 write_header (const char *filename
, const char *comment
)
182 static int first_file
= 1;
184 printf ("%s==> %s%s%s <==\n", (first_file
? "" : "\n"), filename
,
185 (comment
? ": " : ""),
186 (comment
? comment
: ""));
190 /* Print the last N_LINES lines from the end of file FD.
191 Go backward through the file, reading `BUFSIZ' bytes at a time (except
192 probably the first), until we hit the start of the file or have
193 read NUMBER newlines.
194 POS starts out as the length of the file (the offset of the last
195 byte of the file + 1).
196 Return 0 if successful, 1 if an error occurred. */
199 file_lines (const char *filename
, int fd
, long int n_lines
, off_t pos
)
203 int i
; /* Index into `buffer' for scanning. */
208 /* Set `bytes_read' to the size of the last, probably partial, buffer;
209 0 < `bytes_read' <= `BUFSIZ'. */
210 bytes_read
= pos
% BUFSIZ
;
213 /* Make `pos' a multiple of `BUFSIZ' (0 if the file is short), so that all
214 reads will be on block boundaries, which might increase efficiency. */
216 lseek (fd
, pos
, SEEK_SET
);
217 bytes_read
= safe_read (fd
, buffer
, bytes_read
);
218 if (bytes_read
== -1)
220 error (0, errno
, "%s", filename
);
224 /* Count the incomplete line on files that don't end with a newline. */
225 if (bytes_read
&& buffer
[bytes_read
- 1] != '\n')
230 /* Scan backward, counting the newlines in this bufferfull. */
231 for (i
= bytes_read
- 1; i
>= 0; i
--)
233 /* Have we counted the requested number of newlines yet? */
234 if (buffer
[i
] == '\n' && n_lines
-- == 0)
236 /* If this newline wasn't the last character in the buffer,
237 print the text after it. */
238 if (i
!= bytes_read
- 1)
239 XWRITE (STDOUT_FILENO
, &buffer
[i
+ 1], bytes_read
- (i
+ 1));
243 /* Not enough newlines in that bufferfull. */
246 /* Not enough lines in the file; print the entire file. */
247 lseek (fd
, (off_t
) 0, SEEK_SET
);
251 lseek (fd
, pos
, SEEK_SET
);
253 while ((bytes_read
= safe_read (fd
, buffer
, BUFSIZ
)) > 0);
254 if (bytes_read
== -1)
256 error (0, errno
, "%s", filename
);
262 /* Print the last N_LINES lines from the end of the standard input,
263 open for reading as pipe FD.
264 Buffer the text as a linked list of LBUFFERs, adding them as needed.
265 Return 0 if successful, 1 if an error occured. */
268 pipe_lines (const char *filename
, int fd
, long int n_lines
)
274 struct linebuffer
*next
;
276 typedef struct linebuffer LBUFFER
;
277 LBUFFER
*first
, *last
, *tmp
;
278 int i
; /* Index into buffers. */
279 int total_lines
= 0; /* Total number of newlines in all buffers. */
282 first
= last
= (LBUFFER
*) xmalloc (sizeof (LBUFFER
));
283 first
->nbytes
= first
->nlines
= 0;
285 tmp
= (LBUFFER
*) xmalloc (sizeof (LBUFFER
));
287 /* Input is always read into a fresh buffer. */
288 while ((tmp
->nbytes
= safe_read (fd
, tmp
->buffer
, BUFSIZ
)) > 0)
293 /* Count the number of newlines just read. */
294 for (i
= 0; i
< tmp
->nbytes
; i
++)
295 if (tmp
->buffer
[i
] == '\n')
297 total_lines
+= tmp
->nlines
;
299 /* If there is enough room in the last buffer read, just append the new
300 one to it. This is because when reading from a pipe, `nbytes' can
301 often be very small. */
302 if (tmp
->nbytes
+ last
->nbytes
< BUFSIZ
)
304 memcpy (&last
->buffer
[last
->nbytes
], tmp
->buffer
, tmp
->nbytes
);
305 last
->nbytes
+= tmp
->nbytes
;
306 last
->nlines
+= tmp
->nlines
;
310 /* If there's not enough room, link the new buffer onto the end of
311 the list, then either free up the oldest buffer for the next
312 read if that would leave enough lines, or else malloc a new one.
313 Some compaction mechanism is possible but probably not
315 last
= last
->next
= tmp
;
316 if (total_lines
- first
->nlines
> n_lines
)
319 total_lines
-= first
->nlines
;
323 tmp
= (LBUFFER
*) xmalloc (sizeof (LBUFFER
));
326 if (tmp
->nbytes
== -1)
328 error (0, errno
, "%s", filename
);
336 /* This prevents a core dump when the pipe contains no newlines. */
340 /* Count the incomplete line on files that don't end with a newline. */
341 if (last
->buffer
[last
->nbytes
- 1] != '\n')
347 /* Run through the list, printing lines. First, skip over unneeded
349 for (tmp
= first
; total_lines
- tmp
->nlines
> n_lines
; tmp
= tmp
->next
)
350 total_lines
-= tmp
->nlines
;
352 /* Find the correct beginning, then print the rest of the file. */
353 if (total_lines
> n_lines
)
357 /* Skip `total_lines' - `n_lines' newlines. We made sure that
358 `total_lines' - `n_lines' <= `tmp->nlines'. */
360 for (i
= total_lines
- n_lines
; i
; --i
)
361 while (*cp
++ != '\n')
363 i
= cp
- tmp
->buffer
;
367 XWRITE (STDOUT_FILENO
, &tmp
->buffer
[i
], tmp
->nbytes
- i
);
369 for (tmp
= tmp
->next
; tmp
; tmp
= tmp
->next
)
370 XWRITE (STDOUT_FILENO
, tmp
->buffer
, tmp
->nbytes
);
376 free ((char *) first
);
382 /* Print the last N_BYTES characters from the end of pipe FD.
383 This is a stripped down version of pipe_lines.
384 Return 0 if successful, 1 if an error occurred. */
387 pipe_bytes (const char *filename
, int fd
, off_t n_bytes
)
393 struct charbuffer
*next
;
395 typedef struct charbuffer CBUFFER
;
396 CBUFFER
*first
, *last
, *tmp
;
397 int i
; /* Index into buffers. */
398 int total_bytes
= 0; /* Total characters in all buffers. */
401 first
= last
= (CBUFFER
*) xmalloc (sizeof (CBUFFER
));
404 tmp
= (CBUFFER
*) xmalloc (sizeof (CBUFFER
));
406 /* Input is always read into a fresh buffer. */
407 while ((tmp
->nbytes
= safe_read (fd
, tmp
->buffer
, BUFSIZ
)) > 0)
411 total_bytes
+= tmp
->nbytes
;
412 /* If there is enough room in the last buffer read, just append the new
413 one to it. This is because when reading from a pipe, `nbytes' can
414 often be very small. */
415 if (tmp
->nbytes
+ last
->nbytes
< BUFSIZ
)
417 memcpy (&last
->buffer
[last
->nbytes
], tmp
->buffer
, tmp
->nbytes
);
418 last
->nbytes
+= tmp
->nbytes
;
422 /* If there's not enough room, link the new buffer onto the end of
423 the list, then either free up the oldest buffer for the next
424 read if that would leave enough characters, or else malloc a new
425 one. Some compaction mechanism is possible but probably not
427 last
= last
->next
= tmp
;
428 if (total_bytes
- first
->nbytes
> n_bytes
)
431 total_bytes
-= first
->nbytes
;
436 tmp
= (CBUFFER
*) xmalloc (sizeof (CBUFFER
));
440 if (tmp
->nbytes
== -1)
442 error (0, errno
, "%s", filename
);
450 /* Run through the list, printing characters. First, skip over unneeded
452 for (tmp
= first
; total_bytes
- tmp
->nbytes
> n_bytes
; tmp
= tmp
->next
)
453 total_bytes
-= tmp
->nbytes
;
455 /* Find the correct beginning, then print the rest of the file.
456 We made sure that `total_bytes' - `n_bytes' <= `tmp->nbytes'. */
457 if (total_bytes
> n_bytes
)
458 i
= total_bytes
- n_bytes
;
461 XWRITE (STDOUT_FILENO
, &tmp
->buffer
[i
], tmp
->nbytes
- i
);
463 for (tmp
= tmp
->next
; tmp
; tmp
= tmp
->next
)
464 XWRITE (STDOUT_FILENO
, tmp
->buffer
, tmp
->nbytes
);
470 free ((char *) first
);
476 /* Skip N_BYTES characters from the start of pipe FD, and print
477 any extra characters that were read beyond that.
478 Return 1 on error, 0 if ok. */
481 start_bytes (const char *filename
, int fd
, off_t n_bytes
)
486 while (n_bytes
> 0 && (bytes_read
= safe_read (fd
, buffer
, BUFSIZ
)) > 0)
487 n_bytes
-= bytes_read
;
488 if (bytes_read
== -1)
490 error (0, errno
, "%s", filename
);
493 else if (n_bytes
< 0)
494 XWRITE (STDOUT_FILENO
, &buffer
[bytes_read
+ n_bytes
], -n_bytes
);
498 /* Skip N_LINES lines at the start of file or pipe FD, and print
499 any extra characters that were read beyond that.
500 Return 1 on error, 0 if ok. */
503 start_lines (const char *filename
, int fd
, long int n_lines
)
507 int bytes_to_skip
= 0;
509 while (n_lines
&& (bytes_read
= safe_read (fd
, buffer
, BUFSIZ
)) > 0)
512 while (bytes_to_skip
< bytes_read
)
513 if (buffer
[bytes_to_skip
++] == '\n' && --n_lines
== 0)
516 if (bytes_read
== -1)
518 error (0, errno
, "%s", filename
);
521 else if (bytes_to_skip
< bytes_read
)
523 XWRITE (STDOUT_FILENO
, &buffer
[bytes_to_skip
],
524 bytes_read
- bytes_to_skip
);
529 /* Display file FILENAME from the current position in FD to the end.
530 If `forever' is nonzero, keep reading from the end of the file
531 until killed. Return the number of bytes read from the file. */
534 dump_remainder (const char *filename
, int fd
)
542 while ((bytes_read
= safe_read (fd
, buffer
, BUFSIZ
)) > 0)
544 XWRITE (STDOUT_FILENO
, buffer
, bytes_read
);
547 if (bytes_read
== -1)
548 error (EXIT_FAILURE
, errno
, "%s", filename
);
557 if (forever_multiple
)
564 /* Tail NFILES (>1) files forever until killed. The file names are in
565 NAMES. The open file descriptors are in `file_descs', and the size
566 at which we stopped tailing them is in `file_sizes'. We loop over
567 each of them, doing an fstat to see if they have changed size. If
568 none of them have changed size in one iteration, we sleep for a
569 second and try again. We do this until the user interrupts us. */
572 tail_forever (char **names
, int nfiles
)
584 for (i
= 0; i
< nfiles
; i
++)
588 if (file_descs
[i
] < 0)
590 if (fstat (file_descs
[i
], &stats
) < 0)
592 error (0, errno
, "%s", names
[i
]);
596 if (stats
.st_size
== file_sizes
[i
])
599 /* This file has changed size. Print out what we can, and
600 then keep looping. */
604 if (stats
.st_size
< file_sizes
[i
])
606 write_header (names
[i
], _("file truncated"));
608 lseek (file_descs
[i
], stats
.st_size
, SEEK_SET
);
609 file_sizes
[i
] = stats
.st_size
;
616 write_header (names
[i
], NULL
);
619 file_sizes
[i
] += dump_remainder (names
[i
], file_descs
[i
]);
622 /* If none of the files changed size, sleep. */
628 /* Output the last N_BYTES bytes of file FILENAME open for reading in FD.
629 Return 0 if successful, 1 if an error occurred. */
632 tail_bytes (const char *filename
, int fd
, off_t n_bytes
)
636 /* FIXME: resolve this like in dd.c. */
637 /* Use fstat instead of checking for errno == ESPIPE because
638 lseek doesn't work on some special files but doesn't return an
640 if (fstat (fd
, &stats
))
642 error (0, errno
, "%s", filename
);
648 if (S_ISREG (stats
.st_mode
))
649 lseek (fd
, n_bytes
, SEEK_CUR
);
650 else if (start_bytes (filename
, fd
, n_bytes
))
652 dump_remainder (filename
, fd
);
656 if (S_ISREG (stats
.st_mode
))
658 off_t current_pos
, end_pos
;
659 size_t bytes_remaining
;
661 if ((current_pos
= lseek (fd
, (off_t
) 0, SEEK_CUR
)) != -1
662 && (end_pos
= lseek (fd
, (off_t
) 0, SEEK_END
)) != -1)
665 /* Be careful here. The current position may actually be
666 beyond the end of the file. */
667 bytes_remaining
= (diff
= end_pos
- current_pos
) < 0 ? 0 : diff
;
671 error (0, errno
, "%s", filename
);
675 if (bytes_remaining
<= n_bytes
)
677 /* From the current position to end of file, there are no
678 more bytes than have been requested. So reposition the
679 file pointer to the incoming current position and print
680 everything after that. */
681 lseek (fd
, current_pos
, SEEK_SET
);
685 /* There are more bytes remaining than were requested.
687 lseek (fd
, -n_bytes
, SEEK_END
);
689 dump_remainder (filename
, fd
);
692 return pipe_bytes (filename
, fd
, n_bytes
);
697 /* Output the last N_LINES lines of file FILENAME open for reading in FD.
698 Return 0 if successful, 1 if an error occurred. */
701 tail_lines (const char *filename
, int fd
, long int n_lines
)
706 if (fstat (fd
, &stats
))
708 error (0, errno
, "%s", filename
);
714 if (start_lines (filename
, fd
, n_lines
))
716 dump_remainder (filename
, fd
);
720 /* Use file_lines only if FD refers to a regular file with
721 its file pointer positioned at beginning of file. */
722 /* FIXME: adding the lseek conjunct is a kludge.
723 Once there's a reasonable test suite, fix the true culprit:
724 file_lines. file_lines shouldn't presume that the input
725 file pointer is initially positioned to beginning of file. */
726 if (S_ISREG (stats
.st_mode
)
727 && lseek (fd
, (off_t
) 0, SEEK_CUR
) == (off_t
) 0)
729 length
= lseek (fd
, (off_t
) 0, SEEK_END
);
730 if (length
!= 0 && file_lines (filename
, fd
, n_lines
, length
))
732 dump_remainder (filename
, fd
);
735 return pipe_lines (filename
, fd
, n_lines
);
740 /* Display the last N_UNITS units of file FILENAME, open for reading
742 Return 0 if successful, 1 if an error occurred. */
745 tail (const char *filename
, int fd
, off_t n_units
)
748 return tail_lines (filename
, fd
, (long) n_units
);
750 return tail_bytes (filename
, fd
, n_units
);
753 /* Display the last N_UNITS units of file FILENAME.
754 "-" for FILENAME means the standard input.
755 FILENUM is this file's index in the list of files the user gave.
756 Return 0 if successful, 1 if an error occurred. */
759 tail_file (const char *filename
, off_t n_units
, int filenum
)
764 if (!strcmp (filename
, "-"))
767 filename
= _("standard input");
769 write_header (filename
, NULL
);
770 errors
= tail (filename
, 0, n_units
);
771 if (forever_multiple
)
773 if (fstat (0, &stats
) < 0)
775 error (0, errno
, _("standard input"));
778 else if (!S_ISREG (stats
.st_mode
))
781 _("standard input: cannot follow end of non-regular file"));
785 file_descs
[filenum
] = -1;
788 file_descs
[filenum
] = 0;
789 file_sizes
[filenum
] = stats
.st_size
;
795 /* Not standard input. */
796 fd
= open (filename
, O_RDONLY
);
799 if (forever_multiple
)
800 file_descs
[filenum
] = -1;
801 error (0, errno
, "%s", filename
);
807 write_header (filename
, NULL
);
808 errors
= tail (filename
, fd
, n_units
);
809 if (forever_multiple
)
811 if (fstat (fd
, &stats
) < 0)
813 error (0, errno
, "%s", filename
);
816 else if (!S_ISREG (stats
.st_mode
))
818 error (0, 0, _("%s: cannot follow end of non-regular file"),
825 file_descs
[filenum
] = -1;
829 file_descs
[filenum
] = fd
;
830 file_sizes
[filenum
] = stats
.st_size
;
837 error (0, errno
, "%s", filename
);
847 /* If the command line arguments are of the obsolescent form and the
848 option string is well-formed, set *FAIL to zero, set *N_UNITS, the
849 globals COUNT_LINES, FOREVER, and FROM_START, and return non-zero.
850 Otherwise, if the command line arguments appear to be of the
851 obsolescent form but the option string is malformed, set *FAIL to
852 non-zero, don't modify any other parameter or global variable, and
853 return non-zero. Otherwise, return zero and don't modify any parameter
854 or global variable. */
857 parse_obsolescent_option (int argc
, const char *const *argv
,
858 off_t
*n_units
, int *fail
)
860 const char *p
= argv
[1];
861 const char *n_string
= NULL
;
862 const char *n_string_end
;
868 /* With the obsolescent form, there is one option string and
869 (technically) at most one file argument. But we allow two or more
874 /* If I were implementing this in Perl, the rest of this function
875 would be essentially this single statement:
876 return $p ne '-' && $p ne '-c' && $p =~ /^[+-]\d*[cl]?f?$/; */
879 if (STREQ (p, "-") || STREQ (p, "-c"))
880 but without using strcmp. */
881 if (p
[0] == '-' && (p
[1] == 0 || (p
[1] == 'c' && p
[2] == 0)))
899 while (ISDIGIT (*p
));
923 /* If (argv[1] begins with a `+' or if it begins with `-' followed
924 by a digit), but has an invalid suffix character, give a diagnostic
925 and indicate to caller that this *is* of the obsolescent form,
926 but that it's an invalid option. */
927 if (t_from_start
|| n_string
)
930 _("%c: invalid suffix character in obsolescent option" ), *p
);
935 /* Otherwise, it might be a valid non-obsolescent option like -n. */
940 if (n_string
== NULL
)
941 *n_units
= DEFAULT_N_LINES
;
945 unsigned long int tmp_ulong
;
947 s_err
= xstrtoul (n_string
, &end
, 0, &tmp_ulong
, NULL
);
948 if (s_err
== LONGINT_OK
&& tmp_ulong
<= OFF_T_MAX
)
949 *n_units
= (off_t
) tmp_ulong
;
952 /* Extract a NUL-terminated string for the error message. */
953 size_t len
= n_string_end
- n_string
;
954 char *n_string_tmp
= xmalloc (len
+ 1);
956 strncpy (n_string_tmp
, n_string
, len
);
957 n_string_tmp
[len
] = '\0';
960 _("%s: %s is so large that it is not representable"),
961 n_string_tmp
, (count_lines
962 ? _("number of lines")
963 : _("number of bytes")));
973 int posix_pedantic
= (getenv ("POSIXLY_CORRECT") != NULL
);
975 /* When POSIXLY_CORRECT is set, enforce the `at most one
976 file argument' requirement. */
980 too many arguments; When using tail's obsolescent option syntax (%s)\n\
981 there may be no more than one file argument. Use the equivalent -n or -c\n\
982 option instead."), argv
[1]);
987 #if DISABLED /* FIXME: enable or remove this warning. */
989 Warning: it is not portable to use two or more file arguments with\n\
990 tail's obsolescent option syntax (%s). Use the equivalent -n or -c\n\
991 option instead."), argv
[1]);
996 from_start
= t_from_start
;
997 count_lines
= t_count_lines
;
1005 parse_options (int argc
, char **argv
,
1006 off_t
*n_units
, enum header_mode
*header_mode
)
1011 forever
= forever_multiple
= from_start
= print_headers
= 0;
1013 while ((c
= getopt_long (argc
, argv
, "c:n:fqv", long_options
, NULL
)) != -1)
1022 count_lines
= (c
== 'n');
1025 else if (*optarg
== '-')
1030 unsigned long int tmp_ulong
;
1031 s_err
= xstrtoul (optarg
, NULL
, 0, &tmp_ulong
, "bkm");
1032 if (s_err
== LONGINT_INVALID
)
1034 error (EXIT_FAILURE
, 0, "%s: %s", optarg
,
1036 ? _("invalid number of lines")
1037 : _("invalid number of bytes")));
1039 if (s_err
!= LONGINT_OK
|| tmp_ulong
> OFF_T_MAX
)
1041 error (EXIT_FAILURE
, 0,
1042 _("%s: %s is so large that it is not representable"),
1044 c
== 'n' ? _("number of lines") : _("number of bytes"));
1046 *n_units
= (off_t
) tmp_ulong
;
1055 *header_mode
= never
;
1059 *header_mode
= always
;
1069 main (int argc
, char **argv
)
1071 enum header_mode header_mode
= multiple_files
;
1072 int exit_status
= 0;
1073 /* If from_start, the number of items to skip before printing; otherwise,
1074 the number of items at the end of the file to print. Although the type
1075 is signed, the value is never negative. */
1076 off_t n_units
= DEFAULT_N_LINES
;
1080 program_name
= argv
[0];
1081 setlocale (LC_ALL
, "");
1082 bindtextdomain (PACKAGE
, LOCALEDIR
);
1083 textdomain (PACKAGE
);
1085 have_read_stdin
= 0;
1088 int found_obsolescent
;
1090 found_obsolescent
= parse_obsolescent_option (argc
,
1091 (const char *const *) argv
,
1093 if (found_obsolescent
)
1096 exit (EXIT_FAILURE
);
1101 parse_options (argc
, argv
, &n_units
, &header_mode
);
1107 printf ("tail (%s) %s\n", GNU_PACKAGE
, VERSION
);
1108 exit (EXIT_SUCCESS
);
1114 /* To start printing with item N_UNITS from the start of the file, skip
1115 N_UNITS - 1 items. `tail +0' is actually meaningless, but for Unix
1116 compatibility it's treated the same as `tail +1'. */
1123 n_files
= argc
- optind
;
1124 file
= argv
+ optind
;
1126 if (n_files
> 1 && forever
)
1128 forever_multiple
= 1;
1130 file_descs
= (int *) xmalloc (n_files
* sizeof (int));
1131 file_sizes
= (off_t
*) xmalloc (n_files
* sizeof (off_t
));
1134 if (header_mode
== always
1135 || (header_mode
== multiple_files
&& n_files
> 1))
1140 exit_status
|= tail_file ("-", n_units
, 0);
1145 for (i
= 0; i
< n_files
; i
++)
1146 exit_status
|= tail_file (file
[i
], n_units
, i
);
1148 if (forever_multiple
)
1149 tail_forever (file
, n_files
);
1152 if (have_read_stdin
&& close (0) < 0)
1153 error (EXIT_FAILURE
, errno
, "-");
1154 if (fclose (stdout
) == EOF
)
1155 error (EXIT_FAILURE
, errno
, _("write error"));
1156 exit (exit_status
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
);