add --version and --help
authorJim Meyering <jim@meyering.net>
Thu, 29 Apr 1993 05:26:22 +0000 (05:26 +0000)
committerJim Meyering <jim@meyering.net>
Thu, 29 Apr 1993 05:26:22 +0000 (05:26 +0000)
21 files changed:
src/cat.c
src/cksum.c
src/comm.c
src/csplit.c
src/cut.c
src/expand.c
src/fold.c
src/head.c
src/join.c
src/nl.c
src/od.c
src/paste.c
src/pr.c
src/split.c
src/sum.c
src/tac.c
src/tail.c
src/tr.c
src/unexpand.c
src/uniq.c
src/wc.c

index a2b1c14..044c9a9 100644 (file)
--- a/src/cat.c
+++ b/src/cat.c
@@ -29,6 +29,7 @@
 #include <sys/ioctl.h>
 #endif
 #include "system.h"
+#include "version.h"
 
 #define max(h,i) ((h) > (i) ? (h) : (i))
 
@@ -82,7 +83,7 @@ usage (reason)
   fprintf (stderr, "\
 Usage: %s [-benstuvAET] [--number] [--number-nonblank] [--squeeze-blank]\n\
        [--show-nonprinting] [--show-ends] [--show-tabs] [--show-all]\n\
-       [file...]\n",
+       [--help] [--version] [file...]\n",
           program_name);
 
   exit (2);
@@ -132,8 +133,16 @@ main (argc, argv)
   int mark_line_ends = 0;
   int quote = 0;
   int output_tabs = 1;
+
+/* If non-zero, call cat, otherwise call simple_cat to do the actual work. */
   int options = 0;
 
+  /* If non-zero, display usage information and exit.  */
+  static int flag_help;
+
+  /* If non-zero, print the version on standard error.  */
+  static int flag_version;
+
   static struct option const long_options[] =
   {
     {"number-nonblank", no_argument, NULL, 'b'},
@@ -143,6 +152,8 @@ main (argc, argv)
     {"show-ends", no_argument, NULL, 'E'},
     {"show-tabs", no_argument, NULL, 'T'},
     {"show-all", no_argument, NULL, 'A'},
+    {"help", no_argument, &flag_help, 1},
+    {"version", no_argument, &flag_version, 1},
     {NULL, 0, NULL, 0}
   };
 
@@ -153,52 +164,62 @@ main (argc, argv)
   while ((c = getopt_long (argc, argv, "benstuvAET", long_options, (int *) 0))
         != EOF)
     {
-      options++;
       switch (c)
        {
+       case 0:
+         break;
+
        case 'b':
+         ++options;
          numbers = 1;
          numbers_at_empty_lines = 0;
          break;
 
        case 'e':
+         ++options;
          mark_line_ends = 1;
          quote = 1;
          break;
 
        case 'n':
+         ++options;
          numbers = 1;
          break;
 
        case 's':
+         ++options;
          squeeze_empty_lines = 1;
          break;
 
        case 't':
+         ++options;
          output_tabs = 0;
          quote = 1;
          break;
 
        case 'u':
          /* We provide the -u feature unconditionally.  */
-         options--;
          break;
 
        case 'v':
+         ++options;
          quote = 1;
          break;
 
        case 'A':
+         ++options;
          quote = 1;
          mark_line_ends = 1;
          output_tabs = 0;
          break;
 
        case 'E':
+         ++options;
          mark_line_ends = 1;
          break;
 
        case 'T':
+         ++options;
          output_tabs = 0;
          break;
 
@@ -207,6 +228,12 @@ main (argc, argv)
        }
     }
 
+  if (flag_version)
+    fprintf (stderr, "%s\n", version_string);
+
+  if (flag_help)
+    usage ();
+
   output_desc = 1;
 
   /* Get device, i-node number, and optimal blocksize of output.  */
@@ -278,8 +305,9 @@ main (argc, argv)
          goto contin;
        }
 
-      /* Select which version of `cat' to use. If any options (more than -u)
-        were specified, use `cat', otherwise use `simple_cat'.  */
+      /* Select which version of `cat' to use. If any options (more than -u,
+        --version, or --help) were specified, use `cat', otherwise use
+        `simple_cat'.  */
 
       if (options == 0)
        {
index 6cce4a8..e629dd8 100644 (file)
@@ -103,14 +103,32 @@ main ()
 #else /* !CRCTAB */
 
 #include <stdio.h>
+#include <getopt.h>
 #include <sys/types.h>
 #include "system.h"
+#include "version.h"
 
 /* Number of bytes to read at once.  */
 #define BUFLEN (1 << 16)
 
 void error ();
 
+/* The name this program was run with.  */
+char *program_name;
+
+/* If non-zero, display usage information and exit.  */
+static int flag_help;
+
+/* If non-zero, print the version on standard error.  */
+static int flag_version;
+
+static struct option const long_options[] =
+{
+  {"help", no_argument, &flag_help, 1},
+  {"version", no_argument, &flag_version, 1},
+  {0, 0, 0, 0}
+};
+
 static unsigned long const crctab[256] =
 {
   0x0,
@@ -167,9 +185,6 @@ static unsigned long const crctab[256] =
   0xA2F33668, 0xBCB4666D, 0xB8757BDA, 0xB5365D03, 0xB1F740B4
 };
 
-/* The name this program was run with.  */
-char *program_name;
-
 /* Nonzero if any of the files read were the standard input. */
 static int have_read_stdin;
 
@@ -244,27 +259,52 @@ cksum (file, print_name)
   return 0;
 }
 
