home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 1 / FFMCD01.bin / useful / dist / gnu / textutils / textutils-1.6-amiga / src / head.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-15  |  8.2 KB  |  397 lines

  1. /* head -- output first part of file(s)
  2.    Copyright (C) 1989, 1990, 1991 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 2, 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. /* Options:
  19.    -b            Print first N 512-byte blocks.
  20.    -c, --bytes=N[bkm]    Print first N bytes
  21.             [or 512-byte blocks, kilobytes, or megabytes].
  22.    -k            Print first N kilobytes.
  23.    -N, -l, -n, --lines=N    Print first N lines.
  24.    -m            Print first N megabytes.
  25.    -q, --quiet, --silent    Never print filename headers.
  26.    -v, --verbose        Always print filename headers.
  27.  
  28.    Reads from standard input if no files are given or when a filename of
  29.    ``-'' is encountered.
  30.    By default, filename headers are printed only if more than one file
  31.    is given.
  32.    By default, prints the first 10 lines (head -n 10).
  33.  
  34.    David MacKenzie <djm@gnu.ai.mit.edu> */
  35.  
  36. #include <stdio.h>
  37. #include <getopt.h>
  38. #include <sys/types.h>
  39. #include "system.h"
  40. #include "version.h"
  41.  
  42. /* Number of lines/chars/blocks to head. */
  43. #define DEFAULT_NUMBER 10
  44.  
  45. /* Size of atomic reads. */
  46. #define BUFSIZE (512 * 8)
  47.  
  48. /* Number of bytes per item we are printing.
  49.    If 0, head in lines. */
  50. static int unit_size;
  51.  
  52. /* If nonzero, print filename headers. */
  53. static int print_headers;
  54.  
  55. /* When to print the filename banners. */
  56. enum header_mode
  57. {
  58.   multiple_files, always, never
  59. };
  60.  
  61. void error ();
  62. void xwrite ();
  63.  
  64. static int head ();
  65. static int head_bytes ();
  66. static int head_file ();
  67. static int head_lines ();
  68. static long atou ();
  69. static void parse_unit ();
  70. static void usage ();
  71. static void write_header ();
  72.  
  73. /* The name this program was run with. */
  74. char *program_name;
  75.  
  76. /* Have we ever read standard input?  */
  77. static int have_read_stdin;
  78.  
  79. /* If non-zero, display usage information and exit.  */
  80. static int flag_help;
  81.  
  82. /* If non-zero, print the version on standard error.  */
  83. static int flag_version;
  84.  
  85. static struct option const long_options[] =
  86. {
  87.   {"bytes", required_argument, NULL, 'c'},
  88.   {"lines", required_argument, NULL, 'n'},
  89.   {"quiet", no_argument, NULL, 'q'},
  90.   {"silent", no_argument, NULL, 'q'},
  91.   {"verbose", no_argument, NULL, 'v'},
  92.   {"help", no_argument, &flag_help, 1},
  93.   {"version", no_argument, &flag_version, 1},
  94.   {NULL, 0, NULL, 0}
  95. };
  96.  
  97. void
  98. main (argc, argv)
  99.      int argc;
  100.      char **argv;
  101. {
  102.   enum header_mode header_mode = multiple_files;
  103.   int exit_status = 0;
  104.   long number = -1;        /* Number of items to print (-1 if undef.). */
  105.   int c;            /* Option character. */
  106.  
  107.   program_name = argv[0];
  108.   have_read_stdin = 0;
  109.   unit_size = 0;
  110.   print_headers = 0;
  111.  
  112.   if (argc > 1 && argv[1][0] == '-' && ISDIGIT (argv[1][1]))
  113.     {
  114.       /* Old option syntax; a dash, one or more digits, and one or
  115.      more option letters.  Move past the number. */
  116.       for (number = 0, ++argv[1]; ISDIGIT (*argv[1]); ++argv[1])
  117.     number = number * 10 + *argv[1] - '0';
  118.       /* Parse any appended option letters. */
  119.       while (*argv[1])
  120.     {
  121.       switch (*argv[1])
  122.         {
  123.         case 'b':
  124.           unit_size = 512;
  125.           break;
  126.  
  127.         case 'c':
  128.           unit_size = 1;
  129.           break;
  130.  
  131.         case 'k':
  132.           unit_size = 1024;
  133.           break;
  134.  
  135.         case 'l':
  136.           unit_size = 0;
  137.           break;
  138.  
  139.         case 'm':
  140.           unit_size = 1048576;
  141.           break;
  142.  
  143.         case 'q':
  144.           header_mode = never;
  145.           break;
  146.  
  147.         case 'v':
  148.           header_mode = always;
  149.           break;
  150.  
  151.         default:
  152.           error (0, 0, "unrecognized option `-%c'", *argv[1]);
  153.           usage ();
  154.         }
  155.       ++argv[1];
  156.     }
  157.       /* Make the options we just parsed invisible to getopt. */
  158.       argv[1] = argv[0];
  159.       argv++;
  160.       argc--;
  161.     }
  162.  
  163.   while ((c = getopt_long (argc, argv, "c:n:qv", long_options, (int *) 0))
  164.      != EOF)
  165.     {
  166.       switch (c)
  167.     {
  168.     case 0:
  169.       break;
  170.  
  171.     case 'c':
  172.       unit_size = 1;
  173.       parse_unit (optarg);
  174.       goto getnum;
  175.     case 'n':
  176.       unit_size = 0;
  177.     getnum:
  178.       number = atou (optarg);
  179.       if (number == -1)
  180.         error (1, 0, "invalid number `%s'", optarg);
  181.       break;
  182.  
  183.     case 'q':
  184.       header_mode = never;
  185.       break;
  186.  
  187.     case 'v':
  188.       header_mode = always;
  189.       break;
  190.  
  191.     default:
  192.       usage ();
  193.     }
  194.     }
  195.  
  196.   if (flag_version)
  197.     {
  198.       fprintf (stderr, "%s\n", version_string);
  199.       exit (0);
  200.     }
  201.  
  202.   if (flag_help)
  203.     usage ();
  204.  
  205.   if (number == -1)
  206.     number = DEFAULT_NUMBER;
  207.  
  208.   if (unit_size > 1)
  209.     number *= unit_size;
  210.  
  211.   if (header_mode == always
  212.       || (header_mode == multiple_files && optind < argc - 1))
  213.     print_headers = 1;
  214.  
  215.   if (optind == argc)
  216.     exit_status |= head_file ("-", number);
  217.  
  218.   for (; optind < argc; ++optind)
  219.     exit_status |= head_file (argv[optind], number);
  220.  
  221.   if (have_read_stdin && close (0) < 0)
  222.     error (1, errno, "-");
  223.   if (close (1) < 0)
  224.     error (1, errno, "write error");
  225.  
  226.   exit (exit_status);
  227. }
  228.  
  229. static int
  230. head_file (filename, number)
  231.      char *filename;
  232.      long number;
  233. {
  234.   int fd;
  235.  
  236.   if (!strcmp (filename, "-"))
  237.     {
  238.       have_read_stdin = 1;
  239.       filename = "standard input";
  240.       if (print_headers)
  241.     write_header (filename);
  242.       return head (filename, 0, number);
  243.     }
  244.   else
  245.     {
  246.       fd = open (filename, O_RDONLY);
  247.       if (fd >= 0)
  248.     {
  249.       int errors;
  250.  
  251.       if (print_headers)
  252.         write_header (filename);
  253.       errors = head (filename, fd, number);
  254.       if (close (fd) == 0)
  255.         return errors;
  256.     }
  257.       error (0, errno, "%s", filename);
  258.       return 1;
  259.     }
  260. }
  261.  
  262. static void
  263. write_header (filename)
  264.      char *filename;
  265. {
  266.   static int first_file = 1;
  267.  
  268.   if (first_file)
  269.     {
  270.       xwrite (1, "==> ", 4);
  271.       first_file = 0;
  272.     }
  273.   else
  274.     xwrite (1, "\n==> ", 5);
  275.   xwrite (1, filename, strlen (filename));
  276.   xwrite (1, " <==\n", 5);
  277. }
  278.  
  279. static int
  280. head (filename, fd, number)
  281.      char *filename;
  282.      int fd;
  283.      long number;
  284. {
  285.   if (unit_size)
  286.     return head_bytes (filename, fd, number);
  287.   else
  288.     return head_lines (filename, fd, number);
  289. }
  290.  
  291. static int
  292. head_bytes (filename, fd, bytes_to_write)
  293.      char *filename;
  294.      int fd;
  295.      long bytes_to_write;
  296. {
  297.   char buffer[BUFSIZE];
  298.   int bytes_read;
  299.  
  300.   while (bytes_to_write)
  301.     {
  302.       bytes_read = read (fd, buffer, BUFSIZE);
  303.       if (bytes_read == -1)
  304.     {
  305.       error (0, errno, "%s", filename);
  306.       return 1;
  307.     }
  308.       if (bytes_read == 0)
  309.     break;
  310.       if (bytes_read > bytes_to_write)
  311.     bytes_read = bytes_to_write;
  312.       xwrite (1, buffer, bytes_read);
  313.       bytes_to_write -= bytes_read;
  314.     }
  315.   return 0;
  316. }
  317.  
  318. static int
  319. head_lines (filename, fd, lines_to_write)
  320.      char *filename;
  321.      int fd;
  322.      long lines_to_write;
  323. {
  324.   char buffer[BUFSIZE];
  325.   int bytes_read;
  326.   int bytes_to_write;
  327.  
  328.   while (lines_to_write)
  329.     {
  330.       bytes_read = read (fd, buffer, BUFSIZE);
  331.       if (bytes_read == -1)
  332.     {
  333.       error (0, errno, "%s", filename);
  334.       return 1;
  335.     }
  336.       if (bytes_read == 0)
  337.     break;
  338.       bytes_to_write = 0;
  339.       while (bytes_to_write < bytes_read)
  340.     if (buffer[bytes_to_write++] == '\n' && --lines_to_write == 0)
  341.       break;
  342.       xwrite (1, buffer, bytes_to_write);
  343.     }
  344.   return 0;
  345. }
  346.  
  347. static void
  348. parse_unit (str)
  349.      char *str;
  350. {
  351.   int arglen = strlen (str);
  352.  
  353.   if (arglen == 0)
  354.     return;
  355.  
  356.   switch (str[arglen - 1])
  357.     {
  358.     case 'b':
  359.       unit_size = 512;
  360.       str[arglen - 1] = '\0';
  361.       break;
  362.     case 'k':
  363.       unit_size = 1024;
  364.       str[arglen - 1] = '\0';
  365.       break;
  366.     case 'm':
  367.       unit_size = 1048576;
  368.       str[arglen - 1] = '\0';
  369.       break;
  370.     }
  371. }
  372.  
  373. /* Convert STR, a string of ASCII digits, into an unsigned integer.
  374.    Return -1 if STR does not represent a valid unsigned integer. */
  375.  
  376. static long
  377. atou (str)
  378.      char *str;
  379. {
  380.   int value;
  381.  
  382.   for (value = 0; ISDIGIT (*str); ++str)
  383.     value = value * 10 + *str - '0';
  384.   return *str ? -1 : value;
  385. }
  386.  
  387. static void
  388. usage ()
  389. {
  390.   fprintf (stderr, "\
  391. Usage: %s [-c N[bkm]] [-n N] [-qv] [--bytes=N[bkm]] [--lines=N]\n\
  392.        [--quiet] [--silent] [--verbose] [--help] [--version] [file...]\n\
  393.        %s [-Nbcklmqv] [--help] [--version] [file...]\n",
  394.        program_name, program_name);
  395.   exit (1);
  396. }
  397.