-#include <ctype.h>
-#define BB_DECLARE_EXTERN
-#define bb_need_help
-#include "messages.c"
-
-
-#define XWRITE(fd, buffer, n_bytes) \
- do { \
- if (n_bytes > 0 && fwrite ((buffer), 1, (n_bytes), stdout) == 0) \
- errorMsg("write error"); \
- } while (0)
-
-/* Number of items to tail. */
-#define DEFAULT_N_LINES 10
-
-/* Size of atomic reads. */
-#ifndef BUFSIZ
-#define BUFSIZ (512 * 8)
-#endif
-
-/* If nonzero, read from the end of one file until killed. */
-static int forever;
-
-/* If nonzero, print filename headers. */
-static int print_headers;
-
-const char tail_usage[] =
- "tail [OPTION] [FILE]...\n"
-#ifndef BB_FEATURE_TRIVIAL_HELP
- "\nPrint last 10 lines of each FILE to standard output.\n"
- "With more than one FILE, precede each with a header giving the\n"
- "file name. With no FILE, or when FILE is -, read standard input.\n\n"
- "Options:\n"
- "\t-n NUM\t\tPrint last NUM lines instead of first 10\n"
-
- "\t-f\t\tOutput data as the file grows. This version\n"
- "\t\t\tof 'tail -f' supports only one file at a time.\n"
-#endif
- ;
-
-
-static void write_header(const char *filename)
-{
- static int first_file = 1;
-
- printf("%s==> %s <==\n", (first_file ? "" : "\n"), filename);
- 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(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 = fullRead(fd, buffer, bytes_read);
- if (bytes_read == -1)
- errorMsg("read error");
-
- /* 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 = fullRead(fd, buffer, BUFSIZ)) > 0);
- if (bytes_read == -1)
- errorMsg("read error");
-
- 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(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)
- errorMsg("read error");
-
- 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;
-}
-
-/* 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(int fd)
-{
- char buffer[BUFSIZ];
- int bytes_read;
- long total;