+static void
+usage ()
+{
+  fprintf (stderr, "\
+Usage: %s [--help] [--version] [file...]\n", program_name);
+  exit (1);
+}
+
 void
 main (argc, argv)
      int argc;
      char **argv;
 {
+  int i, c;
   int errors = 0;
 
   program_name = argv[0];
   have_read_stdin = 0;
 
-  if (argc == 1)
+  while ((c = getopt_long (argc, argv, "", long_options, (int *) 0)) != EOF)
+    {
+      switch (c)
+       {
+       case 0:
+         break;
+
+       default:
+         usage ();
+       }
+    }
+
+  if (flag_version)
+    fprintf (stderr, "%s\n", version_string);
+
+  if (flag_help)
+    usage ();
+
+  if (optind >= argc)
     {
       if (cksum ("-", 0) < 0)
        errors = 1;
     }
   else
     {
-      int optind;
-
-      for (optind = 1; optind < argc; ++optind)
-       if (cksum (argv[optind], 1) < 0)
+      for (i = optind; i < argc; i++)
+       if (cksum (argv[i], 1) < 0)
          errors = 1;
     }
 
index 7a7d6ac..8b4f87f 100644 (file)
 #include <sys/types.h>
 #include "system.h"
 #include "linebuffer.h"
+#include "version.h"
 
 #define min(x, y) ((x) < (y) ? (x) : (y))
 
+/* The name this program was run with. */
+char *program_name;
+
 /* If nonzero, print lines that are found only in file 1. */
 static int only_file_1;
 
@@ -34,8 +38,18 @@ static int only_file_2;
 /* If nonzero, print lines that are found in both files. */
 static int both;
 
-/* The name this program was run with. */
-char *program_name;
+/* If non-zero, display usage information and exit.  */
+static int flag_help;
+
+/* If non-zero, print the version on standard error.  */
+static int flag_version;
+
+static struct option const long_options[] =
+{
+  {"help", no_argument, &flag_help, 1},
+  {"version", no_argument, &flag_version, 1},
+  {0, 0, 0, 0}
+};
 
 void error ();
 static int compare_files ();
@@ -55,9 +69,12 @@ main (argc, argv)
   only_file_2 = 1;
   both = 1;
 
-  while ((c = getopt (argc, argv, "123")) != EOF)
+  while ((c = getopt_long (argc, argv, "123", long_options, (int *) 0)) != EOF)
     switch (c)
       {
+      case 0:
+       break;
+
       case '1':
        only_file_1 = 0;
        break;
@@ -74,6 +91,12 @@ main (argc, argv)
        usage ();
       }
 
+  if (flag_version)
+    fprintf (stderr, "%s\n", version_string);
+
+  if (flag_help)
+    usage ();
+
   if (optind + 2 != argc)
     usage ();
 
@@ -216,6 +239,7 @@ writeline (line, stream, class)
 static void
 usage ()
 {
-  fprintf (stderr, "Usage: %s [-123] file1 file2\n", program_name);
+  fprintf (stderr, "Usage: %s [-123] [--help] [--version] file1 file2\n",
+          program_name);
   exit (1);
 }
index 8d27ce0..e5b6ed6 100644 (file)
@@ -24,6 +24,7 @@
 #include <signal.h>
 #include "regex.h"
 #include "system.h"
+#include "version.h"
 
 #ifdef STDC_HEADERS
 #include <stdlib.h>
@@ -118,6 +119,9 @@ struct buffer_record
   struct buffer_record *next;
 };
 
+/* The name this program was run with. */
+char *program_name;
+
 /* Input file descriptor. */
 static int input_desc = 0;
 
@@ -179,8 +183,23 @@ static struct control *controls;
 /* Number of elements in `controls'. */
 static unsigned control_used;
 
-/* The name this program was run with. */
-char *program_name;
+/* If non-zero, display usage information and exit.  */
+static int flag_help;
+
+/* If non-zero, print the version on standard error.  */
+static int flag_version;
+
+static struct option const longopts[] =
+{
+  {"digits", required_argument, NULL, 'n'},
+  {"quiet", no_argument, NULL, 's'},
+  {"silent", no_argument, NULL, 's'},
+  {"keep-files", no_argument, NULL, 'k'},
+  {"prefix", required_argument, NULL, 'f'},
+  {"help", no_argument, &flag_help, 1},
+  {"version", no_argument, &flag_version, 1},
+  {NULL, 0, NULL, 0}
+};
 
 /* Allocate N bytes of memory dynamically, with error checking.  */
 
@@ -1191,16 +1210,6 @@ interrupt_handler ()
   cleanup ();
 }
 
-static struct option const longopts[] =
-{
-  {"digits", required_argument, NULL, 'n'},
-  {"quiet", no_argument, NULL, 's'},
-  {"silent", no_argument, NULL, 's'},
-  {"keep-files", no_argument, NULL, 'k'},
-  {"prefix", required_argument, NULL, 'f'},
-  {NULL, 0, NULL, 0}
-};
-
 void
 main (argc, argv)
      int argc;
@@ -1254,6 +1263,9 @@ main (argc, argv)
         != EOF)
     switch (optc)
       {
+      case 0:
+       break;
+
       case 'f':
        prefix = optarg;
        break;
@@ -1275,6 +1287,12 @@ main (argc, argv)
        usage ();
       }
 
+  if (flag_version)
+    fprintf (stderr, "%s\n", version_string);
+
+  if (flag_help)
+    usage ();
+
   if (optind >= argc - 1)
     usage ();
 
@@ -1300,7 +1318,8 @@ usage ()
 {
   fprintf (stderr, "\
 Usage: %s [-sk] [-f prefix] [-n digits] [--prefix=prefix]\n\
-       [--digits=digits] [--quiet] [--silent] [--keep-files] file pattern...\n",
+       [--digits=digits] [--quiet] [--silent] [--keep-files]\n\
+       [--help] [--version] file pattern...\n",
           program_name);
   exit (1);
 }
index 2418398..2e611a9 100644 (file)
--- a/src/cut.c
+++ b/src/cut.c
@@ -64,6 +64,7 @@
 #include <getopt.h>
 #include <sys/types.h>
 #include "system.h"
+#include "version.h"
 
 char *xmalloc ();
 char *xrealloc ();
@@ -119,6 +120,9 @@ enum operating_mode
   field_mode
 };
 
+/* The name this program was run with. */
+char *program_name;
+
 static enum operating_mode operating_mode;
 
 /* If nonzero,
@@ -131,8 +135,11 @@ static unsigned char delim;
 /* Nonzero if we have ever read standard input. */
 static int have_read_stdin;
 
-/* The name this program was run with. */
-char *program_name;
+/* If non-zero, display usage information and exit.  */
+static int flag_help;
+
+/* If non-zero, print the version on standard error.  */
+static int flag_version;
 
 static struct option const longopts[] =
 {
@@ -141,6 +148,8 @@ static struct option const longopts[] =
   {"fields", required_argument, 0, 'f'},
   {"delimiter", required_argument, 0, 'd'},
   {"only-delimited", no_argument, 0, 's'},
+  {"help", no_argument, &flag_help, 1},
+  {"version", no_argument, &flag_version, 1},
   {0, 0, 0, 0}
 };
 
@@ -172,6 +181,9 @@ main (argc, argv)
     {
       switch (optc)
        {
+       case 0:
+         break;
+
        case 'b':
        case 'c':
          /* Build the byte list. */
@@ -212,6 +224,12 @@ main (argc, argv)
        }
     }
 
+  if (flag_version)
+    fprintf (stderr, "%s\n", version_string);
+
+  if (flag_help)
+    usage ();
+
   if (operating_mode == undefined_mode)
     usage ();
 
