Protoize.
authorJim Meyering <jim@meyering.net>
Mon, 16 Oct 1995 21:30:25 +0000 (21:30 +0000)
committerJim Meyering <jim@meyering.net>
Mon, 16 Oct 1995 21:30:25 +0000 (21:30 +0000)
Reorder functions to obviate forward dcls.
Remove forward dcls.

src/tail.c

index be41195..1613412 100644 (file)
@@ -107,20 +107,6 @@ enum header_mode
 char *xmalloc ();
 int safe_read ();
 
-static int file_lines ();
-static int pipe_bytes ();
-static int pipe_lines ();
-static int start_bytes ();
-static int start_lines ();
-static int tail ();
-static int tail_bytes ();
-static int tail_file ();
-static int tail_lines ();
-static long dump_remainder ();
-static void tail_forever ();
-static void usage ();
-static void write_header ();
-
 /* The name this program was run with.  */
 char *program_name;
 
@@ -146,320 +132,491 @@ static struct option const long_options[] =
   {NULL, 0, NULL, 0}
 };
 
-void
-main (argc, argv)
-     int argc;
-     char **argv;
+static void
+usage (int status)
 {
-  enum header_mode header_mode = multiple_files;
-  int exit_status = 0;
-  /* If from_start, the number of items to skip before printing; otherwise,
-     the number of items at the end of the file to print.  Initially, -1
-     means the value has not been set.  */
-  off_t n_units = -1;
-  long int tmp_long;
-  int c;                       /* Option character.  */
-  int fileind;                 /* Index in ARGV of first file name.  */
-
-  program_name = argv[0];
-  have_read_stdin = 0;
-  count_lines = 1;
-  forever = forever_multiple = from_start = print_headers = 0;
-
-  if (argc > 1
-      && ((argv[1][0] == '-' && ISDIGIT (argv[1][1]))
-         || (argv[1][0] == '+' && (ISDIGIT (argv[1][1]) || argv[1][1] == 0))))
+  if (status != 0)
+    fprintf (stderr, _("Try `%s --help' for more information.\n"),
+            program_name);
+  else
     {
-      /* Old option syntax: a dash or plus, one or more digits (zero digits
-        are acceptable with a plus), and one or more option letters.  */
-      if (argv[1][0] == '+')
-       from_start = 1;
-      if (argv[1][1] != '\0')
-       {
-         strtol_error s_err;
-         char *p;
+      printf (_("\
+Usage: %s [OPTION]... [FILE]...\n\
+"),
+             program_name);
+      printf (_("\
+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\
+"));
+    }
+  exit (status);
+}
 
-         s_err = xstrtol (++argv[1], &p, 0, &tmp_long, "bkm");
-         n_units = tmp_long;
-         if (s_err == LONGINT_OVERFLOW)
-           {
-             STRTOL_FATAL_ERROR (argv[1], _("argument"), s_err);
-           }
-         /* Parse any appended option letters.  */
-         while (*p)
-           {
-             switch (*p)
-               {
-               case 'c':
-                 /* Interpret N_UNITS as # of bytes.  */
-                 count_lines = 0;
-                 break;
+static void
+write_header (const char *filename, const char *comment)
+{
+  static int first_file = 1;
 
-               case 'f':
-                 forever = 1;
-                 break;
+  printf ("%s==> %s%s%s <==\n", (first_file ? "" : "\n"), filename,
+         (comment ? ": " : ""),
+         (comment ? comment : ""));
+  first_file = 0;
+}
 
-               case 'l':
-                 count_lines = 1;
-                 break;
+/* 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.  */
 
-               case 'q':
-                 header_mode = never;
-                 break;
+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.  */
 
-               case 'v':
-                 header_mode = always;
-                 break;
+  if (n_lines == 0)
+    return 0;
 
-               default:
-                 error (0, 0, _("unrecognized option `-%c'"), *p);
-                 usage (1);
-               }
-             ++p;
-           }
-       }
-      /* Make the options we just parsed invisible to getopt.  */
-      argv[1] = argv[0];
-      argv++;
-      argc--;
+  /* 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;
     }
 
-  while ((c = getopt_long (argc, argv, "c:n:fqv", long_options, (int *) 0))
-        != EOF)
-    {
-      strtol_error s_err;
+  /* Count the incomplete line on files that don't end with a newline.  */
+  if (bytes_read && buffer[bytes_read - 1] != '\n')
+    --n_lines;
 
