home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 367_01 / futi14as.zoo / head.c < prev    next >
C/C++ Source or Header  |  1992-02-22  |  11KB  |  492 lines

  1. /* head -- output first 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/head.c 1.4.0.3 90/09/19 11:17:59 tho Exp $";
  30.  
  31. static char Program_Id[] = "head";
  32. static char RCS_Revision[] = "$Revision: 1.4.0.3 $";
  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. /* Usage: head [-b #] [-c #] [-n #] [-qv] [+blocks #] [+bytes #] [+lines #]
  44.           [+quiet] [+silent] [+verbose] [file...]
  45.  
  46.           head [-#bclqv] [file...]
  47.  
  48.    Options:
  49.    -b, +blocks #    Print first # 512-byte blocks.
  50.    -c, +bytes #        Print first # bytes.
  51.    -l, -n, +lines #    Print first # lines.
  52.    -q, +quiet, +silent    Never print filename headers.
  53.    -v, +verbose        Always print filename headers.
  54.  
  55.    Reads from standard input if no files are given or when a filename of
  56.    ``-'' is encountered.
  57.    By default, filename headers are printed only if more than one file
  58.    is given.
  59.    By default, prints the first 10 lines (head -n 10).
  60.  
  61.    David MacKenzie <djm@ai.mit.edu> */
  62.  
  63. #include <stdio.h>
  64. #include <getopt.h>
  65. #include <ctype.h>
  66. #include <sys/types.h>
  67. #include "system.h"
  68.  
  69. #ifdef STDC_HEADERS
  70. #include <errno.h>
  71. #include <stdlib.h>
  72. #define ISDIGIT(c) (isdigit ((unsigned char) (c)))
  73. #else
  74. #define ISDIGIT(c) (isascii (c) && isdigit (c))
  75.  
  76. extern int errno;
  77. #endif
  78.  
  79. /* Number of lines/chars/blocks to head. */
  80. #define DEFAULT_NUMBER 10
  81.  
  82. #define BLOCKSIZE 512
  83.  
  84. /* Size of atomic reads. */
  85. #define BUFSIZE (BLOCKSIZE*8)
  86.  
  87. /* Masks for the operation mode.  If neither BYTES nor BLOCKS is set,
  88.    head operates by lines. */
  89. #define BYTES 1            /* Head in bytes. */
  90. #define BLOCKS 2        /* Head in blocks. */
  91. #define HEADERS 4        /* Write filename headers. */
  92. #ifdef MSDOS
  93. #define BINARY 32        /* Suppress crlf translation. */
  94. #endif
  95.  
  96. /* When to print the filename banners. */
  97. enum header_mode
  98. {
  99.   multiple_files, always, never
  100. };
  101.  
  102. #ifdef MSDOS
  103.  
  104. #include <string.h>
  105. #include <stdarg.h>
  106. #include <io.h>
  107.  
  108. #include <gnulib.h>
  109.  
  110. extern  void main (int, char **);
  111. extern  int head_file (char *, int, long);
  112. extern  void write_header (char *);
  113. extern  int head (char *, int, int, long);
  114. extern  int head_bytes (char *, int, long);
  115. extern  int head_lines (char *, int, long);
  116. extern  void xwrite (int, char *, int);
  117. extern  long atou (char *);
  118. extern  char *basename (char *);
  119. extern  void usage (void);
  120.  
  121. #else /* not MSDOS */
  122.  
  123. int head ();
  124. int head_bytes ();
  125. int head_file ();
  126. int head_lines ();
  127. long atou ();
  128. void error ();
  129. void usage ();
  130. void write_header ();
  131. void xwrite ();
  132.  
  133. #endif /* MSDOS */
  134.  
  135. /* The name this program was run with. */
  136. char *program_name;
  137.  
  138. struct option long_options[] =
  139. {
  140. #ifdef MSDOS
  141.   {"binary", 1, NULL, 'B'},
  142.   {"copying", 0, NULL, 30},
  143.   {"version", 0, NULL, 31},
  144. #endif
  145.   {"blocks", 1, NULL, 'b'},
  146.   {"bytes", 1, NULL, 'c'},
  147.   {"lines", 1, NULL, 'n'},
  148.   {"quiet", 0, NULL, 'q'},
  149.   {"silent", 0, NULL, 'q'},
  150.   {"verbose", 0, NULL, 'v'},
  151.   {NULL, 0, NULL, 0}
  152. };
  153.  
  154. void
  155. main (argc, argv)
  156.      int argc;
  157.      char **argv;
  158. {
  159.   enum header_mode header_mode = multiple_files;
  160.   int errors = 0;        /* Exit status. */
  161.   int mode = 0;            /* Flags. */
  162.   long number = -1;        /* Number of items to print (-1 if undef.). */
  163.   int c;            /* Option character. */
  164.   int longind;            /* Index in `long_options' of option found. */
  165.  
  166.   program_name = argv[0];
  167.  
  168.   if (argc > 1 && argv[1][0] == '-' && ISDIGIT (argv[1][1]))
  169.     {
  170.       /* Old option syntax; a dash, one or more digits, and one or
  171.      more option letters.  Move past the number. */
  172.       for (number = 0, ++argv[1]; ISDIGIT (*argv[1]); ++argv[1])
  173.     number = number * 10 + *argv[1] - '0';
  174.       /* Parse any appended option letters. */
  175.       while (*argv[1])
  176.     {
  177.       switch (*argv[1])
  178.         {
  179.         case 'b':
  180.           mode |= BLOCKS;
  181.           mode &= ~BYTES;
  182.           break;
  183.  
  184.         case 'c':
  185.           mode |= BYTES;
  186.           mode &= ~BLOCKS;
  187.           break;
  188.  
  189.         case 'l':
  190.           mode &= ~(BYTES | BLOCKS);
  191.           break;
  192.  
  193.         case 'q':
  194.           header_mode = never;
  195.           break;
  196.  
  197.         case 'v':
  198.           header_mode = always;
  199.           break;
  200.  
  201.         default:
  202.           error (0, 0, "unrecognized option `-%c'", *argv[1]);
  203.           usage ();
  204.         }
  205.       ++argv[1];
  206.     }
  207.       /* Make the options we just parsed invisible to getopt. */
  208.       argv[1] = argv[0];
  209.       argv++;
  210.       argc--;
  211.     }
  212.  
  213. #ifdef MSDOS
  214.   while ((c = getopt_long (argc, argv, "Bb:c:n:qv", long_options, &longind))
  215. #else
  216.   while ((c = getopt_long (argc, argv, "b:c:n:qv", long_options, &longind))
  217. #endif
  218.      != EOF)
  219.     {
  220.       switch (c)
  221.     {
  222. #ifdef MSDOS
  223.     case 30:
  224.       fprintf (stderr, COPYING);
  225.       exit (0);
  226.       break;
  227.  
  228.     case 31:
  229.       fprintf (stderr, VERSION);
  230.       exit (0);
  231.       break;
  232.  
  233.     case 'B':
  234.       mode |= BINARY;
  235.       break;
  236. #endif
  237.  
  238.     case 'b':
  239.       mode |= BLOCKS;
  240.       mode &= ~BYTES;
  241.       number = atou (optarg);
  242.       if (number == -1)
  243.         error (1, 0, "invalid number `%s'", optarg);
  244.       break;
  245.  
  246.     case 'c':
  247.       mode |= BYTES;
  248.       mode &= ~BLOCKS;
  249.       number = atou (optarg);
  250.       if (number == -1)
  251.         error (1, 0, "invalid number `%s'", optarg);
  252.       break;
  253.  
  254.     case 'n':
  255.       mode &= ~(BYTES | BLOCKS);
  256.       number = atou (optarg);
  257.       if (number == -1)
  258.         error (1, 0, "invalid number `%s'", optarg);
  259.       break;
  260.  
  261.     case 'q':
  262.       header_mode = never;
  263.       break;
  264.  
  265.     case 'v':
  266.       header_mode = always;
  267.       break;
  268.  
  269.     default:
  270.       usage ();
  271.     }
  272.     }
  273.  
  274.   if (number == -1)
  275.     number = DEFAULT_NUMBER;
  276.  
  277.   if (mode & BLOCKS)
  278.     number *= BLOCKSIZE;
  279.  
  280.   if (header_mode == always
  281.       || header_mode == multiple_files && optind < argc - 1)
  282.     mode |= HEADERS;
  283.  
  284.   if (optind == argc)
  285.     errors |= head_file ("-", mode, number);
  286.  
  287.   for (; optind < argc; ++optind)
  288.     errors |= head_file (argv[optind], mode, number);
  289.  
  290.   exit (errors);
  291. }
  292.  
  293. int
  294. head_file (filename, mode, number)
  295.      char *filename;
  296.      int mode;
  297.      long number;
  298. {
  299.   int fd;
  300.  
  301.   if (!strcmp (filename, "-"))
  302.     {
  303.       filename = "standard input";
  304.       if (mode & HEADERS)
  305.     write_header (filename);
  306.       return head (filename, 0, mode, number);
  307.     }
  308.   else
  309.     {
  310.       fd = open (filename, O_RDONLY);
  311.       if (fd == -1)
  312.     {
  313.       error (0, errno, "%s", filename);
  314.       return 1;
  315.     }
  316.       else
  317.     {
  318.       int errors;
  319.  
  320.       if (mode & HEADERS)
  321.         write_header (filename);
  322.       errors = head (filename, fd, mode, number);
  323.       close (fd);
  324.       return errors;
  325.     }
  326.     }
  327. }
  328.  
  329. void
  330. write_header (filename)
  331.      char *filename;
  332. {
  333.   static int first_file = 1;
  334.  
  335.   if (first_file)
  336.     {
  337.       xwrite (1, "==> ", 4);
  338.       first_file = 0;
  339.     }
  340.   else
  341.     xwrite (1, "\n==> ", 5);
  342.   xwrite (1, filename, strlen (filename));
  343.   xwrite (1, " <==\n", 5);
  344. }
  345.  
  346. int
  347. head (filename, fd, mode, number)
  348.      char *filename;
  349.      int fd;
  350.      int mode;
  351.      long number;
  352. {
  353. #ifdef MSDOS
  354.   int errors;
  355.  
  356.   if (mode & BINARY)
  357.     {
  358.       setmode (fileno (stdout), O_BINARY);
  359.       setmode (fd, O_BINARY);
  360.     }
  361.  
  362.   if (mode & (BYTES | BLOCKS))
  363.     errors = head_bytes (filename, fd, number);
  364.   else
  365.     errors = head_lines (filename, fd, number);
  366.  
  367.   if (mode & BINARY)
  368.     setmode (fileno (stdout), O_TEXT);
  369.  
  370.   return errors;
  371.  
  372. #else /* not MSDOS */
  373.  
  374.   if (mode & (BYTES | BLOCKS))
  375.     return head_bytes (filename, fd, number);
  376.   else
  377.     return head_lines (filename, fd, number);
  378.  
  379. #endif /* not MSDOS */
  380. }
  381.  
  382. int
  383. head_bytes (filename, fd, bytes_to_write)
  384.      char *filename;
  385.      int fd;
  386.      long bytes_to_write;
  387. {
  388.   char buffer[BUFSIZE];
  389.   int bytes_read;
  390.  
  391.   while (bytes_to_write)
  392.     {
  393.       bytes_read = read (fd, buffer, BUFSIZE);
  394.       if (bytes_read == -1)
  395.     {
  396.       error (0, errno, "%s", filename);
  397.       return 1;
  398.     }
  399.       if (bytes_read == 0)
  400.     break;
  401. #ifdef MSDOS
  402.       if ((long) bytes_read > bytes_to_write)
  403.     bytes_read = (int) bytes_to_write;
  404.       xwrite (1, buffer, bytes_read);
  405.       bytes_to_write -= (long) bytes_read;
  406. #else /* not MSDOS */
  407.       if (bytes_read > bytes_to_write)
  408.     bytes_read = bytes_to_write;
  409.       xwrite (1, buffer, bytes_read);
  410.       bytes_to_write -= bytes_read;
  411. #endif /* not MSDOS */
  412.     }
  413.   return 0;
  414. }
  415.  
  416. int
  417. head_lines (filename, fd, lines_to_write)
  418.      char *filename;
  419.      int fd;
  420.      long lines_to_write;
  421. {
  422.   char buffer[BUFSIZE];
  423.   int bytes_read;
  424.   int bytes_to_write;
  425.  
  426.   while (lines_to_write)
  427.     {
  428.       bytes_read = read (fd, buffer, BUFSIZE);
  429.       if (bytes_read == -1)
  430.     {
  431.       error (0, errno, "%s", filename);
  432.       return 1;
  433.     }
  434.       if (bytes_read == 0)
  435.     break;
  436.       bytes_to_write = 0;
  437.       while (bytes_to_write < bytes_read)
  438.     if (buffer[bytes_to_write++] == '\n' && --lines_to_write == 0)
  439.       break;
  440.       xwrite (1, buffer, bytes_to_write);
  441.     }
  442.   return 0;
  443. }
  444.  
  445. /* Write plus error check. */
  446.  
  447. void
  448. xwrite (fd, buffer, count)
  449.      int fd;
  450.      int count;
  451.      char *buffer;
  452. {
  453.   fd = write (fd, buffer, count);
  454.   if (fd != count)
  455.     error (1, errno, "write error");
  456. }
  457.  
  458. /* Convert `str', a string of ASCII digits, into an unsigned integer.
  459.    Return -1 if `str' does not represent a valid unsigned integer. */
  460.  
  461. long
  462. atou (str)
  463.      char *str;
  464. {
  465.   long value;
  466.  
  467.   for (value = 0; ISDIGIT (*str); ++str)
  468.     value = value * 10 + *str - '0';
  469.   return *str ? -1L : value;
  470. }
  471.  
  472. void
  473. usage ()
  474. {
  475. #ifdef MSDOS
  476.   fprintf (stderr, "\
  477. Usage: %s [-b #] [-c #] [-n #] [-qvB] [+blocks #] [+bytes #]\n\
  478.        [+lines #] [+quiet] [+silent] [+verbose] [+binary] [+version]\n\
  479.        [+copying] [file...]\n\
  480. \n\
  481.        %s [-#bclqv] [file...]\n", program_name, program_name);
  482.   exit (1);
  483. #else
  484.   fprintf (stderr, "\
  485. Usage: %s [-b #] [-c #] [-n #] [-qv] [+blocks #] [+bytes #] [+lines #]\n\
  486.        [+quiet] [+silent] [+verbose] [file...]\n\
  487. \n\
  488.        %s [-#bclqv] [file...]\n", program_name, program_name);
  489.   exit (1);
  490. #endif
  491. }
  492.