@@ -570,10 +588,11 @@ static void
 usage ()
 {
   fprintf (stderr, "\
-Usage: %s {-b byte-list,--bytes=byte-list} [-n] [file...]\n\
-       %s {-c character-list,--characters=character-list} [file...]\n\
-       %s {-f field-list,--fields=field-list} [-d delim] [-s]\n\
-       [--delimiter=delim] [--only-delimited] [file...]\n",
+Usage: %s {-b byte-list,--bytes=byte-list} [-n] [file...] <options> \n\
+       %s {-c character-list,--characters=character-list} <options> [file...]\n\
+       %s {-f field-list,--fields=field-list} [-d delim] [-s] \n\
+       [--delimiter=delim] [--only-delimited] <options> [file...]\n\
+  Options: [--help] [--version]\n",
           program_name, program_name, program_name);
   exit (2);
 }
index 36d8e05..f269510 100644 (file)
@@ -40,6 +40,7 @@
 #include <getopt.h>
 #include <sys/types.h>
 #include "system.h"
+#include "version.h"
 
 /* The number of bytes added at a time to the amount of memory
    allocated for the output line. */
@@ -60,6 +61,9 @@ static void parse_tabstops ();
 static void usage ();
 static void validate_tabstops ();
 
+/* The name this program was run with. */
+char *program_name;
+
 /* If nonzero, convert blanks even after nonblank characters have been
    read on the line. */
 static int convert_entire_line;
@@ -91,13 +95,18 @@ static int have_read_stdin;
 /* Status to return to the system. */
 static int exit_status;
 
-/* The name this program was run with. */
-char *program_name;
+/* If non-zero, display usage information and exit.  */
+static int flag_help;
+
+/* If non-zero, print the version on standard error.  */
+static int flag_version;
 
 static struct option const longopts[] =
 {
   {"tabs", required_argument, NULL, 't'},
   {"initial", no_argument, NULL, 'i'},
+  {"help", no_argument, &flag_help, 1},
+  {"version", no_argument, &flag_version, 1},
   {NULL, 0, NULL, 0}
 };
 
@@ -121,6 +130,9 @@ main (argc, argv)
     {
       switch (c)
        {
+       case 0:
+         break;
+
        case '?':
          usage ();
        case 'i':
@@ -141,6 +153,12 @@ main (argc, argv)
        }
     }
 
+  if (flag_version)
+    fprintf (stderr, "%s\n", version_string);
+
+  if (flag_help)
+    usage ();
+
   add_tabstop (tabval);
 
   validate_tabstops (tab_list, first_free_tab);
@@ -363,7 +381,7 @@ usage ()
 {
   fprintf (stderr, "\
 Usage: %s [-tab1[,tab2[,...]]] [-t tab1[,tab2[,...]]] [-i]\n\
-       [--tabs=tab1[,tab2[,...]]] [--initial] [file...]\n",
+       [--tabs=tab1[,tab2[,...]]] [--initial] [--help] [--version] [file...]\n",
           program_name);
   exit (1);
 }
index fcdd885..ca20a28 100644 (file)
 #include <getopt.h>
 #include <sys/types.h>
 #include "system.h"
+#include "version.h"
 
 char *xrealloc ();
 void error ();
 
+/* The name this program was run with. */
+char *program_name;
+
 static int adjust_column ();
 static int fold_file ();
 
@@ -40,17 +44,32 @@ static int count_bytes;
 /* If nonzero, at least one of the files we read was standard input. */
 static int have_read_stdin;
 
-/* The name this program was run with. */
-char *program_name;
+/* If non-zero, display usage information and exit.  */
+static int flag_help;
+
+/* If non-zero, print the version on standard error.  */
+static int flag_version;
 
 static struct option const longopts[] =
 {
   {"bytes", no_argument, NULL, 'b'},
   {"spaces", no_argument, NULL, 's'},
   {"width", required_argument, NULL, 'w'},
+  {"help", no_argument, &flag_help, 1},
+  {"version", no_argument, &flag_version, 1},
   {NULL, 0, NULL, 0}
 };
 \f
+static void
+usage ()
+{
+  fprintf (stderr, "\
+Usage: %s [-bs] [-w width] [--bytes] [--spaces] [--width=width]\n\
+       [--help] [--version] [file...]\n",
+          program_name);
+  exit (1);
+}
+
 void
 main (argc, argv)
      int argc;
@@ -69,6 +88,9 @@ main (argc, argv)
     {
       switch (optc)
        {
+       case 0:
+         break;
+
        case 'b':               /* Count bytes rather than columns. */
          count_bytes = 1;
          break;
@@ -84,13 +106,16 @@ main (argc, argv)
          break;
 
        default:
-         fprintf (stderr, "\
-Usage: %s [-bs] [-w width] [--bytes] [--spaces] [--width=width] [file...]\n",
-                  argv[0]);
-         exit (1);
+         usage ();
        }
     }
 
+  if (flag_version)
+    fprintf (stderr, "%s\n", version_string);
+
+  if (flag_help)
+    usage ();
+
   if (argc == optind)
     errs |= fold_file ("-", width);
   else
index c818c66..6d7ac55 100644 (file)
@@ -37,6 +37,7 @@
 #include <getopt.h>
 #include <sys/types.h>
 #include "system.h"
+#include "version.h"
 
 /* Number of lines/chars/blocks to head. */
 #define DEFAULT_NUMBER 10
@@ -75,6 +76,12 @@ char *program_name;
 /* Have we ever read standard input?  */
 static int have_read_stdin;
 
+/* If non-zero, display usage information and exit.  */
+static int flag_help;
+
+/* If non-zero, print the version on standard error.  */
+static int flag_version;
+
 static struct option const long_options[] =
 {
   {"bytes", required_argument, NULL, 'c'},
@@ -82,6 +89,8 @@ static struct option const long_options[] =
   {"quiet", no_argument, NULL, 'q'},
   {"silent", no_argument, NULL, 'q'},
   {"verbose", no_argument, NULL, 'v'},
+  {"help", no_argument, &flag_help, 1},
+  {"version", no_argument, &flag_version, 1},
   {NULL, 0, NULL, 0}
 };
 
@@ -156,6 +165,9 @@ main (argc, argv)
     {
       switch (c)
        {
+       case 0:
+         break;
+
        case 'c':
          unit_size = 1;
          parse_unit (optarg);
@@ -181,6 +193,12 @@ main (argc, argv)
        }
     }
 
+  if (flag_version)
+    fprintf (stderr, "%s\n", version_string);
+
+  if (flag_help)
+    usage ();
+
   if (number == -1)
     number = DEFAULT_NUMBER;
 
@@ -368,7 +386,8 @@ usage ()
 {
   fprintf (stderr, "\
 Usage: %s [-c N[bkm]] [-n N] [-qv] [--bytes=N[bkm]] [--lines=N]\n\
-       [--quiet] [--silent] [--verbose] [file...]\n\
-       %s [-Nbcklmqv] [file...]\n", program_name, program_name);
+       [--quiet] [--silent] [--verbose] [--help] [--version] [file...]\n\
+       %s [-Nbcklmqv] [--help] [--version] [file...]\n",
+       program_name, program_name);
   exit (1);
 }
