home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / f / futi14as.zip / TAIL.C < prev    next >
C/C++ Source or Header  |  1992-02-22  |  26KB  |  1,027 lines

  1. /* tail -- output last part of file(s)
  2.    Copyright (C) 1989, 1990 Free Software Foundation, Inc.
  3.  
  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 1, or (at your option)
  7.    any later version.
  8.  
  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.
  13.  
  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
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* MS-DOS port (c) 1990 by Thorsten Ohl, ohl@gnu.ai.mit.edu
  19.    This port is also distributed under the terms of the
  20.    GNU General Public License as published by the
  21.    Free Software Foundation.
  22.  
  23.    Please note that this file is not identical to the
  24.    original GNU release, you should have received this
  25.    code as patch to the official release.  */
  26.  
  27. #ifdef MSDOS
  28. static char RCS_Id[] =
  29.   "$Header: e:/gnu/fileutil/RCS/tail.c 1.4.0.4 90/09/19 12:09:20 tho Exp $";
  30.  
  31. static char Program_Id[] = "tail";
  32. static char RCS_Revision[] = "$Revision: 1.4.0.4 $";
  33.  
  34. #define VERSION \
  35.   "GNU %s, Version %.*s (compiled %s %s for MS-DOS)\n", Program_Id, \
  36.   (sizeof RCS_Revision - 14), (RCS_Revision + 11), __DATE__, __TIME__
  37.  
  38. #define COPYING \
  39.   "This is free software, distributed under the terms of the\n" \
  40.   "GNU General Public License.  For details, see the file COPYING.\n"
  41. #endif /* MSDOS */
  42.  
  43. /* Can display any amount of data, unlike the Unix version, which uses
  44.    a fixed size buffer and therefore can only deliver a limited number
  45.    of lines.
  46.  
  47.    Usage: tail [-b [+]#] [-c [+]#] [-n [+]#] [-fqv] [+blocks [+]#]
  48.           [+bytes [+]#] [+lines [+]#] [+follow] [+quiet] [+silent]
  49.           [+verbose] [file...]
  50.  
  51.           tail [+/-#bcflqv] [file...]
  52.  
  53.    Options:
  54.    -b, +blocks #    Tail by # 512-byte blocks.
  55.    -c, +bytes #        Tail by # bytes.
  56.    -f, +follow        Loop forever trying to read more characters at the
  57.             end of the file, on the assumption that the file
  58.             is growing.  Ignored if reading from a pipe.
  59.             Cannot be used if more than one file is given.
  60.    -l, -n, +lines #    Tail by # lines.
  61.    -q, +quiet, +silent    Never print filename headers.
  62.    -v, +verbose        Always print filename headers.
  63.  
  64.    If a number (#) starts with a `+', begin printing with the #th item
  65.    from the start of each file, instead of from the end.
  66.  
  67.    Reads from standard input if no files are given or when a filename of
  68.    ``-'' is encountered.
  69.    By default, filename headers are printed only more than one file
  70.    is given.
  71.    By default, prints the last 10 lines (tail -n 10).
  72.  
  73.    Started by Paul Rubin <phr@ai.mit.edu>
  74.    Finished by David MacKenzie <djm@ai.mit.edu> */
  75.  
  76. #include <stdio.h>
  77. #include <getopt.h>
  78. #include <ctype.h>
  79. #include <sys/types.h>
  80. #include "system.h"
  81.  
  82. #ifdef STDC_HEADERS
  83. #include <errno.h>
  84. #include <stdlib.h>
  85. #define ISDIGIT(c) (isdigit ((unsigned char) (c)))
  86. #else
  87. #define ISDIGIT(c) (isascii (c) && isdigit (c))
  88. char *malloc ();
  89. void free ();
  90.  
  91. extern int errno;
  92. #endif
  93.  
  94. #ifndef _POSIX_SOURCE
  95. long lseek();
  96. #endif
  97.  
  98. /* Number of items to tail. */
  99. #define DEFAULT_NUMBER 10
  100.  
  101. /* The number of bytes in a block (-b option). */
  102. #define BLOCKSIZE 512
  103.  
  104. /* Size of atomic reads. */
  105. #define BUFSIZE (BLOCKSIZE * 8)
  106.  
  107. /* Masks for the operation mode.  If neither BYTES nor BLOCKS is set,
  108.    tail operates by lines. */
  109. #define BYTES 1            /* Tail by characters. */
  110. #define BLOCKS 2        /* Tail by blocks. */
  111. #define FOREVER 4        /* Read from end of file forever. */
  112. #define START 8            /* Count from start of file instead of end. */
  113. #define HEADERS 16        /* Print filename headers. */
  114. #ifdef MSDOS
  115. #define BINARY 32        /* Suppress crlf translation. */
  116. #endif
  117.  
  118. /* When to print the filename banners. */
  119. enum header_mode
  120. {
  121.   multiple_files, always, never
  122. };
  123.  
  124. #ifdef MSDOS
  125.  
  126. #include <io.h>
  127. #include <string.h>
  128.  
  129. extern  void main (int, char **);
  130. extern  void write_header (char *);
  131. extern  int tail (char *, int, int, long);
  132. extern  int tail_file (char *, int, long);
  133. extern  int tail_bytes (char *, int, int, long);
  134. extern  int tail_lines (char *, int, int, long);
  135. extern  int file_lines (char *, int, long, long);
  136. extern  int pipe_lines (char *, int, long);
  137. extern  int pipe_bytes (char *, int, long);
  138. extern  int start_bytes (char *, int, long);
  139. extern  int start_lines (char *, int, long);
  140. extern  void dump_remainder (char *, int, int);
  141. extern  void xwrite (int, char *, int);
  142. extern  char *xmalloc (int);
  143. extern  long atou (char *);
  144. extern  char *basename (char *);
  145. extern  void error (int status, int errnum, char *message, ...);
  146. extern  void usage (void);
  147.  
  148. #else  /* not MSDOS */
  149.  
  150. char *xmalloc ();
  151. int file_lines ();
  152. int pipe_bytes ();
  153. int pipe_lines ();
  154. int start_bytes ();
  155. int start_lines ();
  156. int tail ();
  157. int tail_bytes ();
  158. int tail_file ();
  159. int tail_lines ();
  160. long atou();
  161. void dump_remainder ();
  162. void error ();
  163. void usage ();
  164. void write_header ();
  165. void xwrite ();
  166.  
  167. #endif /* not MSDOS */
  168.  
  169. /* The name this program was run with. */
  170. char *program_name;
  171.  
  172. struct option long_options[] =
  173. {
  174. #ifdef MSDOS
  175.   {"binary", 1, NULL, 'B'},
  176.   {"copying", 0, NULL, 30},
  177.   {"version", 0, NULL, 31},
  178. #endif
  179.   {"blocks", 1, NULL, 'b'},
  180.   {"bytes", 1, NULL, 'c'},
  181.   {"follow", 0, NULL, 'f'},
  182.   {"lines", 1, NULL, 'n'},
  183.   {"quiet", 0, NULL, 'q'},
  184.   {"silent", 0, NULL, 'q'},
  185.   {"verbose", 0, NULL, 'v'},
  186.   {NULL, 0, NULL, 0}
  187. };
  188.  
  189. void
  190. main (argc, argv)
  191.      int argc;
  192.      char **argv;
  193. {
  194.   enum header_mode header_mode = multiple_files;
  195.   int errors = 0;        /* Exit status. */
  196.   int mode = 0;            /* Flags. */
  197.   /* In START mode, the number of items to skip before printing; otherwise,
  198.      the number of items at the end of the file to print.  Initially, -1
  199.      means the value has not been set. */
  200.   long number = -1;
  201.   int c;            /* Option character. */
  202.   int longind;            /* Index in `long_options' of option found. */
  203.  
  204.   program_name = argv[0];
  205.  
  206.   if (argc > 1
  207.       && ((argv[1][0] == '-' && ISDIGIT (argv[1][1]))
  208.       || (argv[1][0] == '+' && (ISDIGIT (argv[1][1]) || argv[1][1] == 0))))
  209.     {
  210.       /* Old option syntax: a dash or plus, one or more digits (zero digits
  211.      are acceptable with a plus), and one or more option letters. */
  212.       if (argv[1][0] == '+')
  213.     mode |= START;
  214.       if (argv[1][1] != 0)
  215.     {
  216.       for (number = 0, ++argv[1]; ISDIGIT (*argv[1]); ++argv[1])
  217.         number = number * 10 + *argv[1] - '0';
  218.       /* Parse any appended option letters. */
  219.       while (*argv[1])
  220.         {
  221.           switch (*argv[1])
  222.         {
  223.         case 'b':
  224.           mode |= BLOCKS;
  225.           mode &= ~BYTES;
  226.           break;
  227.  
  228.         case 'c':
  229.           mode |= BYTES;
  230.           mode &= ~BLOCKS;
  231.           break;
  232.  
  233.         case 'f':
  234.           mode |= FOREVER;
  235.           break;
  236.  
  237.         case 'l':
  238.           mode &= ~(BYTES | BLOCKS);
  239.           break;
  240.  
  241.         case 'q':
  242.           header_mode = never;
  243.           break;
  244.  
  245.         case 'v':
  246.           header_mode = always;
  247.           break;
  248.  
  249.         default:
  250.           error (0, 0, "unrecognized option `-%c'", *argv[1]);
  251.           usage ();
  252.         }
  253.           ++argv[1];
  254.         }
  255.     }
  256.       /* Make the options we just parsed invisible to getopt. */
  257.       argv[1] = argv[0];
  258.       argv++;
  259.       argc--;
  260.     }
  261.  
  262. #ifdef MSDOS
  263.   while ((c = getopt_long (argc, argv, "b:c:n:fqvB", long_options, &longind))
  264. #else
  265.   while ((c = getopt_long (argc, argv, "b:c:n:fqv", long_options, &longind))
  266. #endif
  267.      != EOF)
  268.     {
  269.       switch (c)
  270.     {
  271. #ifdef MSDOS
  272.     case 30:
  273.       fprintf (stderr, COPYING);
  274.       exit (0);
  275.       break;
  276.  
  277.     case 31:
  278.       fprintf (stderr, VERSION);
  279.       exit (0);
  280.       break;
  281.  
  282.     case 'B':
  283.       mode |= BINARY;
  284.       break;
  285. #endif
  286.  
  287.     case 'b':
  288.       mode |= BLOCKS;
  289.       mode &= ~BYTES;
  290.       if (*optarg == '+')
  291.         {
  292.           mode |= START;
  293.           ++optarg;
  294.         }
  295.       else if (*optarg == '-')
  296.         ++optarg;
  297.       number = atou (optarg);
  298.       if (number == -1)
  299.         error (1, 0, "invalid number `%s'", optarg);
  300.       break;
  301.  
  302.     case 'c':
  303.       mode |= BYTES;
  304.       mode &= ~BLOCKS;
  305.       if (*optarg == '+')
  306.         {
  307.           mode |= START;
  308.           ++optarg;
  309.         }
  310.       else if (*optarg == '-')
  311.         ++optarg;
  312.       number = atou (optarg);
  313.       if (number == -1)
  314.         error (1, 0, "invalid number `%s'", optarg);
  315.       break;
  316.  
  317.     case 'f':
  318. #ifndef MSDOS
  319.         mode |= FOREVER;
  320. #endif /* not MSDOS */
  321.       break;
  322.  
  323.     case 'n':
  324.       mode &= ~(BYTES | BLOCKS);
  325.       if (*optarg == '+')
  326.         {
  327.           mode |= START;
  328.           ++optarg;
  329.         }
  330.       else if (*optarg == '-')
  331.         ++optarg;
  332.       number = atou (optarg);
  333.       if (number == -1)
  334.         error (1, 0, "invalid number `%s'", optarg);
  335.       break;
  336.  
  337.     case 'q':
  338.       header_mode = never;
  339.       break;
  340.  
  341.     case 'v':
  342.       header_mode = always;
  343.       break;
  344.  
  345.     default:
  346.       usage ();
  347.     }
  348.     }
  349.  
  350.   if (number == -1)
  351.     number = DEFAULT_NUMBER;
  352.  
  353.   /* To start printing with item `number' from the start of the file, skip
  354.      `number' - 1 items.  `tail +0' is actually meaningless, but for Unix
  355.      compatibility it's treated the same as `tail +1'. */
  356.   if (mode & START)
  357.     {
  358.       if (number)
  359.     --number;
  360.     }
  361.  
  362.   if (mode & BLOCKS)
  363.     number *= BLOCKSIZE;
  364.  
  365.   if (optind < argc - 1 && (mode & FOREVER))
  366.     error (1, 0, "cannot follow the ends of multiple files");
  367.  
  368.   if (header_mode == always
  369.       || header_mode == multiple_files && optind < argc - 1)
  370.     mode |= HEADERS;
  371.  
  372.   if (optind == argc)
  373.     errors |= tail_file ("-", mode, number);
  374.  
  375.   for (; optind < argc; ++optind)
  376.     errors |= tail_file (argv[optind], mode, number);
  377.  
  378.   exit (errors);
  379. }
  380.  
  381. /* Display the last `number' units of file `filename', controlled by
  382.    the flags in `mode'.  "-" for `filename' means the standard input.
  383.    Return 0 if successful, 1 if an error occurred. */
  384.  
  385. int
  386. tail_file (filename, mode, number)
  387.      char *filename;
  388.      int mode;
  389.      long number;
  390. {
  391.   int fd;
  392.  
  393.   if (!strcmp (filename, "-"))
  394.     {
  395.       filename = "standard input";
  396.       if (mode & HEADERS)
  397.     write_header (filename);
  398.       return tail (filename, 0, mode, number);
  399.     }
  400.   else
  401.     {
  402.       fd = open (filename, O_RDONLY);
  403.       if (fd == -1)
  404.     {
  405.       error (0, errno, "%s", filename);
  406.       return 1;
  407.     }
  408.       else
  409.     {
  410.       int errors;
  411.  
  412.       if (mode & HEADERS)
  413.         write_header (filename);
  414.       errors = tail (filename, fd, mode, number);
  415.       close (fd);
  416.       return errors;
  417.     }
  418.     }
  419. }
  420.  
  421. void
  422. write_header (filename)
  423.      char *filename;
  424. {
  425.   static int first_file = 1;
  426.  
  427.   if (first_file)
  428.     {
  429.       xwrite (1, "==> ", 4);
  430.       first_file = 0;
  431.     }
  432.   else
  433.     xwrite (1, "\n==> ", 5);
  434.   xwrite (1, filename, strlen (filename));
  435.   xwrite (1, " <==\n", 5);
  436. }
  437.  
  438. /* Display the last `number' units of file `filename', open for reading
  439.    in `fd', controlled by `mode'.
  440.    Return 0 if successful, 1 if an error occurred. */
  441.  
  442. int
  443. tail (filename, fd, mode, number)
  444.      char *filename;
  445.      int fd;
  446.      int mode;
  447.      long number;
  448. {
  449. #ifdef MSDOS
  450.   int errors;
  451.  
  452.   if (mode & BINARY)
  453.     {
  454.       setmode (fileno (stdout), O_BINARY);
  455.       setmode (fd, O_BINARY);
  456.     }
  457.  
  458.   if (mode & (BYTES | BLOCKS))
  459.     errors = tail_bytes (filename, fd, mode, number);
  460.   else
  461.     errors = tail_lines (filename, fd, mode, number);
  462.  
  463.   if (mode & BINARY)
  464.     setmode (fileno (stdout), O_TEXT);
  465.  
  466.   return errors;
  467.  
  468. #else /* not MSDOS */
  469.  
  470.   if (mode & (BYTES | BLOCKS))
  471.     return tail_bytes (filename, fd, mode, number);
  472.   else
  473.     return tail_lines (filename, fd, mode, number);
  474.  
  475. #endif /* not MSDOS */
  476. }
  477.  
  478. /* Display the last part of file `filename', open for reading in`fd',
  479.    using `number' characters, controlled by `mode'.
  480.    Return 0 if successful, 1 if an error occurred. */
  481.  
  482. int
  483. tail_bytes (filename, fd, mode, number)
  484.      char *filename;
  485.      int fd;
  486.      int mode;
  487.      long number;
  488. {
  489.   struct stat stats;
  490.  
  491.   /* Use fstat instead of checking for errno == ESPIPE because
  492.      lseek doesn't work on some special files but doesn't return an
  493.      error, either. */
  494.   if (fstat (fd, &stats))
  495.     {
  496.       error (0, errno, "%s", filename);
  497.       return 1;
  498.     }
  499.  
  500.   if (mode & START)
  501.     {
  502.       if ((stats.st_mode & S_IFMT) == S_IFREG)
  503.     lseek (fd, number, L_SET);
  504.       else if (start_bytes (filename, fd, number))
  505.     return 1;
  506.       dump_remainder (filename, fd, mode);
  507.     }
  508.   else
  509.     {
  510.       if ((stats.st_mode & S_IFMT) == S_IFREG)
  511.     {
  512.       if (lseek (fd, 0L, L_XTND) <= number)
  513.         /* The file is shorter than we want, or just the right size, so
  514.            print the whole file. */
  515.         lseek (fd, 0L, L_SET);
  516.       else
  517.         /* The file is longer than we want, so go back. */
  518.         lseek (fd, -number, L_XTND);
  519.       dump_remainder (filename, fd, mode);
  520.     }
  521.       else
  522.     return pipe_bytes (filename, fd, number);
  523.     }
  524.   return 0;
  525. }
  526.  
  527. /* Display the last part of file `filename', open for reading on `fd',
  528.    using `number' lines, controlled by `mode'.
  529.    Return 0 if successful, 1 if an error occurred. */
  530.  
  531. int
  532. tail_lines (filename, fd, mode, number)
  533.      char *filename;
  534.      int fd;
  535.      int mode;
  536.      long number;
  537. {
  538.   struct stat stats;
  539.   long length;
  540.  
  541.   if (fstat (fd, &stats))
  542.     {
  543.       error (0, errno, "%s", filename);
  544.       return 1;
  545.     }
  546.  
  547.   if (mode & START)
  548.     {
  549.       if (start_lines (filename, fd, number))
  550.     return 1;
  551.       dump_remainder (filename, fd, mode);
  552.     }
  553.   else
  554.     {
  555.       if ((stats.st_mode & S_IFMT) == S_IFREG)
  556.     {
  557.       length = lseek (fd, 0L, L_XTND);
  558.       if (length != 0 && file_lines (filename, fd, number, length))
  559.         return 1;
  560.       dump_remainder (filename, fd, mode);
  561.     }
  562.       else
  563.     return pipe_lines (filename, fd, number);
  564.     }
  565.   return 0;
  566. }
  567.  
  568. /* Print the last `number' lines from the end of file `fd'.
  569.    Go backward through the file, reading `BUFSIZE' bytes at a time (except
  570.    probably the first), until we hit the start of the file or have
  571.    read `number' newlines.
  572.    `pos' starts out as the length of the file (the offset of the last
  573.    byte of the file + 1).
  574.    Return 0 if successful, 1 if an error occurred. */
  575.  
  576. int
  577. file_lines (filename, fd, number, pos)
  578.      char *filename;
  579.      int fd;
  580.      long number;
  581.      long pos;
  582. {
  583.   char buffer[BUFSIZE];
  584.   int bytes_read;
  585.   int i;            /* Index into `buffer' for scanning. */
  586.  
  587.   if (number == 0)
  588.     return 0;
  589.  
  590.   /* Set `bytes_read' to the size of the last, probably partial, buffer;
  591.      0 < `bytes_read' <= `BUFSIZE'. */
  592. #ifdef MSDOS                /* shut up the compiler */
  593.   bytes_read = (int) (pos % (long) BUFSIZE);
  594. #else
  595.   bytes_read = pos % BUFSIZE;
  596. #endif
  597.   if (bytes_read == 0)
  598.     bytes_read = BUFSIZE;
  599.   /* Make `pos' a multiple of `BUFSIZE' (0 if the file is short), so that all
  600.      reads will be on block boundaries, which might increase efficiency. */
  601.   pos -= bytes_read;
  602.   lseek (fd, pos, L_SET);
  603.   bytes_read = read (fd, buffer, bytes_read);
  604.   if (bytes_read == -1)
  605.     {
  606.       error (0, errno, "%s", filename);
  607.       return 1;
  608.     }
  609.  
  610.   /* Count the incomplete line on files that don't end with a newline. */
  611.   if (bytes_read && buffer[bytes_read - 1] != '\n')
  612.     --number;
  613.  
  614.   do
  615.     {
  616.       /* Scan backward, counting the newlines in this bufferfull. */
  617.       for (i = bytes_read - 1; i >= 0; i--)
  618.     {
  619.       /* Have we counted the requested number of newlines yet? */
  620.       if (buffer[i] == '\n' && number-- == 0)
  621.         {
  622.           /* If this newline wasn't the last character in the buffer,
  623.              print the text after it. */
  624.           if (i != bytes_read - 1)
  625.         xwrite (1, &buffer[i + 1], bytes_read - (i + 1));
  626.           return 0;
  627.         }
  628.     }
  629.       /* Not enough newlines in that bufferfull. */
  630.       if (pos == 0)
  631.     {
  632.       /* Not enough lines in the file; print the entire file. */
  633.       lseek (fd, 0L, L_SET);
  634.       return 0;
  635.     }
  636.       pos -= BUFSIZE;
  637.       lseek (fd, pos, L_SET);
  638.     }
  639.   while ((bytes_read = read (fd, buffer, BUFSIZE)) > 0);
  640.   if (bytes_read == -1)
  641.     {
  642.       error (0, errno, "%s", filename);
  643.       return 1;
  644.     }
  645.   return 0;
  646. }
  647.  
  648. /* Print the last `number' lines from the end of the standard input,
  649.    open for reading as pipe `fd'.
  650.    Buffer the text as a linked list of LBUFFERs, adding them as needed.
  651.    Return 0 if successful, 1 if an error occured. */
  652.  
  653. int
  654. pipe_lines (filename, fd, number)
  655.      char *filename;
  656.      int fd;
  657.      long number;
  658. {
  659.   struct linebuffer
  660.   {
  661.     int nbytes, nlines;
  662.     char buffer[BUFSIZE];
  663.     struct linebuffer *next;
  664.   };
  665.   typedef struct linebuffer LBUFFER;
  666.   LBUFFER *first, *last, *tmp;
  667.   int i;            /* Index into buffers. */
  668.   long total_lines = 0;        /* Total number of newlines in all buffers. */
  669.   int errors = 0;
  670.  
  671.   first = last = (LBUFFER *) xmalloc (sizeof (LBUFFER));
  672.   first->nbytes = first->nlines = 0;
  673.   tmp = (LBUFFER *) xmalloc (sizeof (LBUFFER));
  674.  
  675.   /* Input is always read into a fresh buffer. */
  676.   while ((tmp->nbytes = read (fd, tmp->buffer, BUFSIZE)) > 0)
  677.     {
  678.       tmp->nlines = 0;
  679.       tmp->next = NULL;
  680.  
  681.       /* Count the number of newlines just read. */
  682.       for (i = 0; i < tmp->nbytes; i++)
  683.     if (tmp->buffer[i] == '\n')
  684.       ++tmp->nlines;
  685.       total_lines += tmp->nlines;
  686.  
  687.       /* If there is enough room in the last buffer read, just append the new
  688.          one to it.  This is because when reading from a pipe, `nbytes' can
  689.          often be very small. */
  690.       if (tmp->nbytes + last->nbytes < BUFSIZE)
  691.     {
  692.       bcopy (tmp->buffer, &last->buffer[last->nbytes], tmp->nbytes);
  693.       last->nbytes += tmp->nbytes;
  694.       last->nlines += tmp->nlines;
  695.     }
  696.       else
  697.     {
  698.       /* If there's not enough room, link the new buffer onto the end of
  699.          the list, then either free up the oldest buffer for the next
  700.          read if that would leave enough lines, or else malloc a new one.
  701.          Some compaction mechanism is possible but probably not
  702.          worthwhile. */
  703.       last = last->next = tmp;
  704.       if (total_lines - first->nlines > number)
  705.         {
  706.           tmp = first;
  707.           total_lines -= first->nlines;
  708.           first = first->next;
  709.         }
  710.       else
  711.         tmp = (LBUFFER *) xmalloc (sizeof (LBUFFER));
  712.     }
  713.     }
  714.   if (tmp->nbytes == -1)
  715.     {
  716.       error (0, errno, "%s", filename);
  717.       errors = 1;
  718.       free ((char *) tmp);
  719.       goto free_lbuffers;
  720.     }
  721.  
  722.   free ((char *) tmp);
  723.  
  724.   /* This prevents a core dump when the pipe contains no newlines. */
  725.   if (number == 0)
  726.     goto free_lbuffers;
  727.  
  728.   /* Count the incomplete line on files that don't end with a newline. */
  729.   if (last->buffer[last->nbytes - 1] != '\n')
  730.     {
  731.       ++last->nlines;
  732.       ++total_lines;
  733.     }
  734.  
  735.   /* Run through the list, printing lines.  First, skip over unneeded
  736.      buffers. */
  737.   for (tmp = first; total_lines - tmp->nlines > number; tmp = tmp->next)
  738.     total_lines -= tmp->nlines;
  739.  
  740.   /* Find the correct beginning, then print the rest of the file. */
  741.   if (total_lines > number)
  742.     {
  743.       char *cp;
  744.  
  745.       /* Skip `total_lines' - `number' newlines.  We made sure that
  746.          `total_lines' - `number' <= `tmp->nlines'. */
  747.       cp = tmp->buffer;
  748. #ifdef MSDOS                /* shut up the compiler */
  749.       for (i = (int) (total_lines - number); i; --i)
  750. #else
  751.       for (i = total_lines - number; i; --i)
  752. #endif
  753.     while (*cp++ != '\n')
  754.       /* Do nothing. */ ;
  755.       i = cp - tmp->buffer;
  756.     }
  757.   else
  758.     i = 0;
  759.   xwrite (1, &tmp->buffer[i], tmp->nbytes - i);
  760.  
  761.   for (tmp = tmp->next; tmp; tmp = tmp->next)
  762.     xwrite (1, tmp->buffer, tmp->nbytes);
  763.  
  764. free_lbuffers:
  765.   while (first)
  766.     {
  767.       tmp = first->next;
  768.       free ((char *) first);
  769.       first = tmp;
  770.     }
  771.   return errors;
  772. }
  773.  
  774. /* Print the last `number' characters from the end of pipe `fd'.
  775.    This is a stripped down version of pipe_lines.
  776.    Return 0 if successful, 1 if an error occurred. */
  777.  
  778. int
  779. pipe_bytes (filename, fd, number)
  780.      char *filename;
  781.      int fd;
  782.      long number;
  783. {
  784.   struct charbuffer
  785.   {
  786.     int nbytes;
  787.     char buffer[BUFSIZE];
  788.     struct charbuffer *next;
  789.   };
  790.   typedef struct charbuffer CBUFFER;
  791.   CBUFFER *first, *last, *tmp;
  792.   int i;            /* Index into buffers. */
  793. #ifdef MSDOS
  794.   long total_bytes = 0;        /* Total characters in all buffers. */
  795. #else
  796.   int total_bytes = 0;        /* Total characters in all buffers. */
  797. #endif
  798.   int errors = 0;
  799.  
  800.   first = last = (CBUFFER *) xmalloc (sizeof (CBUFFER));
  801.   first->nbytes = 0;
  802.   tmp = (CBUFFER *) xmalloc (sizeof (CBUFFER));
  803.  
  804.   /* Input is always read into a fresh buffer. */
  805.   while ((tmp->nbytes = read (fd, tmp->buffer, BUFSIZE)) > 0)
  806.     {
  807.       tmp->next = NULL;
  808.  
  809.       total_bytes += tmp->nbytes;
  810.       /* If there is enough room in the last buffer read, just append the new
  811.          one to it.  This is because when reading from a pipe, `nbytes' can
  812.          often be very small. */
  813.       if (tmp->nbytes + last->nbytes < BUFSIZE)
  814.     {
  815.       bcopy (tmp->buffer, &last->buffer[last->nbytes], tmp->nbytes);
  816.       last->nbytes += tmp->nbytes;
  817.     }
  818.       else
  819.     {
  820.       /* If there's not enough room, link the new buffer onto the end of
  821.          the list, then either free up the oldest buffer for the next
  822.          read if that would leave enough characters, or else malloc a new
  823.          one.  Some compaction mechanism is possible but probably not
  824.          worthwhile. */
  825.       last = last->next = tmp;
  826.       if (total_bytes - first->nbytes > number)
  827.         {
  828.           tmp = first;
  829.           total_bytes -= first->nbytes;
  830.           first = first->next;
  831.         }
  832.       else
  833.         {
  834.           tmp = (CBUFFER *) xmalloc (sizeof (CBUFFER));
  835.         }
  836.     }
  837.     }
  838.   if (tmp->nbytes == -1)
  839.     {
  840.       error (0, errno, "%s", filename);
  841.       errors = 1;
  842.       free ((char *) tmp);
  843.       goto free_cbuffers;
  844.     }
  845.  
  846.   free ((char *) tmp);
  847.  
  848.   /* Run through the list, printing characters.  First, skip over unneeded
  849.      buffers. */
  850.   for (tmp = first; total_bytes - tmp->nbytes > number; tmp = tmp->next)
  851.     total_bytes -= tmp->nbytes;
  852.  
  853.   /* Find the correct beginning, then print the rest of the file.
  854.      We made sure that `total_bytes' - `number' <= `tmp->nbytes'. */
  855.   if (total_bytes > number)
  856. #ifdef MSDOS                /* shut up the compiler */
  857.     i = (int) (total_bytes - number);
  858. #else
  859.     i = total_bytes - number;
  860. #endif
  861.   else
  862.     i = 0;
  863.   xwrite (1, &tmp->buffer[i], tmp->nbytes - i);
  864.  
  865.   for (tmp = tmp->next; tmp; tmp = tmp->next)
  866.     xwrite (1, tmp->buffer, tmp->nbytes);
  867.  
  868. free_cbuffers:
  869.   while (first)
  870.     {
  871.       tmp = first->next;
  872.       free ((char *) first);
  873.       first = tmp;
  874.     }
  875.   return errors;
  876. }
  877.  
  878. /* Skip `number' characters from the start of pipe `fd', and print
  879.    any extra characters that were read beyond that.
  880.    Return 1 on error, 0 if ok.  */
  881.  
  882. int
  883. start_bytes (filename, fd, number)
  884.      char *filename;
  885.      int fd;
  886.      long number;
  887. {
  888.   char buffer[BUFSIZE];
  889.   int bytes_read = 0;
  890.  
  891.   while (number > 0 && (bytes_read = read (fd, buffer, BUFSIZE)) > 0)
  892.     number -= bytes_read;
  893.   if (bytes_read == -1)
  894.     {
  895.       error (0, errno, "%s", filename);
  896.       return 1;
  897.     }
  898.   else if (number < 0)
  899. #ifdef MSDOS                /* |number| < 64k ??? */
  900.     xwrite (1, &buffer[bytes_read + number], (unsigned int) (-number));
  901. #else
  902.     xwrite (1, &buffer[bytes_read + number], -number);
  903. #endif
  904.   return 0;
  905. }
  906.  
  907. /* Skip `number' lines at the start of file or pipe `fd', and print
  908.    any extra characters that were read beyond that.
  909.    Return 1 on error, 0 if ok.  */
  910.  
  911. int
  912. start_lines (filename, fd, number)
  913.      char *filename;
  914.      int fd;
  915.      long number;
  916. {
  917.   char buffer[BUFSIZE];
  918.   int bytes_read = 0;
  919.   int bytes_to_skip = 0;
  920.  
  921.   while (number && (bytes_read = read (fd, buffer, BUFSIZE)) > 0)
  922.     {
  923.       bytes_to_skip = 0;
  924.       while (bytes_to_skip < bytes_read)
  925.     if (buffer[bytes_to_skip++] == '\n' && --number == 0)
  926.       break;
  927.     }
  928.   if (bytes_read == -1)
  929.     {
  930.       error (0, errno, "%s", filename);
  931.       return 1;
  932.     }
  933.   else if (bytes_to_skip < bytes_read)
  934.     xwrite (1, &buffer[bytes_to_skip], bytes_read - bytes_to_skip);
  935.   return 0;
  936. }
  937.  
  938. /* Display file `filename' from the current position in `fd'
  939.    to the end.  If selected in `mode', keep reading from the
  940.    end of the file until killed. */
  941.  
  942. void
  943. dump_remainder (filename, fd, mode)
  944.      char *filename;
  945.      int fd;
  946.      int mode;
  947. {
  948.   char buffer[BUFSIZE];
  949.   int bytes_read;
  950.  
  951. output:
  952.   while ((bytes_read = read (fd, buffer, BUFSIZE)) > 0)
  953.     xwrite (1, buffer, bytes_read);
  954.   if (bytes_read == -1)
  955.     error (1, errno, "%s", filename);
  956. #ifndef MSDOS
  957.   if (mode & FOREVER)
  958.     {
  959.       sleep (1);
  960.       goto output;
  961.     }
  962. #endif
  963. }
  964.  
  965. /* Write plus error check. */
  966.  
  967. void
  968. xwrite (fd, buffer, count)
  969.      int fd;
  970.      int count;
  971.      char *buffer;
  972. {
  973.   fd = write (fd, buffer, count);
  974.   if (fd != count)
  975.     error (1, errno, "write error");
  976. }
  977.  
  978. /* Allocate `size' bytes of memory dynamically, with error check. */
  979.  
  980. char *
  981. xmalloc (size)
  982.      int size;
  983. {
  984.   char *p;
  985.  
  986.   p = malloc ((unsigned) size);
  987.   if (p == NULL)
  988.     error (1, 0, "virtual memory exhausted");
  989.   return p;
  990. }
  991.  
  992. /* Convert `str', a string of ASCII digits, into an unsigned integer.
  993.    Return -1 if `str' does not represent a valid unsigned integer. */
  994.  
  995. long
  996. atou (str)
  997.      char *str;
  998. {
  999.   unsigned long value;
  1000.  
  1001.   for (value = 0; ISDIGIT (*str); ++str)
  1002.     value = value * 10 + *str - '0';
  1003.   return *str ? -1L : value;
  1004. }
  1005.  
  1006. void
  1007. usage ()
  1008. {
  1009. #ifdef MSDOS
  1010.   fprintf (stderr, "\
  1011. Usage: %s [-b [+]#] [-c [+]#] [-n [+]#] [-fqvB] [+blocks [+]#]\n\
  1012.        [+bytes [+]#] [+lines [+]#] [+follow] [+quiet] [+silent]\n\
  1013.        [+verbose] [+binary] [+copying] [+version] [file...]\n\
  1014. \n\
  1015.        %s [+/-#bcflqv] [file...]\n", program_name, program_name);
  1016.   exit (1);
  1017. #else
  1018.   fprintf (stderr, "\
  1019. Usage: %s [-b [+]#] [-c [+]#] [-n [+]#] [-fqv] [+blocks [+]#]\n\
  1020.        [+bytes [+]#] [+lines [+]#] [+follow] [+quiet] [+silent]\n\
  1021.        [+verbose] [file...]\n\
  1022. \n\
  1023.        %s [+/-#bcflqv] [file...]\n", program_name, program_name);
  1024.   exit (1);
  1025. #endif
  1026. }
  1027.