-      switch (c)
+  do
+    {
+      /* Scan backward, counting the newlines in this bufferfull.  */
+      for (i = bytes_read - 1; i >= 0; i--)
        {
-       case 0:
-         break;
-
-       case 'c':
-         count_lines = 0;
-         goto getnum;
-
-       case 'n':
-         count_lines = 1;
-       getnum:
-         if (*optarg == '+')
-           {
-             from_start = 1;
-           }
-
-         s_err = xstrtol (optarg, NULL, 0, &tmp_long, "bkm");
-         if (tmp_long < 0)
-           tmp_long = -tmp_long;
-         n_units = tmp_long;
-         if (s_err != LONGINT_OK)
+         /* Have we counted the requested number of newlines yet?  */
+         if (buffer[i] == '\n' && n_lines-- == 0)
            {
-             STRTOL_FATAL_ERROR (optarg, (c == 'n'
-                                          ? _("number of lines")
-                                          : _("number of bytes")), s_err);
+             /* 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;
            }
-         break;
-
-       case 'f':
-         forever = 1;
-         break;
-
-       case 'q':
-         header_mode = never;
-         break;
-
-       case 'v':
-         header_mode = always;
-         break;
-
-       default:
-         usage (1);
        }
+      /* 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);
     }
-
-  if (show_version)
+  while ((bytes_read = safe_read (fd, buffer, BUFSIZ)) > 0);
+  if (bytes_read == -1)
     {
-      printf ("tail - %s\n", version_string);
-      exit (0);
+      error (0, errno, "%s", filename);
+      return 1;
     }
+  return 0;
+}
 
-  if (show_help)
-    usage (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.  */
 
-  if (n_units == -1)
-    n_units = DEFAULT_N_LINES;
+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;
 
-  /* To start printing with item N_UNITS from the start of the file, skip
-     N_UNITS - 1 items.  `tail +0' is actually meaningless, but for Unix
-     compatibility it's treated the same as `tail +1'.  */
-  if (from_start)
+  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)
     {
-      if (n_units)
-       --n_units;
-    }
+      tmp->nlines = 0;
+      tmp->next = NULL;
 
-  fileind = optind;
+      /* 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 (optind < argc - 1 && forever)
+      /* 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)
     {
-      forever_multiple = 1;
-      forever = 0;
-      file_descs = (int *) xmalloc ((argc - optind) * sizeof (int));
-      file_sizes = (off_t *) xmalloc ((argc - optind) * sizeof (off_t));
+      error (0, errno, "%s", filename);
+      errors = 1;
+      free ((char *) tmp);
+      goto free_lbuffers;
     }
 
-  if (header_mode == always
-      || (header_mode == multiple_files && optind < argc - 1))
-    print_headers = 1;
+  free ((char *) tmp);
 
-  if (optind == argc)
-    exit_status |= tail_file ("-", n_units, 0);
+  /* This prevents a core dump when the pipe contains no newlines.  */
+  if (n_lines == 0)
+    goto free_lbuffers;
 
-  for (; optind < argc; ++optind)
-    exit_status |= tail_file (argv[optind], n_units, optind - fileind);
+  /* Count the incomplete line on files that don't end with a newline.  */
+  if (last->buffer[last->nbytes - 1] != '\n')
+    {
+      ++last->nlines;
+      ++total_lines;
+    }
 
-  if (forever_multiple)
-    tail_forever (argv + fileind, argc - fileind);
+  /* 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;
 
-  if (have_read_stdin && close (0) < 0)
-    error (1, errno, "-");
-  if (fclose (stdout) == EOF)
-    error (1, errno, _("write error"));
-  exit (exit_status);
+  /* 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 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.
+/* 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
-tail_file (filename, n_units, filenum)
-     const char *filename;
-     off_t n_units;
-     int filenum;
+pipe_bytes (const char *filename, int fd, off_t n_bytes)
 {
-  int fd, errors;
-  struct stat stats;
+  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;
 
-  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
+  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)
     {
-      /* Not standard input.  */
-      fd = open (filename, O_RDONLY);
-      if (fd == -1)
+      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)
        {
-         if (forever_multiple)
-           file_descs[filenum] = -1;
-         error (0, errno, "%s", filename);
-         errors = 1;
+         memcpy (&last->buffer[last->nbytes], tmp->buffer, tmp->nbytes);
+         last->nbytes += tmp->nbytes;
        }
       else
        {
-         if (print_headers)
-           write_header (filename, NULL);
-         errors = tail (filename, fd, n_units);
-         if (forever_multiple)
+         /* 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)
            {
-             if (fstat (fd, &stats) < 0)
-               {
-                 error (0, errno, "%s", filename);
-                 errors = 1;
-               }
-             else if (!S_ISREG (stats.st_mode))
-               {
-                 error (0, 0, _("%s: cannot follow end of non-regular file"),
-                        filename);
-                 errors = 1;
-               }
-             if (errors)
-               {
-                 close (fd);
-                 file_descs[filenum] = -1;
-               }
-             else
-               {
-                 file_descs[filenum] = fd;
-                 file_sizes[filenum] = stats.st_size;
-               }
+             tmp = first;
+             total_bytes -= first->nbytes;
+             first = first->next;
            }
          else
            {
-             if (close (fd))
-               {
-                 error (0, errno, "%s", filename);
-                 errors = 1;
-               }
+             tmp = (CBUFFER *) xmalloc (sizeof (CBUFFER));
            }
        }
     }
+  if (tmp->nbytes == -1)
+    {
+      error (0, errno, "%s", filename);
+      errors = 1;
+      free ((char *) tmp);
+      goto free_cbuffers;
+    }
 
-  return errors;
-}
+  free ((char *) tmp);
 
-static void
-write_header (filename, comment)
-     const char *filename;
-     const char *comment;
-{
-  static int first_file = 1;
+  /* 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;
 
-  printf ("%s==> %s%s%s <==\n", (first_file ? "" : "\n"), filename,
-         (comment ? ": " : ""),
-         (comment ? comment : ""));
-  first_file = 0;
+  /* 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;
 }
 
-/* Display the last N_UNITS units of file FILENAME, open for reading
-   in FD.
-   Return 0 if successful, 1 if an error occurred.  */
+/* 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
-tail (filename, fd, n_units)
-     const char *filename;
-     int fd;
-     off_t n_units;
+start_bytes (const char *filename, int fd, off_t n_bytes)
 {
-  if (count_lines)
-    return tail_lines (filename, fd, (long) n_units);
-  else
-    return tail_bytes (filename, fd, n_units);
-}
-
+  char buffer[BUFSIZ];
+  int bytes_read = 0;
+
+  while (n_bytes > 0 && (bytes_read = safe_read (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 = safe_read (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 = safe_read (fd, buffer, BUFSIZ)) > 0)
+    {
+      XWRITE (STDOUT_FILENO, buffer, bytes_read);
+      total += bytes_read;
+    }
+  if (bytes_read == -1)
+    error (1, errno, "%s", filename);
+  if (forever)
+    {
+      fflush (stdout);
+      sleep (1);
+      goto output;
+    }
+  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 (filename, fd, n_bytes)
-     const char *filename;
-     int fd;
-     off_t n_bytes;
+tail_bytes (const char *filename, int fd, off_t n_bytes)
 {
   struct stat stats;
 
@@ -528,10 +685,7 @@ tail_bytes (filename, fd, n_bytes)
    Return 0 if successful, 1 if an error occurred.  */
 
 static int
-tail_lines (filename, fd, n_lines)
-     const char *filename;
-     int fd;
-     long n_lines;
+tail_lines (const char *filename, int fd, long int n_lines)
 {
   struct stat stats;
   off_t length;
@@ -570,492 +724,287 @@ tail_lines (filename, fd, n_lines)
   return 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).
+/* 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
-file_lines (filename, fd, n_lines, pos)
-     const char *filename;
-     int fd;
-     long n_lines;
-     off_t pos;
+tail (const char *filename, int fd, off_t n_units)
 {
-  char buffer[BUFSIZ];
-  int bytes_read;
-  int i;                       /* Index into `buffer' for scanning.  */
-
-  if (n_lines == 0)
-    return 0;
+  if (count_lines)
+    return tail_lines (filename, fd, (long) n_units);
+  else
+    return tail_bytes (filename, fd, n_units);
+}
 
-  /* 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;
-    }
+/* 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.  */
 
-  /* Count the incomplete line on files that don't end with a newline.  */
-  if (bytes_read && buffer[bytes_read - 1] != '\n')
-    --n_lines;
+static int
+tail_file (const char *filename, off_t n_units, int filenum)
+{
+  int fd, errors;
+  struct stat stats;
 
-  do
+  if (!strcmp (filename, "-"))
     {
-      /* Scan backward, counting the newlines in this bufferfull.  */
-      for (i = bytes_read - 1; i >= 0; i--)
+      have_read_stdin = 1;
+      filename = _("standard input");
+      if (print_headers)
+       write_header (filename, NULL);
+      errors = tail (filename, 0, n_units);
+      if (forever_multiple)
        {
-         /* Have we counted the requested number of newlines yet?  */
-         if (buffer[i] == '\n' && n_lines-- == 0)
+         if (fstat (0, &stats) < 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;
+             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;
            }
        }
-      /* 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)
+  else
     {
-      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 (filename, fd, n_lines)
-     const char *filename;
-     int fd;
-     long 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)
+      /* Not standard input.  */
+      fd = open (filename, O_RDONLY);
+      if (fd == -1)
        {
-         memcpy (&last->buffer[last->nbytes], tmp->buffer, tmp->nbytes);
-         last->nbytes += tmp->nbytes;
-         last->nlines += tmp->nlines;
+         if (forever_multiple)
+           file_descs[filenum] = -1;
+         error (0, errno, "%s", filename);
+         errors = 1;
        }
       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)
+         if (print_headers)
+           write_header (filename, NULL);
+         errors = tail (filename, fd, n_units);
+         if (forever_multiple)
            {
-             tmp = first;
-             total_lines -= first->nlines;
-             first = first->next;
+             if (fstat (fd, &stats) < 0)
+               {
+                 error (0, errno, "%s", filename);
+                 errors = 1;
+               }
+             else if (!S_ISREG (stats.st_mode))
+               {
+                 error (0, 0, _("%s: cannot follow end of non-regular file"),
+                        filename);
+                 errors = 1;
+               }
+             if (errors)
+               {
+                 close (fd);
+                 file_descs[filenum] = -1;
+               }
+             else
+               {
+                 file_descs[filenum] = fd;
+                 file_sizes[filenum] = stats.st_size;
+               }
            }
          else
-           tmp = (LBUFFER *) xmalloc (sizeof (LBUFFER));
+           {
+             if (close (fd))
+               {
+                 error (0, errno, "%s", filename);
+                 errors = 1;
+               }
+           }
        }
     }
