.
[coreutils.git] / src / tail.c
blob949d8c292d8c98e4d9d499134a57dffa61e98ce1
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)
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 Options:
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
37 ``-'' is encountered.
38 By default, filename headers are printed only more than one file
39 is given.
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>. */
46 #include <config.h>
48 #include <stdio.h>
49 #include <assert.h>
50 #include <getopt.h>
51 #include <sys/types.h>
53 #include "system.h"
54 #include "xstrtoul.h"
55 #include "error.h"
57 #ifndef OFF_T_MIN
58 # define OFF_T_MIN TYPE_MINIMUM (off_t)
59 #endif
61 #ifndef OFF_T_MAX
62 # define OFF_T_MAX TYPE_MAXIMUM (off_t)
63 #endif
65 /* Disable assertions. Some systems have broken assert macros. */
66 #define NDEBUG 1
68 #define XWRITE(fd, buffer, n_bytes) \
69 do \
70 { \
71 assert ((fd) == 1); \
72 assert ((n_bytes) >= 0); \
73 if (n_bytes > 0 && fwrite ((buffer), 1, (n_bytes), stdout) == 0) \
74 error (EXIT_FAILURE, errno, _("write error")); \
75 } \
76 while (0)
78 /* Number of items to tail. */
79 #define DEFAULT_N_LINES 10
81 /* Size of atomic reads. */
82 #ifndef BUFSIZ
83 # define BUFSIZ (512 * 8)
84 #endif
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. */
91 static int forever;
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. */
109 enum header_mode
111 multiple_files, always, never
114 int safe_read ();
116 /* The name this program was run with. */
117 char *program_name;
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},
138 {NULL, 0, NULL, 0}
141 static void
142 usage (int status)
144 if (status != 0)
145 fprintf (stderr, _("Try `%s --help' for more information.\n"),
146 program_name);
147 else
149 printf (_("\
150 Usage: %s [OPTION]... [FILE]...\n\
152 program_name);
153 printf (_("\
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\
172 or -c +VALUE.\n\
173 "));
174 puts (_("\nReport bugs to <textutils-bugs@gnu.org>."));
176 exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
179 static void
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 : ""));
187 first_file = 0;
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. */
198 static int
199 file_lines (const char *filename, int fd, long int n_lines, off_t pos)
201 char buffer[BUFSIZ];
202 int bytes_read;
203 int i; /* Index into `buffer' for scanning. */
205 if (n_lines == 0)
206 return 0;
208 /* Set `bytes_read' to the size of the last, probably partial, buffer;
209 0 < `bytes_read' <= `BUFSIZ'. */
210 bytes_read = pos % BUFSIZ;
211 if (bytes_read == 0)
212 bytes_read = 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. */
215 pos -= bytes_read;
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);
221 return 1;
224 /* Count the incomplete line on files that don't end with a newline. */
225 if (bytes_read && buffer[bytes_read - 1] != '\n')
226 --n_lines;
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));
240 return 0;
243 /* Not enough newlines in that bufferfull. */
244 if (pos == 0)
246 /* Not enough lines in the file; print the entire file. */
247 lseek (fd, (off_t) 0, SEEK_SET);
248 return 0;
250 pos -= BUFSIZ;
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);
257 return 1;
259 return 0;
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. */
267 static int
268 pipe_lines (const char *filename, int fd, long int n_lines)
270 struct linebuffer
272 int nbytes, nlines;
273 char buffer[BUFSIZ];
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. */
280 int errors = 0;
282 first = last = (LBUFFER *) xmalloc (sizeof (LBUFFER));
283 first->nbytes = first->nlines = 0;
284 first->next = NULL;
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)
290 tmp->nlines = 0;
291 tmp->next = NULL;
293 /* Count the number of newlines just read. */
294 for (i = 0; i < tmp->nbytes; i++)
295 if (tmp->buffer[i] == '\n')
296 ++tmp->nlines;
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;
308 else
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
314 worthwhile. */
315 last = last->next = tmp;
316 if (total_lines - first->nlines > n_lines)
318 tmp = first;
319 total_lines -= first->nlines;
320 first = first->next;
322 else
323 tmp = (LBUFFER *) xmalloc (sizeof (LBUFFER));
326 if (tmp->nbytes == -1)
328 error (0, errno, "%s", filename);
329 errors = 1;
330 free ((char *) tmp);
331 goto free_lbuffers;
334 free ((char *) tmp);
336 /* This prevents a core dump when the pipe contains no newlines. */
337 if (n_lines == 0)
338 goto free_lbuffers;
340 /* Count the incomplete line on files that don't end with a newline. */
341 if (last->buffer[last->nbytes - 1] != '\n')
343 ++last->nlines;
344 ++total_lines;
347 /* Run through the list, printing lines. First, skip over unneeded
348 buffers. */
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)
355 char *cp;
357 /* Skip `total_lines' - `n_lines' newlines. We made sure that
358 `total_lines' - `n_lines' <= `tmp->nlines'. */
359 cp = tmp->buffer;
360 for (i = total_lines - n_lines; i; --i)
361 while (*cp++ != '\n')
362 /* Do nothing. */ ;
363 i = cp - tmp->buffer;
365 else
366 i = 0;
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);
372 free_lbuffers:
373 while (first)
375 tmp = first->next;
376 free ((char *) first);
377 first = tmp;
379 return errors;
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. */
386 static int
387 pipe_bytes (const char *filename, int fd, off_t n_bytes)
389 struct charbuffer
391 int nbytes;
392 char buffer[BUFSIZ];
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. */
399 int errors = 0;
401 first = last = (CBUFFER *) xmalloc (sizeof (CBUFFER));
402 first->nbytes = 0;
403 first->next = NULL;
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)
409 tmp->next = NULL;
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;
420 else
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
426 worthwhile. */
427 last = last->next = tmp;
428 if (total_bytes - first->nbytes > n_bytes)
430 tmp = first;
431 total_bytes -= first->nbytes;
432 first = first->next;
434 else
436 tmp = (CBUFFER *) xmalloc (sizeof (CBUFFER));
440 if (tmp->nbytes == -1)
442 error (0, errno, "%s", filename);
443 errors = 1;
444 free ((char *) tmp);
445 goto free_cbuffers;
448 free ((char *) tmp);
450 /* Run through the list, printing characters. First, skip over unneeded
451 buffers. */
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;
459 else
460 i = 0;
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);
466 free_cbuffers:
467 while (first)
469 tmp = first->next;
470 free ((char *) first);
471 first = tmp;
473 return errors;
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. */
480 static int
481 start_bytes (const char *filename, int fd, off_t n_bytes)
483 char buffer[BUFSIZ];
484 int bytes_read = 0;
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);
491 return 1;
493 else if (n_bytes < 0)
494 XWRITE (STDOUT_FILENO, &buffer[bytes_read + n_bytes], -n_bytes);
495 return 0;
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. */
502 static int
503 start_lines (const char *filename, int fd, long int n_lines)
505 char buffer[BUFSIZ];
506 int bytes_read = 0;
507 int bytes_to_skip = 0;
509 while (n_lines && (bytes_read = safe_read (fd, buffer, BUFSIZ)) > 0)
511 bytes_to_skip = 0;
512 while (bytes_to_skip < bytes_read)
513 if (buffer[bytes_to_skip++] == '\n' && --n_lines == 0)
514 break;
516 if (bytes_read == -1)
518 error (0, errno, "%s", filename);
519 return 1;
521 else if (bytes_to_skip < bytes_read)
523 XWRITE (STDOUT_FILENO, &buffer[bytes_to_skip],
524 bytes_read - bytes_to_skip);
526 return 0;
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. */
533 static long
534 dump_remainder (const char *filename, int fd)
536 char buffer[BUFSIZ];
537 int bytes_read;
538 long total;
540 total = 0;
541 output:
542 while ((bytes_read = safe_read (fd, buffer, BUFSIZ)) > 0)
544 XWRITE (STDOUT_FILENO, buffer, bytes_read);
545 total += bytes_read;
547 if (bytes_read == -1)
548 error (EXIT_FAILURE, errno, "%s", filename);
549 if (forever)
551 fflush (stdout);
552 sleep (1);
553 goto output;
555 else
557 if (forever_multiple)
558 fflush (stdout);
561 return total;
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. */
571 static void
572 tail_forever (char **names, int nfiles)
574 int last;
576 last = -1;
578 while (1)
580 int i;
581 int changed;
583 changed = 0;
584 for (i = 0; i < nfiles; i++)
586 struct stat stats;
588 if (file_descs[i] < 0)
589 continue;
590 if (fstat (file_descs[i], &stats) < 0)
592 error (0, errno, "%s", names[i]);
593 file_descs[i] = -1;
594 continue;
596 if (stats.st_size == file_sizes[i])
597 continue;
599 /* This file has changed size. Print out what we can, and
600 then keep looping. */
602 changed = 1;
604 if (stats.st_size < file_sizes[i])
606 write_header (names[i], _("file truncated"));
607 last = i;
608 lseek (file_descs[i], stats.st_size, SEEK_SET);
609 file_sizes[i] = stats.st_size;
610 continue;
613 if (i != last)
615 if (print_headers)
616 write_header (names[i], NULL);
617 last = i;
619 file_sizes[i] += dump_remainder (names[i], file_descs[i]);
622 /* If none of the files changed size, sleep. */
623 if (! changed)
624 sleep (1);
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. */
631 static int
632 tail_bytes (const char *filename, int fd, off_t n_bytes)
634 struct stat stats;
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
639 error, either. */
640 if (fstat (fd, &stats))
642 error (0, errno, "%s", filename);
643 return 1;
646 if (from_start)
648 if (S_ISREG (stats.st_mode))
649 lseek (fd, n_bytes, SEEK_CUR);
650 else if (start_bytes (filename, fd, n_bytes))
651 return 1;
652 dump_remainder (filename, fd);
654 else
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)
664 off_t diff;
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;
669 else
671 error (0, errno, "%s", filename);
672 return 1;
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);
683 else
685 /* There are more bytes remaining than were requested.
686 Back up. */
687 lseek (fd, -n_bytes, SEEK_END);
689 dump_remainder (filename, fd);
691 else
692 return pipe_bytes (filename, fd, n_bytes);
694 return 0;
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. */
700 static int
701 tail_lines (const char *filename, int fd, long int n_lines)
703 struct stat stats;
704 off_t length;
706 if (fstat (fd, &stats))
708 error (0, errno, "%s", filename);
709 return 1;
712 if (from_start)
714 if (start_lines (filename, fd, n_lines))
715 return 1;
716 dump_remainder (filename, fd);
718 else
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))
731 return 1;
732 dump_remainder (filename, fd);
734 else
735 return pipe_lines (filename, fd, n_lines);
737 return 0;
740 /* Display the last N_UNITS units of file FILENAME, open for reading
741 in FD.
742 Return 0 if successful, 1 if an error occurred. */
744 static int
745 tail (const char *filename, int fd, off_t n_units)
747 if (count_lines)
748 return tail_lines (filename, fd, (long) n_units);
749 else
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. */
758 static int
759 tail_file (const char *filename, off_t n_units, int filenum)
761 int fd, errors;
762 struct stat stats;
764 if (!strcmp (filename, "-"))
766 have_read_stdin = 1;
767 filename = _("standard input");
768 if (print_headers)
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"));
776 errors = 1;
778 else if (!S_ISREG (stats.st_mode))
780 error (0, 0,
781 _("standard input: cannot follow end of non-regular file"));
782 errors = 1;
784 if (errors)
785 file_descs[filenum] = -1;
786 else
788 file_descs[filenum] = 0;
789 file_sizes[filenum] = stats.st_size;
793 else
795 /* Not standard input. */
796 fd = open (filename, O_RDONLY);
797 if (fd == -1)
799 if (forever_multiple)
800 file_descs[filenum] = -1;
801 error (0, errno, "%s", filename);
802 errors = 1;
804 else
806 if (print_headers)
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);
814 errors = 1;
816 else if (!S_ISREG (stats.st_mode))
818 error (0, 0, _("%s: cannot follow end of non-regular file"),
819 filename);
820 errors = 1;
822 if (errors)
824 close (fd);
825 file_descs[filenum] = -1;
827 else
829 file_descs[filenum] = fd;
830 file_sizes[filenum] = stats.st_size;
833 else
835 if (close (fd))
837 error (0, errno, "%s", filename);
838 errors = 1;
844 return errors;
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. */
856 static int
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;
864 int t_from_start;
865 int t_count_lines;
866 int t_forever;
868 /* With the obsolescent form, there is one option string and
869 (technically) at most one file argument. But we allow two or more
870 by default. */
871 if (argc < 2)
872 return 0;
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?$/; */
878 /* Test this:
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)))
882 return 0;
884 if (*p == '+')
885 t_from_start = 1;
886 else if (*p == '-')
887 t_from_start = 0;
888 else
889 return 0;
891 ++p;
892 if (ISDIGIT (*p))
894 n_string = p;
897 ++p;
899 while (ISDIGIT (*p));
901 n_string_end = p;
903 t_count_lines = 1;
904 if (*p == 'c')
906 t_count_lines = 0;
907 ++p;
909 else if (*p == 'l')
911 ++p;
914 t_forever = 0;
915 if (*p == 'f')
917 t_forever = 1;
918 ++p;
921 if (*p != '\0')
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)
929 error (0, 0,
930 _("%c: invalid suffix character in obsolescent option" ), *p);
931 *fail = 1;
932 return 1;
935 /* Otherwise, it might be a valid non-obsolescent option like -n. */
936 return 0;
939 *fail = 0;
940 if (n_string == NULL)
941 *n_units = DEFAULT_N_LINES;
942 else
944 strtol_error s_err;
945 unsigned long int tmp_ulong;
946 char *end;
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;
950 else
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';
959 error (0, 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")));
964 free (n_string_tmp);
965 *fail = 1;
969 if (!*fail)
971 if (argc > 3)
973 int posix_pedantic = (getenv ("POSIXLY_CORRECT") != NULL);
975 /* When POSIXLY_CORRECT is set, enforce the `at most one
976 file argument' requirement. */
977 if (posix_pedantic)
979 error (0, 0, _("\
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]);
983 *fail = 1;
984 return 1;
987 #if DISABLED /* FIXME: enable or remove this warning. */
988 error (0, 0, _("\
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]);
992 #endif
995 /* Set globals. */
996 from_start = t_from_start;
997 count_lines = t_count_lines;
998 forever = t_forever;
1001 return 1;
1004 static void
1005 parse_options (int argc, char **argv,
1006 off_t *n_units, enum header_mode *header_mode)
1008 int c;
1010 count_lines = 1;
1011 forever = forever_multiple = from_start = print_headers = 0;
1013 while ((c = getopt_long (argc, argv, "c:n:fqv", long_options, NULL)) != -1)
1015 switch (c)
1017 case 0:
1018 break;
1020 case 'c':
1021 case 'n':
1022 count_lines = (c == 'n');
1023 if (*optarg == '+')
1024 from_start = 1;
1025 else if (*optarg == '-')
1026 ++optarg;
1029 strtol_error s_err;
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,
1035 (c == 'n'
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"),
1043 optarg,
1044 c == 'n' ? _("number of lines") : _("number of bytes"));
1046 *n_units = (off_t) tmp_ulong;
1048 break;
1050 case 'f':
1051 forever = 1;
1052 break;
1054 case 'q':
1055 *header_mode = never;
1056 break;
1058 case 'v':
1059 *header_mode = always;
1060 break;
1062 default:
1063 usage (1);
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;
1077 int n_files;
1078 char **file;
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;
1089 int fail;
1090 found_obsolescent = parse_obsolescent_option (argc,
1091 (const char *const *) argv,
1092 &n_units, &fail);
1093 if (found_obsolescent)
1095 if (fail)
1096 exit (EXIT_FAILURE);
1097 optind = 2;
1099 else
1101 parse_options (argc, argv, &n_units, &header_mode);
1105 if (show_version)
1107 printf ("tail (%s) %s\n", GNU_PACKAGE, VERSION);
1108 exit (EXIT_SUCCESS);
1111 if (show_help)
1112 usage (0);
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'. */
1117 if (from_start)
1119 if (n_units)
1120 --n_units;
1123 n_files = argc - optind;
1124 file = argv + optind;
1126 if (n_files > 1 && forever)
1128 forever_multiple = 1;
1129 forever = 0;
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))
1136 print_headers = 1;
1138 if (n_files == 0)
1140 exit_status |= tail_file ("-", n_units, 0);
1142 else
1144 int i;
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);