index 6aac838..39599ef 100644 (file)
@@ -24,6 +24,7 @@
 #include <sys/types.h>
 #include <getopt.h>
 #include "system.h"
+#include "version.h"
 
 char *xmalloc ();
 char *xrealloc ();
@@ -66,6 +67,9 @@ struct seq
   struct line *lines;
 };
 
+/* The name this program was run with. */
+char *program_name;
+
 /* If nonzero, print unpairable lines in file 1 or 2. */
 static int print_unpairables_1, print_unpairables_2;
 
@@ -89,8 +93,23 @@ static struct outlist *outlist_end;
    tab character. */
 static char tab;
 
-/* The name this program was run with. */
-char *program_name;
+/* If non-zero, display usage information and exit.  */
+static int flag_help;
+
+/* If non-zero, print the version on standard error.  */
+static int flag_version;
+
+/* When using getopt_long_only, no long option can start with
+   a character that is a short option. */
+static struct option const longopts[] =
+{
+  {"j", required_argument, NULL, 'j'},
+  {"j1", required_argument, NULL, '1'},
+  {"j2", required_argument, NULL, '2'},
+  {"help", no_argument, &flag_help, 1},
+  {"version", no_argument, &flag_version, 1},
+  {NULL, 0, NULL, 0}
+};
 
 /* Fill in the `fields' structure in LINE. */
 
@@ -546,16 +565,6 @@ add_field_list (str)
   return added;
 }
 
-/* When using getopt_long_only, no long option can start with
-   a character that is a short option. */
-static struct option const longopts[] =
-{
-  {"j", required_argument, NULL, 'j'},
-  {"j1", required_argument, NULL, '1'},
-  {"j2", required_argument, NULL, '2'},
-  {NULL, 0, NULL, 0}
-};
-
 void
 main (argc, argv)
      int argc;
@@ -574,6 +583,9 @@ main (argc, argv)
     {
       switch (optc)
        {
+       case 0:
+         break;
+
        case 'a':
          val = atoi (optarg);
          if (val == 1)
@@ -647,6 +659,12 @@ main (argc, argv)
        }
       prev_optc = optc;
     }
+
+  if (flag_version)
+    fprintf (stderr, "%s\n", version_string);
+
+  if (flag_help)
+    usage ();
   
   if (nfiles != 2)
     usage ();
index d71aa70..1444002 100644 (file)
--- a/src/nl.c
+++ b/src/nl.c
@@ -24,6 +24,7 @@
 #include <regex.h>
 #include "linebuffer.h"
 #include "system.h"
+#include "version.h"
 
 #ifndef TRUE
 #define TRUE   1
@@ -47,7 +48,26 @@ enum section
 {
   Header, Body, Footer, Text
 };
-\f
+
+char *xmalloc ();
+char *xrealloc ();
+void error ();
+
+static enum section check_section ();
+static int build_type_arg ();
+static int nl_file ();
+static void usage ();
+static void process_file ();
+static void proc_header ();
+static void proc_body ();
+static void proc_footer ();
+static void proc_text ();
+static void print_lineno ();
+static void build_print_fmt ();
+
+/* The name this program was run with. */
+char *program_name;
+
 /* Format of body lines (-b).  */
 static char *body_type = "t";
 
@@ -126,28 +146,15 @@ static enum number_format lineno_format = FORMAT_RIGHT_NOLZ;
 /* Current print line number.  */
 static int line_no;
 
-/* The name this program was run with. */
-char *program_name;
-
 /* Nonzero if we have ever read standard input. */
 static int have_read_stdin;
 
-char *xmalloc ();
-char *xrealloc ();
-void error ();
+/* If non-zero, display usage information and exit.  */
+static int flag_help;
+
+/* If non-zero, print the version on standard error.  */
+static int flag_version;
 
-static enum section check_section ();
-static int build_type_arg ();
-static int nl_file ();
-static void usage ();
-static void process_file ();
-static void proc_header ();
-static void proc_body ();
-static void proc_footer ();
-static void proc_text ();
-static void print_lineno ();
-static void build_print_fmt ();
-\f
 static struct option const longopts[] =
 {
   {"header-numbering", required_argument, NULL, 'h'},
@@ -161,6 +168,8 @@ static struct option const longopts[] =
   {"number-width", required_argument, NULL, 'w'},
   {"number-format", required_argument, NULL, 'n'},
   {"section-delimiter", required_argument, NULL, 'd'},
+  {"help", no_argument, &flag_help, 1},
+  {"version", no_argument, &flag_version, 1},
   {NULL, 0, NULL, 0}
 };
 
@@ -179,6 +188,9 @@ main (argc, argv)
     {
       switch (c)
        {
+       case 0:
+         break;
+
        case 'h':
          if (build_type_arg (&header_type, &header_regex) != TRUE)
            usage ();
@@ -250,6 +262,12 @@ main (argc, argv)
        }
     }
 
+  if (flag_version)
+    fprintf (stderr, "%s\n", version_string);
+
+  if (flag_help)
+    usage ();
+
   /* Initialize the section delimiters.  */
   c = strlen (section_del);
 
@@ -541,7 +559,7 @@ Usage: %s [-h header-style] [-b body-style] [-f footer-style] [-p] [-d cc]\n\
        [--first-page=number] [--page-increment=number] [--no-renumber]\n\
        [--join-blank-lines=number] [--number-separator=string]\n\
        [--number-width=number] [--number-format={ln,rn,rz}]\n\
-       [--section-delimiter=cc] [file...]\n",
+       [--section-delimiter=cc] [--help] [--version] [file...]\n",
           program_name);
   exit (2);
 }
index 2047683..f79b3cc 100644 (file)
--- a/src/od.c
+++ b/src/od.c
@@ -37,6 +37,7 @@ char *alloca ();
 #include <getopt.h>
 #include <sys/types.h>
 #include "system.h"
+#include "version.h"
 
 #if defined(__GNUC__) || defined(STDC_HEADERS)
 #include <float.h>
@@ -138,6 +139,9 @@ struct tspec
     char *fmt_string;
   };
 
+/* The name this program was run with.  */
+char *program_name;
+
 /* Convert the number of 8-bit bytes of a binary representation to
    the number of characters (digits + sign if the type is signed)
    required to represent the same quantity in the specified base/type.
@@ -267,6 +271,12 @@ static enum size_spec integral_type_size[MAX_INTEGRAL_TYPE_SIZE + 1];
 #define MAX_FP_TYPE_SIZE sizeof(LONG_DOUBLE)
 static enum size_spec fp_type_size[MAX_FP_TYPE_SIZE + 1];
 
+/* If non-zero, display usage information and exit.  */
+static int flag_help;
+
+/* If non-zero, print the version on standard error.  */
+static int flag_version;
+
 static struct option const long_options[] =
 {
   /* POSIX options.  */
@@ -280,12 +290,11 @@ static struct option const long_options[] =
   {"compatible", no_argument, NULL, 'C'},
   {"strings", optional_argument, NULL, 's'},
   {"width", optional_argument, NULL, 'w'},
+  {"help", no_argument, &flag_help, 1},
+  {"version", no_argument, &flag_version, 1},
   {NULL, 0, NULL, 0}
 };
 
