-char *xmalloc ();
-int safe_read ();
-
-/* The name this program was run with. */
-char *program_name;
-
-/* Nonzero if we have ever read standard input. */
-static int have_read_stdin;
-
-/* If nonzero, display usage information and exit. */
-static int show_help;
-
-/* If nonzero, print the version on standard output then exit. */
-static int show_version;
-
-static const char tail_usage[] =
-"tail [OPTION]... [FILE]...\n\
-Print last 10 lines of each FILE to standard output.\n\
-With more than one FILE, precede each with a header giving the file name.\n\
-With no FILE, or when FILE is -, read standard input.\n\
-\n\
- -c, --bytes=N output the last N bytes\n\
- -f, --follow output appended data as the file grows\n\
- -n, --lines=N output the last N lines, instead of last 10\n\
- -q, --quiet, --silent never output headers giving file names\n\
- -v, --verbose always output headers giving file names\n\
- --help display this help and exit\n\
- --version output version information and exit\n\
-\n\
-If the first character of N (the number of bytes or lines) is a `+',\n\
-print beginning with the Nth item from the start of each file, otherwise,\n\
-print the last N items in the file. N may have a multiplier suffix:\n\
-b for 512, k for 1024, m for 1048576 (1 Meg). A first OPTION of -VALUE\n\
-or +VALUE is treated like -n VALUE or -n +VALUE unless VALUE has one of\n\
-the [bkm] suffix multipliers, in which case it is treated like -c VALUE\n\
-or -c +VALUE.\n";
-
-static void
-write_header (const char *filename, const char *comment)
-{
- static int first_file = 1;
-
- printf ("%s==> %s%s%s <==\n", (first_file ? "" : "\n"), filename,
- (comment ? ": " : ""),
- (comment ? comment : ""));
- first_file = 0;
-}
-
-/* Print the last N_LINES lines from the end of file FD.
- Go backward through the file, reading `BUFSIZ' bytes at a time (except
- probably the first), until we hit the start of the file or have
- read NUMBER newlines.
- POS starts out as the length of the file (the offset of the last
- byte of the file + 1).
- Return 0 if successful, 1 if an error occurred. */
-
-static int
-file_lines (const char *filename, int fd, long int n_lines, off_t pos)
-{
- char buffer[BUFSIZ];
- int bytes_read;
- int i; /* Index into `buffer' for scanning. */
-
- if (n_lines == 0)
- return 0;
-
- /* Set `bytes_read' to the size of the last, probably partial, buffer;
- 0 < `bytes_read' <= `BUFSIZ'. */
- bytes_read = pos % BUFSIZ;
- if (bytes_read == 0)
- bytes_read = BUFSIZ;
- /* Make `pos' a multiple of `BUFSIZ' (0 if the file is short), so that all
- reads will be on block boundaries, which might increase efficiency. */
- pos -= bytes_read;
- lseek (fd, pos, SEEK_SET);
- bytes_read = safe_read (fd, buffer, bytes_read);
- if (bytes_read == -1)
- {
- error (0, errno, "%s", filename);
- return 1;
- }
-
- /* Count the incomplete line on files that don't end with a newline. */
- if (bytes_read && buffer[bytes_read - 1] != '\n')
- --n_lines;
-
- do
- {
- /* Scan backward, counting the newlines in this bufferfull. */
- for (i = bytes_read - 1; i >= 0; i--)
- {
- /* Have we counted the requested number of newlines yet? */
- if (buffer[i] == '\n' && n_lines-- == 0)
- {
- /* If this newline wasn't the last character in the buffer,
- print the text after it. */
- if (i != bytes_read - 1)
- XWRITE (STDOUT_FILENO, &buffer[i + 1], bytes_read - (i + 1));
- return 0;
- }
- }
- /* Not enough newlines in that bufferfull. */
- if (pos == 0)
- {
- /* Not enough lines in the file; print the entire file. */
- lseek (fd, (off_t) 0, SEEK_SET);
- return 0;
- }
- pos -= BUFSIZ;
- lseek (fd, pos, SEEK_SET);
- }
- while ((bytes_read = safe_read (fd, buffer, BUFSIZ)) > 0);
- if (bytes_read == -1)
- {
- error (0, errno, "%s", filename);
- return 1;
- }
- return 0;
-}
-
-/* Print the last N_LINES lines from the end of the standard input,
- open for reading as pipe FD.
- Buffer the text as a linked list of LBUFFERs, adding them as needed.
- Return 0 if successful, 1 if an error occured. */
-
-static int
-pipe_lines (const char *filename, int fd, long int n_lines)
-{
- struct linebuffer
- {
- int nbytes, nlines;
- char buffer[BUFSIZ];
- struct linebuffer *next;
- };
- typedef struct linebuffer LBUFFER;
- LBUFFER *first, *last, *tmp;
- int i; /* Index into buffers. */
- int total_lines = 0; /* Total number of newlines in all buffers. */
- int errors = 0;
-
- first = last = (LBUFFER *) xmalloc (sizeof (LBUFFER));
- first->nbytes = first->nlines = 0;
- first->next = NULL;
- tmp = (LBUFFER *) xmalloc (sizeof (LBUFFER));
-
- /* Input is always read into a fresh buffer. */
- while ((tmp->nbytes = safe_read (fd, tmp->buffer, BUFSIZ)) > 0)
- {
- tmp->nlines = 0;
- tmp->next = NULL;
-
- /* Count the number of newlines just read. */
- for (i = 0; i < tmp->nbytes; i++)
- if (tmp->buffer[i] == '\n')
- ++tmp->nlines;
- total_lines += tmp->nlines;
-
- /* If there is enough room in the last buffer read, just append the new
- one to it. This is because when reading from a pipe, `nbytes' can
- often be very small. */
- if (tmp->nbytes + last->nbytes < BUFSIZ)
- {
- memcpy (&last->buffer[last->nbytes], tmp->buffer, tmp->nbytes);
- last->nbytes += tmp->nbytes;
- last->nlines += tmp->nlines;
- }
- else
- {
- /* If there's not enough room, link the new buffer onto the end of
- the list, then either free up the oldest buffer for the next
- read if that would leave enough lines, or else malloc a new one.
- Some compaction mechanism is possible but probably not
- worthwhile. */
- last = last->next = tmp;
- if (total_lines - first->nlines > n_lines)
- {
- tmp = first;
- total_lines -= first->nlines;
- first = first->next;
- }
- else
- tmp = (LBUFFER *) xmalloc (sizeof (LBUFFER));
- }
- }
- if (tmp->nbytes == -1)
- {
- error (0, errno, "%s", filename);
- errors = 1;
- free ((char *) tmp);
- goto free_lbuffers;
- }
-
- free ((char *) tmp);
-
- /* This prevents a core dump when the pipe contains no newlines. */
- if (n_lines == 0)
- goto free_lbuffers;
-
- /* Count the incomplete line on files that don't end with a newline. */
- if (last->buffer[last->nbytes - 1] != '\n')
- {
- ++last->nlines;
- ++total_lines;
- }
-
- /* Run through the list, printing lines. First, skip over unneeded
- buffers. */
- for (tmp = first; total_lines - tmp->nlines > n_lines; tmp = tmp->next)
- total_lines -= tmp->nlines;
-
- /* Find the correct beginning, then print the rest of the file. */
- if (total_lines > n_lines)
- {
- char *cp;
-
- /* Skip `total_lines' - `n_lines' newlines. We made sure that
- `total_lines' - `n_lines' <= `tmp->nlines'. */
- cp = tmp->buffer;
- for (i = total_lines - n_lines; i; --i)
- while (*cp++ != '\n')
- /* Do nothing. */ ;
- i = cp - tmp->buffer;
- }
- else
- i = 0;
- XWRITE (STDOUT_FILENO, &tmp->buffer[i], tmp->nbytes - i);
-
- for (tmp = tmp->next; tmp; tmp = tmp->next)
- XWRITE (STDOUT_FILENO, tmp->buffer, tmp->nbytes);
-
-free_lbuffers:
- while (first)
- {
- tmp = first->next;
- free ((char *) first);
- first = tmp;
- }
- return errors;
-}
-
-/* Print the last N_BYTES characters from the end of pipe FD.
- This is a stripped down version of pipe_lines.
- Return 0 if successful, 1 if an error occurred. */
-
-static int
-pipe_bytes (const char *filename, int fd, off_t n_bytes)
-{
- struct charbuffer
- {
- int nbytes;
- char buffer[BUFSIZ];
- struct charbuffer *next;
- };
- typedef struct charbuffer CBUFFER;
- CBUFFER *first, *last, *tmp;
- int i; /* Index into buffers. */
- int total_bytes = 0; /* Total characters in all buffers. */
- int errors = 0;
-
- first = last = (CBUFFER *) xmalloc (sizeof (CBUFFER));
- first->nbytes = 0;
- first->next = NULL;
- tmp = (CBUFFER *) xmalloc (sizeof (CBUFFER));
-
- /* Input is always read into a fresh buffer. */
- while ((tmp->nbytes = safe_read (fd, tmp->buffer, BUFSIZ)) > 0)
- {
- tmp->next = NULL;
-
- total_bytes += tmp->nbytes;
- /* If there is enough room in the last buffer read, just append the new
- one to it. This is because when reading from a pipe, `nbytes' can
- often be very small. */
- if (tmp->nbytes + last->nbytes < BUFSIZ)
- {
- memcpy (&last->buffer[last->nbytes], tmp->buffer, tmp->nbytes);
- last->nbytes += tmp->nbytes;
- }
- else
- {
- /* If there's not enough room, link the new buffer onto the end of
- the list, then either free up the oldest buffer for the next
- read if that would leave enough characters, or else malloc a new
- one. Some compaction mechanism is possible but probably not
- worthwhile. */
- last = last->next = tmp;
- if (total_bytes - first->nbytes > n_bytes)
- {
- tmp = first;
- total_bytes -= first->nbytes;
- first = first->next;
- }
- else
- {
- tmp = (CBUFFER *) xmalloc (sizeof (CBUFFER));
- }
- }
- }
- if (tmp->nbytes == -1)
- {
- error (0, errno, "%s", filename);
- errors = 1;
- free ((char *) tmp);
- goto free_cbuffers;
- }
-
- free ((char *) tmp);
-
- /* Run through the list, printing characters. First, skip over unneeded
- buffers. */
- for (tmp = first; total_bytes - tmp->nbytes > n_bytes; tmp = tmp->next)
- total_bytes -= tmp->nbytes;
-
- /* Find the correct beginning, then print the rest of the file.
- We made sure that `total_bytes' - `n_bytes' <= `tmp->nbytes'. */
- if (total_bytes > n_bytes)
- i = total_bytes - n_bytes;
- else
- i = 0;
- XWRITE (STDOUT_FILENO, &tmp->buffer[i], tmp->nbytes - i);