home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / txtut122.zip / textutil / src / head.c < prev    next >
C/C++ Source or Header  |  1998-04-11  |  9KB  |  392 lines

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