-/* The name this program was run with.  */
-char *program_name;
-
 static void
 usage ()
 {
@@ -293,7 +302,7 @@ usage ()
 Usage: %s [-abcdfhiloxv] [-s[bytes]] [-w[bytes]] [-A radix] [-j bytes]\n\
        [-N bytes] [-t type] [--skip-bytes=bytes] [--address-radix=radix]\n\
        [--read-bytes=bytes] [--format=type] [--output-duplicates]\n\
-       [--strings[=bytes]] [--width[=bytes]] [file...]\n",
+       [--strings[=bytes]] [--width[=bytes]] [--help] [--version] [file...]\n",
           program_name);
   exit (1);
 }
@@ -1659,6 +1668,9 @@ main (argc, argv)
 
       switch (c)
        {
+       case 0:
+         break;
+
        case 'A':
          switch (optarg[0])
            {
@@ -1776,6 +1788,12 @@ main (argc, argv)
        }
     }
 
+  if (flag_version)
+    fprintf (stderr, "%s\n", version_string);
+
+  if (flag_help)
+    usage ();
+
   if (flag_dump_strings && n_specs > 0)
     error (2, 0, "no type may be specified when dumping strings");
 
index 3cd95d3..bc9b7e5 100644 (file)
@@ -42,6 +42,7 @@
 #include <getopt.h>
 #include <sys/types.h>
 #include "system.h"
+#include "version.h"
 
 void error ();
 char *xmalloc ();
@@ -77,10 +78,18 @@ static char *delims;
 /* A pointer to the character after the end of `delims'. */
 static char *delim_end;
 
+/* If non-zero, display usage information and exit.  */
+static int flag_help;
+
+/* If non-zero, print the version on standard error.  */
+static int flag_version;
+
 static struct option const longopts[] =
 {
   {"serial", no_argument, 0, 's'},
   {"delimiters", required_argument, 0, 'd'},
+  {"help", no_argument, &flag_help, 1},
+  {"version", no_argument, &flag_version, 1},
   {0, 0, 0, 0}
 };
 
@@ -104,6 +113,9 @@ main (argc, argv)
     {
       switch (optc)
        {
+       case 0:
+         break;
+
        case 'd':
          /* Delimiter character(s). */
          if (optarg[0] == '\0')
@@ -120,6 +132,12 @@ main (argc, argv)
        }
     }
 
+  if (flag_version)
+    fprintf (stderr, "%s\n", version_string);
+
+  if (flag_help)
+    usage ();
+
   if (optind == argc)
     argv[argc++] = "-";
 
@@ -453,7 +471,7 @@ usage ()
 {
   fprintf (stderr, "\
 Usage: %s [-s] [-d delim-list] [--serial] [--delimiters=delim-list]\n\
-       [file...]\n",
+       [--help] [--version] [file...]\n",
           program_name);
   exit (1);
 }
index bf4517d..876387d 100644 (file)
--- a/src/pr.c
+++ b/src/pr.c
@@ -99,6 +99,7 @@
 #include <sys/types.h>
 #include <time.h>
 #include "system.h"
+#include "version.h"
 
 char *xmalloc ();
 char *xrealloc ();
@@ -195,6 +196,9 @@ typedef struct COLUMN COLUMN;
 
 #define NULLCOL (COLUMN *)0
 
+/* The name under which this program was invoked. */
+char *program_name;
+
 /* All of the columns to print.  */
 static COLUMN *column_vector;
 
@@ -246,9 +250,6 @@ static int standard_header = TRUE;
 /* (-f) True means use formfeeds instead of newlines to separate pages. */
 static int use_form_feed = FALSE;
 
-/* True means we haven't encountered any filenames in the argument list. */
-static int input_is_stdin = TRUE;
-
 /* True means we have read the standard input. */
 static int have_read_stdin = FALSE;
 
@@ -427,70 +428,33 @@ static int *clump_buff;
 /* True means we truncate lines longer than chars_per_column. */
 static int truncate_lines = FALSE;
 
-/* The name under which this program was invoked. */
-char *program_name;
-\f
+/* If non-zero, display usage information and exit.  */
+static int flag_help;
+
+/* If non-zero, print the version on standard error.  */
+static int flag_version;
+
+static struct option const long_options[] =
+{
+  {"help", no_argument, &flag_help, 1},
+  {"version", no_argument, &flag_version, 1},
+  {0, 0, 0, 0}
+};
+
 void
 main (argc, argv)
      int argc;
      char **argv;
 {
   int c;
-  char *s;
-  int files = 0;
-  char **file_names, **file_name_vector;
   int accum = 0;
 
   program_name = argv[0];
 
-  file_name_vector = (char **) xmalloc (argc * sizeof (char **));
-  file_names = file_name_vector;
-
-  for (;;)
+  while ((c = getopt_long (argc, argv,
+                          "0123456789abcde::fFh:i::l:mn::o:rs::tvw:",
+                          long_options, (int *) 0)) != EOF)
     {
-      c = getopt (argc, argv, "-0123456789abcde::fFh:i::l:mn::o:rs::tvw:");
-
-      if (c == 1)              /* Non-option argument. */
-       {
-         s = optarg;
-         if (*s == '+')
-           {
-             ++s;
-             if (!ISDIGIT (*s))
-               usage ("`+' requires a numeric argument");
-             first_page_number = atoi (s);
-           }
-         else
-           {
-             *file_names++ = optarg;
-             ++files;
-           }
-       }
-      else if (files > 0)
-       {
-         if (parallel_files && explicit_columns)
-           error (1, 0,
-"Cannot specify number of columns when printing in parallel.");
-
-         if (parallel_files && print_across_flag)
-           error (1, 0,
-"Cannot specify both printing across and printing in parallel.");
-
-         if (parallel_files)
-           print_files (files, file_name_vector);
-         else
-           {
-             file_names = file_name_vector;
-             while (files--)
-               print_files (1, file_names++);
-           }
-
-         input_is_stdin = FALSE;
-         file_names = file_name_vector;
-         files = 0;
-         cleanup ();
-       }
-
       if (ISDIGIT (c))
        {
          accum = accum * 10 + c - '0';
@@ -508,6 +472,9 @@ main (argc, argv)
 
       switch (c)
        {
+       case 0: /* getopt long option */
+         break;
+
        case 'a':
          print_across_flag = TRUE;
          storing_columns = FALSE;
@@ -566,6 +533,7 @@ main (argc, argv)
          use_column_separator = TRUE;
          if (optarg)
            {
+             char *s;
              s = optarg;
              column_separator = *s;
              if (*++s)
@@ -573,7 +541,7 @@ main (argc, argv)
                  fprintf (stderr, "\
 %s: extra characters in the argument to the `-s' option: `%s'\n",
                           program_name, s);
-                 usage ((char *) 0);
+                 usage ();
                }
            }
          break;
@@ -586,17 +554,56 @@ main (argc, argv)
        case 'w':
          chars_per_line = atoi (optarg);
          break;
-       case '?':
-         usage ((char *) 0);
+       default:
+         usage ();
          break;
        }
+    }
+
+  if (flag_version)
+    fprintf (stderr, "%s\n", version_string);
+
+  if (flag_help)
+    usage ();
+
+  if (parallel_files && explicit_columns)
+    error (1, 0,
+  "Cannot specify number of columns when printing in parallel.");
+
+  if (parallel_files && print_across_flag)
+    error (1, 0,
+  "Cannot specify both printing across and printing in parallel.");
+
+  if (optind >= 2 && strcmp (argv[optind - 1], "--") == 0)
+    {
+      /* We've seen `--', so interpret all remaining arguments as
+        filenames. */
+    }
+  else
+    {
+      for ( ; optind < argc && argv[optind][0] == '+'; optind++)
+       {
+         first_page_number = atoi (&argv[optind][1]);
+       }
+    }
 
-      if (c == EOF)
-       break;
+  if (optind >= argc)
+    {
+      /* No file arguments specified;  read from standard input.  */
+      print_files (0, (char **) 0);
+    }
+  else
+    {
+      if (parallel_files)
+       print_files (argc - optind, &argv[optind]);
+      else
+       {
+         for ( ; optind < argc; optind++)
+           print_files (1, &argv[optind]);
+       }
     }
 
-  if (input_is_stdin)
-    print_files (0, (char **) 0);
+  cleanup ();
 
   if (have_read_stdin && fclose (stdin) == EOF)
     error (1, errno, "standard input");
@@ -629,7 +636,7 @@ getoptarg (arg, switch_char, character, number)
          fprintf (stderr, "\
 %s: extra characters in the argument to the `-%c' option: `%s'\n",
                   program_name, switch_char, arg);
-         usage ((char *) 0);
+         usage ();
        }
     }
 }