-  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 (filename, fd, n_bytes)
-     const char *filename;
-     int fd;
-     off_t n_bytes;
+void
+main (int argc, char **argv)
 {
-  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;
+  enum header_mode header_mode = multiple_files;
+  int exit_status = 0;
+  /* If from_start, the number of items to skip before printing; otherwise,
+     the number of items at the end of the file to print.  Initially, -1
+     means the value has not been set.  */
+  off_t n_units = -1;
+  long int tmp_long;
+  int c;                       /* Option character.  */
+  int fileind;                 /* Index in ARGV of first file name.  */
 
-  first = last = (CBUFFER *) xmalloc (sizeof (CBUFFER));
-  first->nbytes = 0;
-  first->next = NULL;
-  tmp = (CBUFFER *) xmalloc (sizeof (CBUFFER));
+  program_name = argv[0];
+  have_read_stdin = 0;
+  count_lines = 1;
+  forever = forever_multiple = from_start = print_headers = 0;
 
-  /* Input is always read into a fresh buffer.  */
-  while ((tmp->nbytes = safe_read (fd, tmp->buffer, BUFSIZ)) > 0)
+  if (argc > 1
+      && ((argv[1][0] == '-' && ISDIGIT (argv[1][1]))
+         || (argv[1][0] == '+' && (ISDIGIT (argv[1][1]) || argv[1][1] == 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
+      /* Old option syntax: a dash or plus, one or more digits (zero digits
+        are acceptable with a plus), and one or more option letters.  */
+      if (argv[1][0] == '+')
+       from_start = 1;
+      if (argv[1][1] != '\0')
        {
-         /* 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)
+         strtol_error s_err;
+         char *p;
+
+         s_err = xstrtol (++argv[1], &p, 0, &tmp_long, "bkm");
+         n_units = tmp_long;
+         if (s_err == LONGINT_OVERFLOW)
            {
-             tmp = first;
-             total_bytes -= first->nbytes;
-             first = first->next;
+             STRTOL_FATAL_ERROR (argv[1], _("argument"), s_err);
            }
-         else
+         /* Parse any appended option letters.  */
+         while (*p)
            {
-             tmp = (CBUFFER *) xmalloc (sizeof (CBUFFER));
-           }
-       }
-    }
-  if (tmp->nbytes == -1)
-    {
-      error (0, errno, "%s", filename);
-      errors = 1;
-      free ((char *) tmp);
-      goto free_cbuffers;
-    }
+             switch (*p)
+               {
+               case 'c':
+                 /* Interpret N_UNITS as # of bytes.  */
+                 count_lines = 0;
+                 break;
 
-  free ((char *) tmp);
+               case 'f':
+                 forever = 1;
+                 break;
 
-  /* 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;
+               case 'l':
+                 count_lines = 1;
+                 break;
 
-  /* 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);
+               case 'q':
+                 header_mode = never;
+                 break;
 
-  for (tmp = tmp->next; tmp; tmp = tmp->next)
-    XWRITE (STDOUT_FILENO, tmp->buffer, tmp->nbytes);
+               case 'v':
+                 header_mode = always;
+                 break;
 
-free_cbuffers:
-  while (first)
-    {
-      tmp = first->next;
-      free ((char *) first);
-      first = tmp;
+               default:
+                 error (0, 0, _("unrecognized option `-%c'"), *p);
+                 usage (1);
+               }
+             ++p;
+           }
+       }
+      /* Make the options we just parsed invisible to getopt.  */
+      argv[1] = argv[0];
+      argv++;
+      argc--;
     }
-  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.  */
+  while ((c = getopt_long (argc, argv, "c:n:fqv", long_options, (int *) 0))
+        != EOF)
+    {
+      strtol_error s_err;
 
-static int
-start_bytes (filename, fd, n_bytes)
-     const char *filename;
-     int fd;
-     off_t n_bytes;
-{
-  char buffer[BUFSIZ];
-  int bytes_read = 0;
+      switch (c)
+       {
+       case 0:
+         break;
 
-  while (n_bytes > 0 && (bytes_read = safe_read (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;
-}
+       case 'c':
+         count_lines = 0;
+         goto getnum;
 
-/* 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.  */
+       case 'n':
+         count_lines = 1;
+       getnum:
+         if (*optarg == '+')
+           {
+             from_start = 1;
+           }
 
-static int
-start_lines (filename, fd, n_lines)
-     const char *filename;
-     int fd;
-     long n_lines;
-{
-  char buffer[BUFSIZ];
-  int bytes_read = 0;
-  int bytes_to_skip = 0;
+         s_err = xstrtol (optarg, NULL, 0, &tmp_long, "bkm");
+         if (tmp_long < 0)
+           tmp_long = -tmp_long;
+         n_units = tmp_long;
+         if (s_err != LONGINT_OK)
+           {
+             STRTOL_FATAL_ERROR (optarg, (c == 'n'
+                                          ? _("number of lines")
+                                          : _("number of bytes")), s_err);
+           }
+         break;
 
-  while (n_lines && (bytes_read = safe_read (fd, buffer, BUFSIZ)) > 0)
-    {
-      bytes_to_skip = 0;
-      while (bytes_to_skip < bytes_read)
-       if (buffer[bytes_to_skip++] == '\n' && --n_lines == 0)
+       case 'f':
+         forever = 1;
          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.  */
+       case 'q':
+         header_mode = never;
+         break;
 
-static long
-dump_remainder (filename, fd)
-     const char *filename;
-     int fd;
-{
-  char buffer[BUFSIZ];
-  int bytes_read;
-  long total;
+       case 'v':
+         header_mode = always;
+         break;
 
-  total = 0;
-output:
-  while ((bytes_read = safe_read (fd, buffer, BUFSIZ)) > 0)
-    {
-      XWRITE (STDOUT_FILENO, buffer, bytes_read);
-      total += bytes_read;
+       default:
+         usage (1);
+       }
     }
-  if (bytes_read == -1)
-    error (1, errno, "%s", filename);
-  if (forever)
+
+  if (show_version)
     {
-      fflush (stdout);
-      sleep (1);
-      goto output;
+      printf ("tail - %s\n", version_string);
+      exit (0);
     }
-  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 (names, nfiles)
-     char **names;
-     int nfiles;
-{
-  int last;
+  if (show_help)
+    usage (0);
 
-  last = -1;
+  if (n_units == -1)
+    n_units = DEFAULT_N_LINES;
 
-  while (1)
+  /* To start printing with item N_UNITS from the start of the file, skip
+     N_UNITS - 1 items.  `tail +0' is actually meaningless, but for Unix
+     compatibility it's treated the same as `tail +1'.  */
+  if (from_start)
     {
-      int i;
-      int changed;
-
-      changed = 0;
-      for (i = 0; i < nfiles; i++)
-       {
-         struct stat stats;
+      if (n_units)
+       --n_units;
+    }
 
-         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;
+  fileind = optind;
 
-         /* This file has changed size.  Print out what we can, and
-            then keep looping.  */
+  if (optind < argc - 1 && forever)
+    {
+      forever_multiple = 1;
+      forever = 0;
+      file_descs = (int *) xmalloc ((argc - optind) * sizeof (int));
+      file_sizes = (off_t *) xmalloc ((argc - optind) * sizeof (off_t));
+    }
 
-         changed = 1;
+  if (header_mode == always
+      || (header_mode == multiple_files && optind < argc - 1))
+    print_headers = 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 (optind == argc)
+    exit_status |= tail_file ("-", n_units, 0);
 
-         if (i != last)
-           {
-             if (print_headers)
-               write_header (names[i], NULL);
-             last = i;
-           }
-         file_sizes[i] += dump_remainder (names[i], file_descs[i]);
-       }
+  for (; optind < argc; ++optind)
+    exit_status |= tail_file (argv[optind], n_units, optind - fileind);
 
-      /* If none of the files changed size, sleep.  */
-      if (! changed)
-       sleep (1);
-    }
-}
+  if (forever_multiple)
+    tail_forever (argv + fileind, argc - fileind);
 
-static void
-usage (status)
-     int status;
-{
-  if (status != 0)
-    fprintf (stderr, _("Try `%s --help' for more information.\n"),
-            program_name);
-  else
-    {
-      printf (_("\
-Usage: %s [OPTION]... [FILE]...\n\
-"),
-             program_name);
-      printf (_("\
-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\
-"));
-    }
-  exit (status);
+  if (have_read_stdin && close (0) < 0)
+    error (1, errno, "-");
+  if (fclose (stdout) == EOF)
+    error (1, errno, _("write error"));
+  exit (exit_status);
 }