- /* 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 = fullRead (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 = fullRead (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 = fullRead (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);
-
- for (tmp = tmp->next; tmp; tmp = tmp->next)
- XWRITE (STDOUT_FILENO, tmp->buffer, tmp->nbytes);
-
-free_cbuffers:
- while (first)
- {
- tmp = first->next;
- free ((char *) first);
- first = tmp;
- }
- return errors;
-}
-
-/* Skip N_BYTES characters from the start of pipe FD, and print
- any extra characters that were read beyond that.
- Return 1 on error, 0 if ok. */
-
-static int
-start_bytes (const char *filename, int fd, off_t n_bytes)
-{
- char buffer[BUFSIZ];
- int bytes_read = 0;
-
- while (n_bytes > 0 && (bytes_read = fullRead (fd, buffer, BUFSIZ)) > 0)
- n_bytes -= bytes_read;
- if (bytes_read == -1)
- {
- error (0, errno, "%s", filename);
- return 1;
- }
- else if (n_bytes < 0)
- XWRITE (STDOUT_FILENO, &buffer[bytes_read + n_bytes], -n_bytes);
- return 0;
-}
-
-/* Skip N_LINES lines at the start of file or pipe FD, and print
- any extra characters that were read beyond that.
- Return 1 on error, 0 if ok. */
-
-static int
-start_lines (const char *filename, int fd, long int n_lines)
-{
- char buffer[BUFSIZ];
- int bytes_read = 0;
- int bytes_to_skip = 0;
-
- while (n_lines && (bytes_read = fullRead (fd, buffer, BUFSIZ)) > 0)
- {
- bytes_to_skip = 0;
- while (bytes_to_skip < bytes_read)
- if (buffer[bytes_to_skip++] == '\n' && --n_lines == 0)
- break;
- }
- if (bytes_read == -1)
- {
- error (0, errno, "%s", filename);
- return 1;
- }
- else if (bytes_to_skip < bytes_read)
- {
- XWRITE (STDOUT_FILENO, &buffer[bytes_to_skip],
- bytes_read - bytes_to_skip);
- }
- return 0;
-}
-
-/* Display file FILENAME from the current position in FD to the end.
- If `forever' is nonzero, keep reading from the end of the file
- until killed. Return the number of bytes read from the file. */
-
-static long
-dump_remainder (const char *filename, int fd)
-{
- char buffer[BUFSIZ];
- int bytes_read;
- long total;
-
- total = 0;
-output:
- while ((bytes_read = fullRead (fd, buffer, BUFSIZ)) > 0)
- {
- XWRITE (STDOUT_FILENO, buffer, bytes_read);
- total += bytes_read;
- }
- if (bytes_read == -1)
- error (EXIT_FAILURE, errno, "%s", filename);
- if (forever)
- {
- fflush (stdout);
- sleep (1);
- goto output;
- }
- else
- {
- if (forever_multiple)
- fflush (stdout);
- }
-
- return total;
-}
-
-/* Tail NFILES (>1) files forever until killed. The file names are in
- NAMES. The open file descriptors are in `file_descs', and the size
- at which we stopped tailing them is in `file_sizes'. We loop over
- each of them, doing an fstat to see if they have changed size. If
- none of them have changed size in one iteration, we sleep for a
- second and try again. We do this until the user interrupts us. */
-
-static void
-tail_forever (char **names, int nfiles)
-{
- int last;
-
- last = -1;
-
- while (1)
- {
- int i;
- int changed;
-
- changed = 0;
- for (i = 0; i < nfiles; i++)
- {
- struct stat stats;
-
- if (file_descs[i] < 0)
- continue;
- if (fstat (file_descs[i], &stats) < 0)
- {
- error (0, errno, "%s", names[i]);
- file_descs[i] = -1;
- continue;
- }
- if (stats.st_size == file_sizes[i])
- continue;
-
- /* This file has changed size. Print out what we can, and
- then keep looping. */
-
- changed = 1;
-
- if (stats.st_size < file_sizes[i])
- {
- write_header (names[i], "file truncated");
- last = i;
- lseek (file_descs[i], stats.st_size, SEEK_SET);
- file_sizes[i] = stats.st_size;
- continue;
- }
-
- if (i != last)
- {
- if (print_headers)
- write_header (names[i], NULL);
- last = i;
- }
- file_sizes[i] += dump_remainder (names[i], file_descs[i]);
- }
-
- /* If none of the files changed size, sleep. */
- if (! changed)
- sleep (1);
- }
-}
-
-/* Output the last N_BYTES bytes of file FILENAME open for reading in FD.
- Return 0 if successful, 1 if an error occurred. */
-
-static int
-tail_bytes (const char *filename, int fd, off_t n_bytes)
-{
- struct stat stats;
-
- /* FIXME: resolve this like in dd.c. */
- /* Use fstat instead of checking for errno == ESPIPE because
- lseek doesn't work on some special files but doesn't return an
- error, either. */
- if (fstat (fd, &stats))
- {
- error (0, errno, "%s", filename);
- return 1;
- }
-
- if (from_start)
- {
- if (S_ISREG (stats.st_mode))
- lseek (fd, n_bytes, SEEK_CUR);
- else if (start_bytes (filename, fd, n_bytes))
- return 1;
- dump_remainder (filename, fd);
- }
- else
- {
- if (S_ISREG (stats.st_mode))
- {
- off_t current_pos, end_pos;
- size_t bytes_remaining;
-
- if ((current_pos = lseek (fd, (off_t) 0, SEEK_CUR)) != -1
- && (end_pos = lseek (fd, (off_t) 0, SEEK_END)) != -1)
- {
- off_t diff;
- /* Be careful here. The current position may actually be
- beyond the end of the file. */
- bytes_remaining = (diff = end_pos - current_pos) < 0 ? 0 : diff;
- }
- else
- {
- error (0, errno, "%s", filename);
- return 1;
- }
-
- if (bytes_remaining <= n_bytes)
- {
- /* From the current position to end of file, there are no
- more bytes than have been requested. So reposition the
- file pointer to the incoming current position and print
- everything after that. */
- lseek (fd, current_pos, SEEK_SET);
- }
- else
- {
- /* There are more bytes remaining than were requested.
- Back up. */
- lseek (fd, -n_bytes, SEEK_END);
- }
- dump_remainder (filename, fd);
- }
- else
- return pipe_bytes (filename, fd, n_bytes);
- }
- return 0;
-}
-
-/* Output the last N_LINES lines of file FILENAME open for reading in FD.
- Return 0 if successful, 1 if an error occurred. */
-
-static int
-tail_lines (const char *filename, int fd, long int n_lines)
-{
- struct stat stats;
- off_t length;
-
- if (fstat (fd, &stats))
- {
- error (0, errno, "%s", filename);
- return 1;
- }
-
- if (from_start)
- {
- if (start_lines (filename, fd, n_lines))
- return 1;
- dump_remainder (filename, fd);
- }
- else
- {
- /* Use file_lines only if FD refers to a regular file with
- its file pointer positioned at beginning of file. */
- /* FIXME: adding the lseek conjunct is a kludge.
- Once there's a reasonable test suite, fix the true culprit:
- file_lines. file_lines shouldn't presume that the input
- file pointer is initially positioned to beginning of file. */
- if (S_ISREG (stats.st_mode)
- && lseek (fd, (off_t) 0, SEEK_CUR) == (off_t) 0)
- {
- length = lseek (fd, (off_t) 0, SEEK_END);
- if (length != 0 && file_lines (filename, fd, n_lines, length))
- return 1;
- dump_remainder (filename, fd);
- }
- else
- return pipe_lines (filename, fd, n_lines);
- }
- return 0;
-}
-
-/* Display the last N_UNITS units of file FILENAME, open for reading
- in FD.
- Return 0 if successful, 1 if an error occurred. */
-
-static int
-tail (const char *filename, int fd, off_t n_units)
-{
- if (count_lines)
- return tail_lines (filename, fd, (long) n_units);
- else
- return tail_bytes (filename, fd, n_units);
-}
-
-/* Display the last N_UNITS units of file FILENAME.
- "-" for FILENAME means the standard input.
- FILENUM is this file's index in the list of files the user gave.
- Return 0 if successful, 1 if an error occurred. */
-
-static int
-tail_file (const char *filename, off_t n_units, int filenum)
-{
- int fd, errors;
- struct stat stats;
-
- if (!strcmp (filename, "-"))
- {
- have_read_stdin = 1;
- filename = "standard input";
- if (print_headers)
- write_header (filename, NULL);
- errors = tail (filename, 0, n_units);
- if (forever_multiple)
- {
- if (fstat (0, &stats) < 0)
- {
- error (0, errno, "standard input");
- errors = 1;
- }
- else if (!S_ISREG (stats.st_mode))
- {
- error (0, 0,
- "standard input: cannot follow end of non-regular file");
- errors = 1;
- }
- if (errors)
- file_descs[filenum] = -1;
- else
- {
- file_descs[filenum] = 0;
- file_sizes[filenum] = stats.st_size;
- }
- }
- }
- else
- {
- /* Not standard input. */
- fd = open (filename, O_RDONLY);
- if (fd == -1)
- {
- if (forever_multiple)
- file_descs[filenum] = -1;
- error (0, errno, "%s", filename);
- errors = 1;
- }
- else
- {
- if (print_headers)
- write_header (filename, NULL);
- errors = tail (filename, fd, n_units);
- if (forever_multiple)
- {
- if (fstat (fd, &stats) < 0)
- {
- error (0, errno, "%s", filename);
- errors = 1;