@@ -1821,17 +1828,13 @@ cleanup ()
 /* Complain, print a usage message, and die. */
 
 static void
-usage (reason)
-     char *reason;
+usage ()
 {
-  if (reason)
-    fprintf (stderr, "%s: %s\n", program_name, reason);
-
   fprintf (stderr, "\
 Usage: %s [+PAGE] [-COLUMN] [-abcdfFmrtv] [-e[in-tab-char[in-tab-width]]]\n\
        [-h header] [-i[out-tab-char[out-tab-width]]] [-l page-length]\n\
        [-n[number-separator[digits]]] [-o left-margin]\n\
-       [-s[column-separator]] [-w page-width] [file...]\n",
+       [-s[column-separator]] [-w page-width] [--help] [--version] [file...]\n",
           program_name);
   exit (2);
 }
index 980edf3..07b37ae 100644 (file)
@@ -25,6 +25,7 @@
 #include <getopt.h>
 #include <sys/types.h>
 #include "system.h"
+#include "version.h"
 
 char *xmalloc ();
 void error ();
@@ -38,7 +39,7 @@ static void cwrite ();
 static void lines_split ();
 static void next_file_name ();
 
-/* Name under which this program was invoked.  */
+/* The name this program was run with. */
 char *program_name;
 
 /* Base name of output files.  */
@@ -64,7 +65,23 @@ static int input_desc;
 
 /* Descriptor on which output file is open.  */
 static int output_desc;
-\f
+
+/* If non-zero, display usage information and exit.  */
+static int flag_help;
+
+/* If non-zero, print the version on standard error.  */
+static int flag_version;
+
+static struct option const longopts[] =
+{
+  {"bytes", required_argument, NULL, 'b'},
+  {"lines", required_argument, NULL, 'l'},
+  {"line-bytes", required_argument, NULL, 'C'},
+  {"help", no_argument, &flag_help, 1},
+  {"version", no_argument, &flag_version, 1},
+  {NULL, 0, NULL, 0}
+};
+
 static void
 usage (reason)
     char *reason;
@@ -74,18 +91,10 @@ usage (reason)
   fprintf (stderr, "\
 Usage: %s [-lines] [-l lines] [-b bytes[bkm]] [-C bytes[bkm]]\n\
        [--lines=lines] [--bytes=bytes[bkm]] [--line-bytes=bytes[bkm]]\n\
-       [infile [outfile-prefix]]\n",
+       [--help] [--version] [infile [outfile-prefix]]\n",
           program_name);
   exit (2);
 }
-\f
-static struct option const longopts[] =
-{
-  {"bytes", required_argument, NULL, 'b'},
-  {"lines", required_argument, NULL, 'l'},
-  {"line-bytes", required_argument, NULL, 'C'},
-  {NULL, 0, NULL, 0}
-};
 
 void
 main (argc, argv)
@@ -123,6 +132,9 @@ main (argc, argv)
 
       switch (c)
        {
+       case 0:
+         break;
+
        case 'b':
          if (split_type != type_undef)
            usage ("cannot split in more than one way");
@@ -172,6 +184,12 @@ main (argc, argv)
        }
     }
 
