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
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>. */
31 #include <sys/types.h>
36 #include "safe-read.h"
39 # define OFF_T_MIN TYPE_MINIMUM (off_t)
43 # define OFF_T_MAX TYPE_MAXIMUM (off_t)
46 /* Number of items to tail. */
47 #define DEFAULT_N_LINES 10
49 /* Size of atomic reads. */
51 # define BUFSIZ (512 * 8)
65 /* The actual file name, or "-" for stdin. */
68 /* File descriptor on which the file is open; -1 if it's not open. */
71 /* The size of the file the last time we checked. */
74 /* The device and inode of the file the last time we checked. */
79 unsigned int n_stat_calls
;
82 unsigned int n_unchanged_stats
;
89 static int allow_missing
;
91 /* If nonzero, interpret the numeric argument as the number of lines.
92 Otherwise, interpret it as the number of bytes. */
93 static int count_lines
;
95 /* Whether we follow the name of each file or the file descriptor
96 that is initially associated with each name. */
97 /* FIXME: make follow_name the default? */
98 static enum Follow_mode follow_mode
;
100 /* If nonzero, read from the ends of all specified files until killed. */
103 /* If nonzero, count from start of file instead of end. */
104 static int from_start
;
106 /* If nonzero, print filename headers. */
107 static int print_headers
;
109 /* When to print the filename banners. */
112 multiple_files
, always
, never
115 /* FIXME: describe -- add option */
116 static unsigned long max_n_unchanged_stats
= 5;
118 /* The name this program was run with. */
121 /* The number of seconds to sleep between accesses. */
122 static unsigned int sleep_interval
= 1;
124 /* Nonzero if we have ever read standard input. */
125 static int have_read_stdin
;
127 /* If nonzero, display usage information and exit. */
128 static int show_help
;
130 /* If nonzero, print the version on standard output then exit. */
131 static int show_version
;
133 static struct option
const long_options
[] =
135 {"allow-missing", required_argument
, NULL
, 11},
136 {"bytes", required_argument
, NULL
, 'c'},
137 {"follow", no_argument
, NULL
, 'f'},
138 {"follow-descriptor", no_argument
, NULL
, 12},
139 {"follow-name", no_argument
, NULL
, 13},
140 {"lines", required_argument
, NULL
, 'n'},
141 {"quiet", no_argument
, NULL
, 'q'},
142 {"silent", no_argument
, NULL
, 'q'},
143 {"sleep-interval", required_argument
, NULL
, 's'},
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 -s, --sleep-interval=S with -f, sleep S seconds between iterations\n\
172 -v, --verbose always output headers giving file names\n\
173 --help display this help and exit\n\
174 --version output version information and exit\n\
176 If the first character of N (the number of bytes or lines) is a `+',\n\
177 print beginning with the Nth item from the start of each file, otherwise,\n\
178 print the last N items in the file. N may have a multiplier suffix:\n\
179 b for 512, k for 1024, m for 1048576 (1 Meg). A first OPTION of -VALUE\n\
180 or +VALUE is treated like -n VALUE or -n +VALUE unless VALUE has one of\n\
181 the [bkm] suffix multipliers, in which case it is treated like -c VALUE\n\
184 puts (_("\nReport bugs to <textutils-bugs@gnu.org>."));
186 exit (status
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
);
190 pretty_name (struct File_spec
const *f
)
192 return (STREQ (f
->name
, "-") ? "standard input" : f
->name
);
196 xwrite (int fd
, char *const buffer
, size_t n_bytes
)
199 assert (n_bytes
>= 0);
200 if (n_bytes
> 0 && fwrite (buffer
, 1, n_bytes
, stdout
) == 0)
201 error (EXIT_FAILURE
, errno
, _("write error"));
205 close_fd (int fd
, const char *filename
)
207 if (fd
!= -1 && fd
!= STDIN_FILENO
&& close (fd
))
209 error (0, errno
, _("closing %s (fd=%d)"), filename
, fd
);
214 write_header (const char *pretty_filename
, const char *comment
)
216 static int first_file
= 1;
218 printf ("%s==> %s%s%s <==\n", (first_file
? "" : "\n"), pretty_filename
,
219 (comment
? ": " : ""),
220 (comment
? comment
: ""));
224 /* Print the last N_LINES lines from the end of file FD.
225 Go backward through the file, reading `BUFSIZ' bytes at a time (except
226 probably the first), until we hit the start of the file or have
227 read NUMBER newlines.
228 POS starts out as the length of the file (the offset of the last
229 byte of the file + 1).
230 Return 0 if successful, 1 if an error occurred. */
233 file_lines (const char *pretty_filename
, int fd
, long int n_lines
, off_t pos
)
237 int i
; /* Index into `buffer' for scanning. */
242 /* Set `bytes_read' to the size of the last, probably partial, buffer;
243 0 < `bytes_read' <= `BUFSIZ'. */
244 bytes_read
= pos
% BUFSIZ
;
247 /* Make `pos' a multiple of `BUFSIZ' (0 if the file is short), so that all
248 reads will be on block boundaries, which might increase efficiency. */
250 /* FIXME: check lseek return value */
251 lseek (fd
, pos
, SEEK_SET
);
252 bytes_read
= safe_read (fd
, buffer
, bytes_read
);
253 if (bytes_read
== -1)
255 error (0, errno
, "%s", pretty_filename
);
259 /* Count the incomplete line on files that don't end with a newline. */
260 if (bytes_read
&& buffer
[bytes_read
- 1] != '\n')
265 /* Scan backward, counting the newlines in this bufferfull. */
266 for (i
= bytes_read
- 1; i
>= 0; i
--)
268 /* Have we counted the requested number of newlines yet? */
269 if (buffer
[i
] == '\n' && n_lines
-- == 0)
271 /* If this newline wasn't the last character in the buffer,
272 print the text after it. */
273 if (i
!= bytes_read
- 1)
274 xwrite (STDOUT_FILENO
, &buffer
[i
+ 1], bytes_read
- (i
+ 1));
278 /* Not enough newlines in that bufferfull. */
281 /* Not enough lines in the file; print the entire file. */
282 /* FIXME: check lseek return value */
283 lseek (fd
, (off_t
) 0, SEEK_SET
);
287 /* FIXME: check lseek return value */
288 lseek (fd
, pos
, SEEK_SET
);
290 while ((bytes_read
= safe_read (fd
, buffer
, BUFSIZ
)) > 0);
291 if (bytes_read
== -1)
293 error (0, errno
, "%s", pretty_filename
);
299 /* Print the last N_LINES lines from the end of the standard input,
300 open for reading as pipe FD.
301 Buffer the text as a linked list of LBUFFERs, adding them as needed.
302 Return 0 if successful, 1 if an error occured. */
305 pipe_lines (const char *pretty_filename
, int fd
, long int n_lines
)
311 struct linebuffer
*next
;
313 typedef struct linebuffer LBUFFER
;
314 LBUFFER
*first
, *last
, *tmp
;
315 int i
; /* Index into buffers. */
316 int total_lines
= 0; /* Total number of newlines in all buffers. */
319 first
= last
= (LBUFFER
*) xmalloc (sizeof (LBUFFER
));
320 first
->nbytes
= first
->nlines
= 0;
322 tmp
= (LBUFFER
*) xmalloc (sizeof (LBUFFER
));
324 /* Input is always read into a fresh buffer. */
325 while ((tmp
->nbytes
= safe_read (fd
, tmp
->buffer
, BUFSIZ
)) > 0)
330 /* Count the number of newlines just read. */
331 for (i
= 0; i
< tmp
->nbytes
; i
++)
332 if (tmp
->buffer
[i
] == '\n')
334 total_lines
+= tmp
->nlines
;
336 /* If there is enough room in the last buffer read, just append the new
337 one to it. This is because when reading from a pipe, `nbytes' can
338 often be very small. */
339 if (tmp
->nbytes
+ last
->nbytes
< BUFSIZ
)
341 memcpy (&last
->buffer
[last
->nbytes
], tmp
->buffer
, tmp
->nbytes
);
342 last
->nbytes
+= tmp
->nbytes
;
343 last
->nlines
+= tmp
->nlines
;
347 /* If there's not enough room, link the new buffer onto the end of
348 the list, then either free up the oldest buffer for the next
349 read if that would leave enough lines, or else malloc a new one.
350 Some compaction mechanism is possible but probably not
352 last
= last
->next
= tmp
;
353 if (total_lines
- first
->nlines
> n_lines
)
356 total_lines
-= first
->nlines
;
360 tmp
= (LBUFFER
*) xmalloc (sizeof (LBUFFER
));
363 if (tmp
->nbytes
== -1)
365 error (0, errno
, "%s", pretty_filename
);
373 /* This prevents a core dump when the pipe contains no newlines. */
377 /* Count the incomplete line on files that don't end with a newline. */
378 if (last
->buffer
[last
->nbytes
- 1] != '\n')
384 /* Run through the list, printing lines. First, skip over unneeded
386 for (tmp
= first
; total_lines
- tmp
->nlines
> n_lines
; tmp
= tmp
->next
)
387 total_lines
-= tmp
->nlines
;
389 /* Find the correct beginning, then print the rest of the file. */
390 if (total_lines
> n_lines
)
394 /* Skip `total_lines' - `n_lines' newlines. We made sure that
395 `total_lines' - `n_lines' <= `tmp->nlines'. */
397 for (i
= total_lines
- n_lines
; i
; --i
)
398 while (*cp
++ != '\n')
400 i
= cp
- tmp
->buffer
;
404 xwrite (STDOUT_FILENO
, &tmp
->buffer
[i
], tmp
->nbytes
- i
);
406 for (tmp
= tmp
->next
; tmp
; tmp
= tmp
->next
)
407 xwrite (STDOUT_FILENO
, tmp
->buffer
, tmp
->nbytes
);
413 free ((char *) first
);
419 /* Print the last N_BYTES characters from the end of pipe FD.
420 This is a stripped down version of pipe_lines.
421 Return 0 if successful, 1 if an error occurred. */
424 pipe_bytes (const char *pretty_filename
, int fd
, off_t n_bytes
)
430 struct charbuffer
*next
;
432 typedef struct charbuffer CBUFFER
;
433 CBUFFER
*first
, *last
, *tmp
;
434 int i
; /* Index into buffers. */
435 int total_bytes
= 0; /* Total characters in all buffers. */
438 first
= last
= (CBUFFER
*) xmalloc (sizeof (CBUFFER
));
441 tmp
= (CBUFFER
*) xmalloc (sizeof (CBUFFER
));
443 /* Input is always read into a fresh buffer. */
444 while ((tmp
->nbytes
= safe_read (fd
, tmp
->buffer
, BUFSIZ
)) > 0)
448 total_bytes
+= tmp
->nbytes
;
449 /* If there is enough room in the last buffer read, just append the new
450 one to it. This is because when reading from a pipe, `nbytes' can
451 often be very small. */
452 if (tmp
->nbytes
+ last
->nbytes
< BUFSIZ
)
454 memcpy (&last
->buffer
[last
->nbytes
], tmp
->buffer
, tmp
->nbytes
);
455 last
->nbytes
+= tmp
->nbytes
;
459 /* If there's not enough room, link the new buffer onto the end of
460 the list, then either free up the oldest buffer for the next
461 read if that would leave enough characters, or else malloc a new
462 one. Some compaction mechanism is possible but probably not
464 last
= last
->next
= tmp
;
465 if (total_bytes
- first
->nbytes
> n_bytes
)
468 total_bytes
-= first
->nbytes
;
473 tmp
= (CBUFFER
*) xmalloc (sizeof (CBUFFER
));
477 if (tmp
->nbytes
== -1)
479 error (0, errno
, "%s", pretty_filename
);
487 /* Run through the list, printing characters. First, skip over unneeded
489 for (tmp
= first
; total_bytes
- tmp
->nbytes
> n_bytes
; tmp
= tmp
->next
)
490 total_bytes
-= tmp
->nbytes
;
492 /* Find the correct beginning, then print the rest of the file.
493 We made sure that `total_bytes' - `n_bytes' <= `tmp->nbytes'. */
494 if (total_bytes
> n_bytes
)
495 i
= total_bytes
- n_bytes
;
498 xwrite (STDOUT_FILENO
, &tmp
->buffer
[i
], tmp
->nbytes
- i
);
500 for (tmp
= tmp
->next
; tmp
; tmp
= tmp
->next
)
501 xwrite (STDOUT_FILENO
, tmp
->buffer
, tmp
->nbytes
);
507 free ((char *) first
);
513 /* Skip N_BYTES characters from the start of pipe FD, and print
514 any extra characters that were read beyond that.
515 Return 1 on error, 0 if ok. */
518 start_bytes (const char *pretty_filename
, int fd
, off_t n_bytes
)
523 while (n_bytes
> 0 && (bytes_read
= safe_read (fd
, buffer
, BUFSIZ
)) > 0)
524 n_bytes
-= bytes_read
;
525 if (bytes_read
== -1)
527 error (0, errno
, "%s", pretty_filename
);
530 else if (n_bytes
< 0)
531 xwrite (STDOUT_FILENO
, &buffer
[bytes_read
+ n_bytes
], -n_bytes
);
535 /* Skip N_LINES lines at the start of file or pipe FD, and print
536 any extra characters that were read beyond that.
537 Return 1 on error, 0 if ok. */
540 start_lines (const char *pretty_filename
, int fd
, long int n_lines
)
544 int bytes_to_skip
= 0;
546 while (n_lines
&& (bytes_read
= safe_read (fd
, buffer
, BUFSIZ
)) > 0)
549 while (bytes_to_skip
< bytes_read
)
550 if (buffer
[bytes_to_skip
++] == '\n' && --n_lines
== 0)
553 if (bytes_read
== -1)
555 error (0, errno
, "%s", pretty_filename
);
558 else if (bytes_to_skip
< bytes_read
)
560 xwrite (STDOUT_FILENO
, &buffer
[bytes_to_skip
],
561 bytes_read
- bytes_to_skip
);
566 /* Display file FILENAME from the current position in FD to the end.
567 Return the number of bytes read from the file. */
570 dump_remainder (const char *pretty_filename
, int fd
)
577 while ((bytes_read
= safe_read (fd
, buffer
, BUFSIZ
)) > 0)
579 xwrite (STDOUT_FILENO
, buffer
, bytes_read
);
582 if (bytes_read
== -1)
583 error (EXIT_FAILURE
, errno
, "%s", pretty_filename
);
591 /* FIXME: describe */
594 recheck (struct File_spec
*f
)
596 /* open/fstat the file and announce if dev/ino
598 struct stat new_stats
;
601 int is_stdin
= (STREQ (f
->name
, "-"));
603 fd
= (is_stdin
? STDIN_FILENO
: open (f
->name
, O_RDONLY
));
605 /* If the open fails because the file doesn't exist,
606 then mark the file as missing. */
607 f
->missing
= (allow_missing
&& fd
== -1 && errno
== ENOENT
);
609 if (fd
== -1 || fstat (fd
, &new_stats
) < 0)
612 error (0, errno
, "%s", pretty_name (f
));
614 else if (!S_ISREG (new_stats
.st_mode
)
615 && !S_ISFIFO (new_stats
.st_mode
))
619 _("%s has been replaced with a non-regular file; \
620 cannot follow end of non-regular file"),
626 close_fd (fd
, pretty_name (f
));
627 close_fd (f
->fd
, pretty_name (f
));
630 else if (f
->ino
!= new_stats
.st_ino
|| f
->dev
!= new_stats
.st_dev
)
632 /* Close the old one. */
633 close_fd (f
->fd
, pretty_name (f
));
635 /* File has been replaced (e.g., via log rotation) --
638 _("%s has been replaced; following end of new file"),
642 f
->size
= new_stats
.st_size
;
643 f
->dev
= new_stats
.st_dev
;
644 f
->ino
= new_stats
.st_ino
;
645 f
->n_unchanged_stats
= 0;
646 /* FIXME: check lseek return value */
647 lseek (f
->fd
, new_stats
.st_size
, SEEK_SET
);
651 error (0, 0, _("%s has reappeared"), pretty_name (f
));
655 f
->size
= new_stats
.st_size
;
656 f
->dev
= new_stats
.st_dev
;
657 f
->ino
= new_stats
.st_ino
;
658 f
->n_unchanged_stats
= 0;
659 /* FIXME: check lseek return value */
660 lseek (f
->fd
, new_stats
.st_size
, SEEK_SET
);
664 close_fd (fd
, pretty_name (f
));
668 /* Tail NFILES files forever, or until killed.
669 The pertinent information for each file is stored in an entry of F.
670 Loop over each of them, doing an fstat to see if they have changed size,
671 and an occasional open/fstat to see if any dev/ino pair has changed.
672 If none of them have changed size in one iteration, sleep for a
673 while and try again. Continue until the user interrupts us. */
676 tail_forever (struct File_spec
*f
, int nfiles
)
690 for (i
= 0; i
< nfiles
; i
++)
694 if (f
[i
].fd
< 0 && !f
[i
].missing
)
699 if (fstat (f
[i
].fd
, &stats
) < 0)
701 error (0, errno
, "%s", pretty_name (&f
[i
]));
706 if (stats
.st_size
== f
[i
].size
)
708 if (++f
[i
].n_unchanged_stats
> max_n_unchanged_stats
709 && follow_mode
== follow_name
)
712 f
[i
].n_unchanged_stats
= 0;
719 Otherwise, a file that's unlinked or moved aside, yet always
720 growing will never be recognized has having been renamed. */
722 /* This file has changed size. Print out what we can, and
723 then keep looping. */
728 f
[i
].n_unchanged_stats
= 0;
730 if (stats
.st_size
< f
[i
].size
)
732 write_header (pretty_name (&f
[i
]), _("file truncated"));
734 /* FIXME: check lseek return value */
735 lseek (f
[i
].fd
, stats
.st_size
, SEEK_SET
);
736 f
[i
].size
= stats
.st_size
;
743 write_header (pretty_name (&f
[i
]), NULL
);
746 f
[i
].size
+= dump_remainder (pretty_name (&f
[i
]), f
[i
].fd
);
749 if (!any_live_files
/* FIXME-now: && ! allow_missing */ )
751 error (0, 0, _("no files remaining"));
755 /* If none of the files changed size, sleep. */
757 sleep (sleep_interval
);
761 /* Output the last N_BYTES bytes of file FILENAME open for reading in FD.
762 Return 0 if successful, 1 if an error occurred. */
765 tail_bytes (const char *pretty_filename
, int fd
, off_t n_bytes
)
769 /* FIXME: resolve this like in dd.c. */
770 /* Use fstat instead of checking for errno == ESPIPE because
771 lseek doesn't work on some special files but doesn't return an
773 if (fstat (fd
, &stats
))
775 error (0, errno
, "%s", pretty_filename
);
781 if (S_ISREG (stats
.st_mode
))
783 /* FIXME: check lseek return value */
784 lseek (fd
, n_bytes
, SEEK_CUR
);
786 else if (start_bytes (pretty_filename
, fd
, n_bytes
))
790 dump_remainder (pretty_filename
, fd
);
794 if (S_ISREG (stats
.st_mode
))
796 off_t current_pos
, end_pos
;
797 size_t bytes_remaining
;
799 if ((current_pos
= lseek (fd
, (off_t
) 0, SEEK_CUR
)) != -1
800 && (end_pos
= lseek (fd
, (off_t
) 0, SEEK_END
)) != -1)
803 /* Be careful here. The current position may actually be
804 beyond the end of the file. */
805 bytes_remaining
= (diff
= end_pos
- current_pos
) < 0 ? 0 : diff
;
809 error (0, errno
, "%s", pretty_filename
);
813 if (bytes_remaining
<= n_bytes
)
815 /* From the current position to end of file, there are no
816 more bytes than have been requested. So reposition the
817 file pointer to the incoming current position and print
818 everything after that. */
819 /* FIXME: check lseek return value */
820 lseek (fd
, current_pos
, SEEK_SET
);
824 /* There are more bytes remaining than were requested.
826 /* FIXME: check lseek return value */
827 lseek (fd
, -n_bytes
, SEEK_END
);
829 dump_remainder (pretty_filename
, fd
);
832 return pipe_bytes (pretty_filename
, fd
, n_bytes
);
837 /* Output the last N_LINES lines of file FILENAME open for reading in FD.
838 Return 0 if successful, 1 if an error occurred. */
841 tail_lines (const char *pretty_filename
, int fd
, long int n_lines
)
846 if (fstat (fd
, &stats
))
848 error (0, errno
, "%s", pretty_filename
);
854 if (start_lines (pretty_filename
, fd
, n_lines
))
856 dump_remainder (pretty_filename
, fd
);
860 /* Use file_lines only if FD refers to a regular file with
861 its file pointer positioned at beginning of file. */
862 /* FIXME: adding the lseek conjunct is a kludge.
863 Once there's a reasonable test suite, fix the true culprit:
864 file_lines. file_lines shouldn't presume that the input
865 file pointer is initially positioned to beginning of file. */
866 if (S_ISREG (stats
.st_mode
)
867 && lseek (fd
, (off_t
) 0, SEEK_CUR
) == (off_t
) 0)
869 length
= lseek (fd
, (off_t
) 0, SEEK_END
);
870 if (length
!= 0 && file_lines (pretty_filename
, fd
, n_lines
, length
))
872 dump_remainder (pretty_filename
, fd
);
875 return pipe_lines (pretty_filename
, fd
, n_lines
);
880 /* Display the last N_UNITS units of file FILENAME, open for reading
882 Return 0 if successful, 1 if an error occurred. */
885 tail (const char *pretty_filename
, int fd
, off_t n_units
)
888 return tail_lines (pretty_filename
, fd
, (long) n_units
);
890 return tail_bytes (pretty_filename
, fd
, n_units
);
893 /* Display the last N_UNITS units of the file described by F.
894 Return 0 if successful, 1 if an error occurred. */
897 tail_file (struct File_spec
*f
, off_t n_units
)
902 int is_stdin
= (STREQ (f
->name
, "-"));
911 fd
= open (f
->name
, O_RDONLY
);
914 f
->missing
= (allow_missing
&& fd
== -1 && errno
== ENOENT
);
920 error (0, errno
, "%s", pretty_name (f
));
926 write_header (pretty_name (f
), NULL
);
927 errors
= tail (pretty_name (f
), fd
, n_units
);
930 /* FIXME: duplicate code */
931 if (fstat (fd
, &stats
) < 0)
933 error (0, errno
, "%s", pretty_name (f
));
936 else if (!S_ISREG (stats
.st_mode
) && !S_ISFIFO (stats
.st_mode
))
938 error (0, 0, _("%s: cannot follow end of non-regular file"),
944 close_fd (fd
, pretty_name (f
));
950 f
->size
= stats
.st_size
;
951 f
->dev
= stats
.st_dev
;
952 f
->ino
= stats
.st_ino
;
953 f
->n_unchanged_stats
= 0;
958 if (!is_stdin
&& close (fd
))
960 error (0, errno
, "%s", pretty_name (f
));
969 /* If the command line arguments are of the obsolescent form and the
970 option string is well-formed, set *FAIL to zero, set *N_UNITS, the
971 globals COUNT_LINES, FOREVER, and FROM_START, and return non-zero.
972 Otherwise, if the command line arguments appear to be of the
973 obsolescent form but the option string is malformed, set *FAIL to
974 non-zero, don't modify any other parameter or global variable, and
975 return non-zero. Otherwise, return zero and don't modify any parameter
976 or global variable. */
979 parse_obsolescent_option (int argc
, const char *const *argv
,
980 off_t
*n_units
, int *fail
)
982 const char *p
= argv
[1];
983 const char *n_string
= NULL
;
984 const char *n_string_end
;
990 /* With the obsolescent form, there is one option string and
991 (technically) at most one file argument. But we allow two or more
996 /* If P starts with `+', `-N' (where N is a digit), or `-l',
997 then it is obsolescent. Return zero otherwise. */
998 if ( ! (p
[0] == '+' || (p
[0] == '-' && (p
[1] == 'l' || ISDIGIT (p
[1])))) )
1016 while (ISDIGIT (*p
));
1040 /* If (argv[1] begins with a `+' or if it begins with `-' followed
1041 by a digit), but has an invalid suffix character, give a diagnostic
1042 and indicate to caller that this *is* of the obsolescent form,
1043 but that it's an invalid option. */
1044 if (t_from_start
|| n_string
)
1047 _("%c: invalid suffix character in obsolescent option" ), *p
);
1052 /* Otherwise, it might be a valid non-obsolescent option like -n. */
1057 if (n_string
== NULL
)
1058 *n_units
= DEFAULT_N_LINES
;
1062 unsigned long int tmp_ulong
;
1064 s_err
= xstrtoul (n_string
, &end
, 0, &tmp_ulong
, NULL
);
1065 if (s_err
== LONGINT_OK
&& tmp_ulong
<= OFF_T_MAX
)
1066 *n_units
= (off_t
) tmp_ulong
;
1069 /* Extract a NUL-terminated string for the error message. */
1070 size_t len
= n_string_end
- n_string
;
1071 char *n_string_tmp
= xmalloc (len
+ 1);
1073 strncpy (n_string_tmp
, n_string
, len
);
1074 n_string_tmp
[len
] = '\0';
1077 _("%s: %s is so large that it is not representable"),
1078 n_string_tmp
, (count_lines
1079 ? _("number of lines")
1080 : _("number of bytes")));
1081 free (n_string_tmp
);
1090 int posix_pedantic
= (getenv ("POSIXLY_CORRECT") != NULL
);
1092 /* When POSIXLY_CORRECT is set, enforce the `at most one
1093 file argument' requirement. */
1097 too many arguments; When using tail's obsolescent option syntax (%s)\n\
1098 there may be no more than one file argument. Use the equivalent -n or -c\n\
1099 option instead."), argv
[1]);
1104 #if DISABLED /* FIXME: enable or remove this warning. */
1106 Warning: it is not portable to use two or more file arguments with\n\
1107 tail's obsolescent option syntax (%s). Use the equivalent -n or -c\n\
1108 option instead."), argv
[1]);
1113 from_start
= t_from_start
;
1114 count_lines
= t_count_lines
;
1115 forever
= t_forever
;
1122 parse_options (int argc
, char **argv
,
1123 off_t
*n_units
, enum header_mode
*header_mode
)
1128 forever
= from_start
= print_headers
= 0;
1130 while ((c
= getopt_long (argc
, argv
, "c:n:fqs:v", long_options
, NULL
)) != -1)
1139 count_lines
= (c
== 'n');
1142 else if (*optarg
== '-')
1147 unsigned long int tmp_ulong
;
1148 s_err
= xstrtoul (optarg
, NULL
, 0, &tmp_ulong
, "bkm");
1149 if (s_err
== LONGINT_INVALID
)
1151 error (EXIT_FAILURE
, 0, "%s: %s", optarg
,
1153 ? _("invalid number of lines")
1154 : _("invalid number of bytes")));
1156 if (s_err
!= LONGINT_OK
|| tmp_ulong
> OFF_T_MAX
)
1158 error (EXIT_FAILURE
, 0,
1159 _("%s: %s is so large that it is not representable"),
1161 c
== 'n' ? _("number of lines") : _("number of bytes"));
1163 *n_units
= (off_t
) tmp_ulong
;
1176 follow_mode
= follow_descriptor
;
1180 follow_mode
= follow_name
;
1184 *header_mode
= never
;
1190 unsigned long int tmp_ulong
;
1191 s_err
= xstrtoul (optarg
, NULL
, 0, &tmp_ulong
, "");
1192 if (s_err
!= LONGINT_OK
|| tmp_ulong
> UINT_MAX
)
1194 error (EXIT_FAILURE
, 0,
1195 _("%s: invalid number of seconds"), optarg
);
1197 sleep_interval
= tmp_ulong
;
1202 *header_mode
= always
;
1212 main (int argc
, char **argv
)
1214 enum header_mode header_mode
= multiple_files
;
1215 int exit_status
= 0;
1216 /* If from_start, the number of items to skip before printing; otherwise,
1217 the number of items at the end of the file to print. Although the type
1218 is signed, the value is never negative. */
1219 off_t n_units
= DEFAULT_N_LINES
;
1222 struct File_spec
*F
;
1225 program_name
= argv
[0];
1226 setlocale (LC_ALL
, "");
1227 bindtextdomain (PACKAGE
, LOCALEDIR
);
1228 textdomain (PACKAGE
);
1230 have_read_stdin
= 0;
1233 int found_obsolescent
;
1235 found_obsolescent
= parse_obsolescent_option (argc
,
1236 (const char *const *) argv
,
1238 if (found_obsolescent
)
1241 exit (EXIT_FAILURE
);
1246 parse_options (argc
, argv
, &n_units
, &header_mode
);
1252 printf ("tail (%s) %s\n", GNU_PACKAGE
, VERSION
);
1253 exit (EXIT_SUCCESS
);
1259 /* To start printing with item N_UNITS from the start of the file, skip
1260 N_UNITS - 1 items. `tail +0' is actually meaningless, but for Unix
1261 compatibility it's treated the same as `tail +1'. */
1268 n_files
= argc
- optind
;
1269 file
= argv
+ optind
;
1273 static char *dummy_stdin
= "-";
1275 file
= &dummy_stdin
;
1278 F
= (struct File_spec
*) xmalloc (n_files
* sizeof (F
[0]));
1279 for (i
= 0; i
< n_files
; i
++)
1280 F
[i
].name
= file
[i
];
1282 if (header_mode
== always
1283 || (header_mode
== multiple_files
&& n_files
> 1))
1286 for (i
= 0; i
< n_files
; i
++)
1287 exit_status
|= tail_file (&F
[i
], n_units
);
1290 tail_forever (F
, n_files
);
1292 if (have_read_stdin
&& close (0) < 0)
1293 error (EXIT_FAILURE
, errno
, "-");
1294 if (fclose (stdout
) == EOF
)
1295 error (EXIT_FAILURE
, errno
, _("write error"));
1296 exit (exit_status
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
);