+  if (flag_version)
+    fprintf (stderr, "%s\n", version_string);
+
+  if (flag_help)
+    usage ();
+
   /* Handle default case.  */
   if (split_type == type_undef)
     {
index a7079b8..c10f0b8 100644 (file)
--- a/src/sum.c
+++ b/src/sum.c
@@ -23,6 +23,7 @@
 #include <sys/types.h>
 #include <getopt.h>
 #include "system.h"
+#include "version.h"
 
 static int bsd_sum_file ();
 static int sysv_sum_file ();
@@ -38,12 +39,28 @@ static int have_read_stdin;
 /* Right-rotate 32-bit integer variable C. */
 #define ROTATE_RIGHT(c) if ((c) & 01) (c) = ((c) >>1) + 0x8000; else (c) >>= 1;
 
+/* If non-zero, display usage information and exit.  */
+static int flag_help;
+
+/* If non-zero, print the version on standard error.  */
+static int flag_version;
+
 static struct option const longopts[] =
 {
   {"sysv", no_argument, NULL, 's'},
+  {"help", no_argument, &flag_help, 1},
+  {"version", no_argument, &flag_version, 1},
   {NULL, 0, NULL, 0}
 };
 
+static void
+usage ()
+{
+  fprintf (stderr, "\
+Usage: %s [-rs] [--help] [--version] [--sysv] [file...]\n", program_name);
+  exit (1);
+}
+
 void
 main (argc, argv)
      int argc;
@@ -61,6 +78,9 @@ main (argc, argv)
     {
       switch (optc)
        {
+       case 0:
+         break;
+
        case 'r':               /* For SysV compatibility. */
          sum_func = bsd_sum_file;
          break;
@@ -69,13 +89,17 @@ main (argc, argv)
          sum_func = sysv_sum_file;
          break;
 
-       case '?':
-         fprintf (stderr, "\
-Usage: %s [-rs] [--sysv] [file...]\n", argv[0]);
-         exit (1);
+       default:
+         usage ();
        }
     }
 
+  if (flag_version)
+    fprintf (stderr, "%s\n", version_string);
+
+  if (flag_help)
+    usage ();
+
   files_given = argc - optind;
   if (files_given == 0)
     {
index 8f3c94a..8107823 100644 (file)
--- a/src/tac.c
+++ b/src/tac.c
@@ -41,6 +41,7 @@ tac -r -s '.\|
 #include <signal.h>
 #include <regex.h>
 #include "system.h"
+#include "version.h"
 
 #ifndef STDC_HEADERS
 char *malloc ();
@@ -101,14 +102,32 @@ static unsigned buffer_size;
 /* The compiled regular expression representing `separator'. */
 static struct re_pattern_buffer compiled_separator;
 
+/* If non-zero, display usage information and exit.  */
+static int flag_help;
+
+/* If non-zero, print the version on standard error.  */
+static int flag_version;
+
 static struct option const longopts[] =
 {
   {"before", no_argument, &separator_ends_record, 0},
   {"regex", no_argument, &sentinel_length, 0},
   {"separator", required_argument, NULL, 's'},
+  {"help", no_argument, &flag_help, 1},
+  {"version", no_argument, &flag_version, 1},
   {NULL, 0, NULL, 0}
 };
 
+static void
+usage ()
+{
+  fprintf (stderr, "\
+Usage: %s [-br] [-s separator] [--before] [--regex] [--separator=separator]\n\
+      [--help] [--version] [file...]\n",
+          program_name);
+  exit (1);
+}
+
 void
 main (argc, argv)
      int argc;
@@ -143,14 +162,16 @@ main (argc, argv)
            error (1, 0, "separator cannot be empty");
          break;
        default:
-         fprintf (stderr, "\
-Usage: %s [-br] [-s separator] [--before] [--regex] [--separator=separator]\n\
-       [file...]\n",
-                  program_name);
-         exit (1);
+         usage ();
        }
     }
 
+  if (flag_version)
+    fprintf (stderr, "%s\n", version_string);
+
+  if (flag_help)
+    usage ();
+
   if (sentinel_length == 0)
     {
       compiled_separator.allocated = 100;
index 138c228..d3bc8cd 100644 (file)
@@ -50,6 +50,7 @@
 #include <sys/types.h>
 #include <signal.h>
 #include "system.h"
+#include "version.h"
 
 /* Number of items to tail. */
 #define DEFAULT_NUMBER 10
@@ -111,6 +112,12 @@ char *program_name;
 /* Nonzero if we have ever read standard input. */
 static int have_read_stdin;
 
+/* If non-zero, display usage information and exit.  */
+static int flag_help;
+
+/* If non-zero, print the version on standard error.  */
+static int flag_version;
+
 static struct option const long_options[] =
 {
   {"bytes", required_argument, NULL, 'c'},
@@ -119,6 +126,8 @@ static struct option const long_options[] =
   {"quiet", no_argument, NULL, 'q'},
   {"silent", no_argument, NULL, 'q'},
   {"verbose", no_argument, NULL, 'v'},
+  {"help", no_argument, &flag_help, 1},
+  {"version", no_argument, &flag_version, 1},
   {NULL, 0, NULL, 0}
 };
 
@@ -208,6 +217,9 @@ main (argc, argv)
     {
       switch (c)
        {
+       case 0:
+         break;
+
        case 'c':
          unit_size = 1;
          parse_unit (optarg);
@@ -244,6 +256,12 @@ main (argc, argv)
        }
     }
 
+  if (flag_version)
+    fprintf (stderr, "%s\n", version_string);
+
+  if (flag_help)
+    usage ();
+
   if (number == -1)
     number = DEFAULT_NUMBER;
 
@@ -987,7 +1005,8 @@ usage ()
 {
   fprintf (stderr, "\
 Usage: %s [-c [+]N[bkm]] [-n [+]N] [-fqv] [--bytes=[+]N[bkm]] [--lines=[+]N]\n\
-       [--follow] [--quiet] [--silent] [--verbose] [file...]\n\
+       [--follow] [--quiet] [--silent] [--verbose] [--help] [--version]\n\
+       [file...]\n\
        %s [{-,+}Nbcfklmqv] [file...]\n", program_name, program_name);
   exit (1);
 }
index f4cc3a7..9010f45 100644 (file)
--- a/src/tr.c
+++ b/src/tr.c
@@ -26,6 +26,7 @@
 #include <sys/types.h>
 #include "getopt.h"
 #include "system.h"
+#include "version.h"
 
 #ifndef LONG_MAX
 #define LONG_MAX 0x7FFFFFFF
@@ -286,12 +287,20 @@ static SET_TYPE in_delete_set[N_CHARS];
    two specification strings and the delete switch is not given. */
 static char xlate[N_CHARS];
 
+/* If non-zero, display usage information and exit.  */
+static int flag_help;
+
+/* If non-zero, print the version on standard error.  */
+static int flag_version;
+
 static struct option const long_options[] =
 {
   {"complement", no_argument, NULL, 'c'},
   {"delete", no_argument, NULL, 'd'},
   {"squeeze-repeats", no_argument, NULL, 's'},
   {"truncate-set1", no_argument, NULL, 't'},
+  {"help", no_argument, &flag_help, 1},
+  {"version", no_argument, &flag_version, 1},
   {NULL, 0, NULL, 0}
 };
 \f
@@ -300,7 +309,7 @@ usage ()
 {
   fprintf (stderr, "\
 Usage: %s [-cdst] [--complement] [--delete] [--squeeze-repeats]\n\
-       [--truncate-set1] string1 [string2]\n",
+       [--truncate-set1] [--help] [--version] string1 [string2]\n",
           program_name);
   exit (2);
 }
@@ -1656,6 +1665,12 @@ main (argc, argv)
        }
     }
 
+  if (flag_version)
+    fprintf (stderr, "%s\n", version_string);
+
+  if (flag_help)
+    usage ();
+
   posix_pedantic = (getenv ("POSIXLY_CORRECT") != NULL);
 
   non_option_args = argc - optind;
index 01dc417..6fc5e78 100644 (file)
@@ -42,6 +42,7 @@
 #include <getopt.h>
 #include <sys/types.h>
 #include "system.h"
+#include "version.h"
 
 /* The number of bytes added at a time to the amount of memory
    allocated for the output line. */
@@ -62,6 +63,9 @@ static void unexpand ();
 static void usage ();
 static void validate_tabstops ();
 
+/* The name this program was run with. */
+char *program_name;
+
 /* If nonzero, convert blanks even after nonblank characters have been
    read on the line. */
 static int convert_entire_line;
@@ -93,13 +97,18 @@ static int have_read_stdin;
 /* Status to return to the system. */
 static int exit_status;
 
-/* The name this program was run with. */
-char *program_name;
+/* If non-zero, display usage information and exit.  */
+static int flag_help;
+
+/* If non-zero, print the version on standard error.  */
+static int flag_version;
 
 static struct option const longopts[] =
 {
   {"tabs", required_argument, NULL, 't'},
   {"all", no_argument, NULL, 'a'},
+  {"help", no_argument, &flag_help, 1},
+  {"version", no_argument, &flag_version, 1},
   {NULL, 0, NULL, 0}
 };
 
@@ -123,6 +132,9 @@ main (argc, argv)
     {
       switch (c)
        {
+       case 0:
+         break;
+
        case '?':
          usage ();
        case 'a':
@@ -144,6 +156,12 @@ main (argc, argv)
        }
     }
 
+  if (flag_version)
+    fprintf (stderr, "%s\n", version_string);
+
+  if (flag_help)
+    usage ();
+
   add_tabstop (tabval);
 
   validate_tabstops (tab_list, first_free_tab);
@@ -418,7 +436,7 @@ usage ()
 {
   fprintf (stderr, "\
 Usage: %s [-tab1[,tab2[,...]]] [-t tab1[,tab2[,...]]] [-a]\n\
-       [--tabs=tab1[,tab2[,...]]] [--all] [file...]\n",
+       [--tabs=tab1[,tab2[,...]]] [--all] [--help] [--version] [file...]\n",
           program_name);
   exit (1);
 }
index 944727f..b902602 100644 (file)
@@ -25,6 +25,7 @@
 #include <sys/types.h>
 #include "system.h"
 #include "linebuffer.h"
+#include "version.h"
 
 #define min(x, y) ((x) < (y) ? (x) : (y))
 
@@ -36,6 +37,9 @@ static void check_file ();
 static void usage ();
 static void writeline ();
 
+/* The name this program was run with. */
+char *program_name;
+
 /* Number of fields to skip on each line when doing comparisons. */
 static int skip_fields;
 
@@ -65,8 +69,11 @@ enum output_mode
 /* Which lines to output. */
 static enum output_mode mode;
 
-/* The name this program was run with. */
-char *program_name;
+/* If non-zero, display usage information and exit.  */
+static int flag_help;
+
+/* If non-zero, print the version on standard error.  */
+static int flag_version;
 
 static struct option const longopts[] =
 {
@@ -76,6 +83,8 @@ static struct option const longopts[] =
   {"skip-fields", required_argument, NULL, 'f'},
   {"skip-chars", required_argument, NULL, 's'},
   {"check-chars", required_argument, NULL, 'w'},
+  {"help", no_argument, &flag_help, 1},
+  {"version", no_argument, &flag_version, 1},
   {NULL, 0, NULL, 0}
 };
 \f
@@ -99,6 +108,9 @@ main (argc, argv)
     {
       switch (optc)
        {
+       case 0:
+         break;
+
        case '0':
        case '1':
        case '2':
@@ -141,6 +153,12 @@ main (argc, argv)
        }
     }
 
+  if (flag_version)
+    fprintf (stderr, "%s\n", version_string);
+
+  if (flag_help)
+    usage ();
+
   while (optind < argc && argv[optind][0] == '+')
     skip_chars = atoi (argv[optind++]);
 
@@ -314,7 +332,7 @@ usage ()
 Usage: %s [-cdu] [-f skip-fields] [-s skip-chars] [-w check-chars]\n\
        [-#skip-fields] [+#skip-chars] [--count] [--repeated] [--unique]\n\
        [--skip-fields=skip-fields] [--skip-chars=skip-chars]\n\
-       [--check-chars=check-chars] [infile] [outfile]\n",
+       [--check-chars=check-chars] [--help] [--version] [infile] [outfile]\n",
           program_name);
   exit (1);
 }
index 5bb4f24..678e179 100644 (file)
--- a/src/wc.c
+++ b/src/wc.c
@@ -22,6 +22,7 @@
 #include <getopt.h>
 #include <sys/types.h>
 #include "system.h"
+#include "version.h"
 
 /* Size of atomic reads. */
 #define BUFFER_SIZE (16 * 1024)
@@ -32,6 +33,9 @@ static void wc ();
 static void wc_file ();
 static void write_counts ();
 
+/* The name this program was run with. */
+char *program_name;
+
 /* Cumulative number of lines, words, and chars in all files so far. */
 static unsigned long total_lines, total_words, total_chars;
 
@@ -41,21 +45,36 @@ static int print_lines, print_words, print_chars;
 /* Nonzero if we have ever read the standard input. */
 static int have_read_stdin;
 
-/* The name this program was run with. */
-char *program_name;
-
 /* The error code to return to the system. */
 static int exit_status;
 
+/* If non-zero, display usage information and exit.  */
+static int flag_help;
+
+/* If non-zero, print the version on standard error.  */
+static int flag_version;
+
 static struct option const longopts[] =
 {
   {"bytes", no_argument, NULL, 'c'},
   {"chars", no_argument, NULL, 'c'},
   {"lines", no_argument, NULL, 'l'},
   {"words", no_argument, NULL, 'w'},
+  {"help", no_argument, &flag_help, 1},
+  {"version", no_argument, &flag_version, 1},
   {NULL, 0, NULL, 0}
 };
 
+static void
+usage ()
+{
+  fprintf (stderr, "\
+Usage: %s [-clw] [--bytes] [--chars] [--lines] [--words]\n\
+       [--help] [--version] [file...]\n",
+          program_name);
+  exit (1);
+}
+
 void
 main (argc, argv)
      int argc;
@@ -72,6 +91,9 @@ main (argc, argv)
   while ((optc = getopt_long (argc, argv, "clw", longopts, (int *) 0)) != EOF)
     switch (optc)
       {
+      case 0:
+       break;
+
       case 'c':
        print_chars = 1;
        break;
@@ -85,11 +107,15 @@ main (argc, argv)
        break;
 
       default:
-       fprintf (stderr, "\
-Usage: %s [-clw] [--bytes] [--chars] [--lines] [--words] [file...]\n", argv[0]);
-       exit (1);
+       usage ();
       }
 
+  if (flag_version)
+    fprintf (stderr, "%s\n", version_string);
+
+  if (flag_help)
+    usage ();
+
   if (print_lines + print_words + print_chars == 0)
     print_lines = print_words = print_chars = 1;