bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

getopt cleanup for coreutils


From: Paul Eggert
Subject: getopt cleanup for coreutils
Date: Tue, 21 Sep 2004 15:30:49 -0700
User-agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux)

I installed the following change to coreutils to clean up getopt
usage, inspired by recent discussions on the Open Group mailing list.
Most of this has no externally-visible effect.  The exceptions
are that pathchk no longer accepts trailing options (since the whole
point of pathchk is to check weird names that might look like options)
and that the following commands reject unknown options:

  basename dirname factor hostname link nohup sync unlink yes

2004-09-21  Paul Eggert  <address@hidden>

        * NEWS: The following commands now reject unknown options:
        basename dirname factor hostname link nohup sync unlink yes
        Also, pathchk no longer accepts trailing options.

        * src/basename.c: Include <getopt.h>.
        * src/chroot.c: Likewise.
        * src/dirname.c: Likewise.
        * src/factor.c: Likewise.
        * src/hostid.c: Likewise.
        * src/hostname.c: Likewise.
        * src/nohup.c: Likewise.
        * src/pwd.c: Likewise.
        * src/setuidgid.c: Likewise.
        * src/sync.c: Likewise.

        * src/basename.c (main): Reject unknown options.
        * src/dirname.c (main): Likewise.
        * src/factor.c (main): Likewise.
        * src/hostid.c (main): Likewise.
        * src/hostname.c (main): Likewise.
        * src/link.c (main): Likewise.
        * src/nohup.c (main): Likewise.
        * src/pwd.c (main): Likewise.
        * src/setuidgid.c (main): Likewise.
        * src/sync.c (main): Likewise.
        * src/unlink.c (main): Likewise.
        * src/yes.c (main): Likewise.

        * src/cat.c (main): Remove unused "case 0".
        * src/chgrp.c (main): Likewise.
        * src/chmod.c (main): Likewise.
        * src/chown.c (main): Likewise.
        * src/comm.c (main): Likewise.
        * src/cp.c (main): Likewise.
        * src/csplit.c (main): Likewise.
        * src/cut.c (main): Likewise.
        * src/date.c (main): Likewise.
        * src/df.c (main): Likewise.
        * src/du.c (main): Likewise.
        * src/env.c (main): Likewise.
        * src/expand.c (main): Likewise.
        * src/fold.c (main): Likewise.
        * src/head.c (main): Likewise.
        * src/id.c (main): Likewise.
        * src/install.c (main): Likewise.
        * src/join.c (main): Likewise.
        * src/ln.c (main): Likewise.
        * src/ls.c (decode_switches): Likewise.
        * src/mkdir.c (main): Likewise.
        * src/mkfifo.c (main): Likewise.
        * src/mknode.c (main): Likewise.
        * src/mv.c (main): Likewise.
        * src/nl.c (main): Likewise.
        * src/paste.c (main): Likewise.
        * src/pinky.c (main): Likewise.
        * src/pr.c (main): Likewise.
        * src/ptx.c (main): Likewise.
        * src/readlink.c (main): Likewise.
        * src/rm.c (main): Likewise.
        * src/rmdir.c (main): Likewise.
        * src/seq.c (main): Likewise.
        * src/shred.c (main): Likewise.
        * src/split.c (main): Likewise.
        * src/sum.c (main): Likewise.
        * src/tac.c (main): Likewise.
        * src/tail.c (main): Likewise.
        * src/tee.c (main): Likewise.
        * src/touch.c (main): Likewise.
        * src/tr.c (main): Likewise.
        * src/tty.c (main): Likewise.
        * src/uname.c (main): Likewise.
        * src/unexpand.c (main): Likewise.
        * src/wc.c (main): Likewise.
        * src/who.c (main): Likewise.

        * src/chroot.c (main): Use getopt where it suffices, not getopt_long.
        * src/cksum.c (main): Likewise.
        * src/dd.c (main): Likewise.
        * src/logname.c (main): Likewise.
        * src/printenv.c (main): Likewise.
        * src/sleep.c (main): Likewise.
        * src/tsort.c (main): Likewise.
        * src/uptime.c (main): Likewise.
        * src/users.c (main): Likewise.
        * src/whoami.c (main): Likewise.

        * src/du.c (long_options): Standardize on NULL vs 0.
        * src/rm.c (long_opts): Likewise.

        * src/logname.c (long_options): Remove.
        * src/printenv.c (long_options): Likewise.
        * src/sleep.c (long_options): Likewise.
        * src/tsort.c (long_options): Likewise.
        * src/uptime.c (longopts): Likewise.
        * src/users.c (longopts): Likewise.
        * src/whoami.c (long_options): Likewise.

        * src/pathchk.c (longopts): Add --help, --version.
        (main): Use longopts rather than parse_long_options.
        * src/stty.c (longpts, main): Likewise.

        * src/pathchk.c (main): Don't reorder arguments, so that
        we can check weird file names.

        * src/readlink.c: Don't include "long-options.h".
        * src/sort.c: Likewise.
        * src/stty.c: Likewise.

        * src/split.c (verbose): Now bool, not int.
        (VERBOSE_OPTION): New enum.
        (longopts, main): Use it.

        * tests/factor/basic: Adjust to new wording in diagnostic
        that results from the above changes.

Index: NEWS
===================================================================
RCS file: /home/eggert/coreutils/cu/NEWS,v
retrieving revision 1.235
diff -p -u -r1.235 NEWS
--- NEWS        19 Sep 2004 16:53:00 -0000      1.235
+++ NEWS        21 Sep 2004 21:35:32 -0000
@@ -66,6 +66,9 @@ GNU coreutils NEWS                      
     It now consistently adjusts out-of-range nice values to the
     closest values in range; formerly it sometimes reported an error.
 
+  pathchk no longer accepts trailing options, e.g., "pathchk -p foo -b"
+  now treats -b as a file name to check, not as an invalid option.
+
   printf has several changes:
 
     It now uses 'intmax_t' (not 'long int') to format integers, so it
@@ -113,6 +116,13 @@ GNU coreutils NEWS                      
   tr no longer mishandles a second operand with leading "-".
 
   who now prints user names in full instead of truncating them after 8 bytes.
+
+  The following commands now reject unknown options instead of
+  accepting them as operands, so that users are properly warned that
+  options may be added later.  Formerly they accepted unknown options
+  as operands; e.g., "basename -a a" acted like "basename -- -a a".
+
+    basename dirname factor hostname link nohup sync unlink yes
 
 ** New features
 
Index: src/basename.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/basename.c,v
retrieving revision 1.57
diff -p -u -r1.57 basename.c
--- src/basename.c      21 Jun 2004 15:03:35 -0000      1.57
+++ src/basename.c      21 Sep 2004 19:04:24 -0000
@@ -26,6 +26,7 @@
    => functions.lis */
 
 #include <config.h>
+#include <getopt.h>
 #include <stdio.h>
 #include <sys/types.h>
 
@@ -102,31 +103,26 @@ main (int argc, char **argv)
 
   parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
                      usage, AUTHORS, (char const *) NULL);
-  /* The above handles --help and --version.
-     Since there is no other invocation of getopt, handle `--' here.  */
-  if (argc > 1 && STREQ (argv[1], "--"))
-    {
-      --argc;
-      ++argv;
-    }
+  if (getopt (argc, argv, "+") != -1)
+    usage (EXIT_FAILURE);
 
-  if (argc < 2)
+  if (argc < optind + 1)
     {
       error (0, 0, _("missing operand"));
       usage (EXIT_FAILURE);
     }
 
-  if (3 < argc)
+  if (optind + 2 < argc)
     {
-      error (0, 0, _("extra operand %s"), quote (argv[3]));
+      error (0, 0, _("extra operand %s"), quote (argv[optind + 2]));
       usage (EXIT_FAILURE);
     }
 
-  name = base_name (argv[1]);
+  name = base_name (argv[optind]);
   name[base_len (name)] = '\0';
 
-  if (argc == 3)
-    remove_suffix (name, argv[2]);
+  if (argc == optind + 2)
+    remove_suffix (name, argv[optind + 1]);
 
   puts (name);
 
Index: src/cat.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/cat.c,v
retrieving revision 1.96
diff -p -u -r1.96 cat.c
--- src/cat.c   29 Jul 2004 06:12:27 -0000      1.96
+++ src/cat.c   21 Sep 2004 20:51:00 -0000
@@ -581,9 +581,6 @@ main (int argc, char **argv)
     {
       switch (c)
        {
-       case 0:
-         break;
-
        case 'b':
          number = true;
          number_nonblank = true;
Index: src/chgrp.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/chgrp.c,v
retrieving revision 1.114
diff -p -u -r1.114 chgrp.c
--- src/chgrp.c 19 Aug 2004 20:02:46 -0000      1.114
+++ src/chgrp.c 21 Sep 2004 20:51:13 -0000
@@ -189,9 +189,6 @@ main (int argc, char **argv)
     {
       switch (optc)
        {
-       case 0:
-         break;
-
        case 'H': /* Traverse command-line symlinks-to-directories.  */
          bit_flags = FTS_COMFOLLOW | FTS_PHYSICAL;
          break;
Index: src/chmod.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/chmod.c,v
retrieving revision 1.105
diff -p -u -r1.105 chmod.c
--- src/chmod.c 30 Jul 2004 04:06:41 -0000      1.105
+++ src/chmod.c 21 Sep 2004 20:51:23 -0000
@@ -365,8 +365,6 @@ main (int argc, char **argv)
 
       switch (c)
        {
-       case 0:
-         break;
        case 'r':
        case 'w':
        case 'x':
Index: src/chown.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/chown.c,v
retrieving revision 1.119
diff -p -u -r1.119 chown.c
--- src/chown.c 19 Aug 2004 20:03:26 -0000      1.119
+++ src/chown.c 21 Sep 2004 20:51:34 -0000
@@ -192,9 +192,6 @@ main (int argc, char **argv)
     {
       switch (optc)
        {
-       case 0:
-         break;
-
        case 'H': /* Traverse command-line symlinks-to-directories.  */
          bit_flags = FTS_COMFOLLOW | FTS_PHYSICAL;
          break;
Index: src/chroot.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/chroot.c,v
retrieving revision 1.46
diff -p -u -r1.46 chroot.c
--- src/chroot.c        19 Sep 2004 01:07:46 -0000      1.46
+++ src/chroot.c        21 Sep 2004 21:01:15 -0000
@@ -18,6 +18,7 @@
 /* Written by Roland McGrath.  */
 
 #include <config.h>
+#include <getopt.h>
 #include <stdio.h>
 #include <sys/types.h>
 
@@ -75,8 +76,7 @@ main (int argc, char **argv)
 
   parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
                      usage, AUTHORS, (char const *) NULL);
-
-  if (getopt_long (argc, argv, "+", NULL, NULL) != -1)
+  if (getopt (argc, argv, "+") != -1)
     usage (EXIT_FAIL);
 
   if (argc <= optind)
Index: src/cksum.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/cksum.c,v
retrieving revision 1.71
diff -p -u -r1.71 cksum.c
--- src/cksum.c 19 Sep 2004 00:39:52 -0000      1.71
+++ src/cksum.c 20 Sep 2004 07:26:55 -0000
@@ -298,11 +298,10 @@ main (int argc, char **argv)
 
   parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE, VERSION,
                      usage, AUTHORS, (char const *) NULL);
+  if (getopt (argc, argv, "") != -1)
+    usage (EXIT_FAILURE);
 
   have_read_stdin = false;
-
-  if (getopt_long (argc, argv, "", NULL, NULL) != -1)
-    usage (EXIT_FAILURE);
 
   if (optind == argc)
     ok = cksum ("-", false);
Index: src/comm.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/comm.c,v
retrieving revision 1.76
diff -p -u -r1.76 comm.c
--- src/comm.c  30 Jul 2004 05:25:17 -0000      1.76
+++ src/comm.c  21 Sep 2004 20:51:46 -0000
@@ -262,9 +262,6 @@ main (int argc, char **argv)
   while ((c = getopt_long (argc, argv, "123", long_options, NULL)) != -1)
     switch (c)
       {
-      case 0:
-       break;
-
       case '1':
        only_file_1 = false;
        break;
Index: src/cp.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/cp.c,v
retrieving revision 1.202
diff -p -u -r1.202 cp.c
--- src/cp.c    13 Sep 2004 12:12:45 -0000      1.202
+++ src/cp.c    21 Sep 2004 20:52:23 -0000
@@ -823,9 +823,6 @@ main (int argc, char **argv)
     {
       switch (c)
        {
-       case 0:
-         break;
-
        case SPARSE_OPTION:
          x.sparse_mode = XARGMATCH ("--sparse", optarg,
                                     sparse_type_string, sparse_type);
Index: src/csplit.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/csplit.c,v
retrieving revision 1.130
diff -p -u -r1.130 csplit.c
--- src/csplit.c        9 Aug 2004 18:45:10 -0000       1.130
+++ src/csplit.c        21 Sep 2004 19:59:53 -0000
@@ -1338,9 +1338,6 @@ main (int argc, char **argv)
   while ((optc = getopt_long (argc, argv, "f:b:kn:sqz", longopts, NULL)) != -1)
     switch (optc)
       {
-      case 0:
-       break;
-
       case 'f':
        prefix = optarg;
        break;
Index: src/cut.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/cut.c,v
retrieving revision 1.114
diff -p -u -r1.114 cut.c
--- src/cut.c   3 Aug 2004 05:59:14 -0000       1.114
+++ src/cut.c   21 Sep 2004 20:00:15 -0000
@@ -755,9 +755,6 @@ main (int argc, char **argv)
     {
       switch (optc)
        {
-       case 0:
-         break;
-
        case 'b':
        case 'c':
          /* Build the byte list. */
Index: src/date.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/date.c,v
retrieving revision 1.138
diff -p -u -r1.138 date.c
--- src/date.c  3 Aug 2004 05:59:36 -0000       1.138
+++ src/date.c  21 Sep 2004 20:00:59 -0000
@@ -313,8 +313,6 @@ main (int argc, char **argv)
         != -1)
     switch (optc)
       {
-      case 0:
-       break;
       case 'd':
        datestr = optarg;
        break;
Index: src/dd.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/dd.c,v
retrieving revision 1.166
diff -p -u -r1.166 dd.c
--- src/dd.c    19 Sep 2004 01:46:09 -0000      1.166
+++ src/dd.c    20 Sep 2004 07:27:13 -0000
@@ -1360,7 +1360,7 @@ main (int argc, char **argv)
 
   parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE, VERSION,
                      usage, AUTHORS, (char const *) NULL);
-  if (getopt_long (argc, argv, "", NULL, NULL) != -1)
+  if (getopt (argc, argv, "") != -1)
     usage (EXIT_FAILURE);
 
   /* Don't close stdout on exit from here on.  */
Index: src/df.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/df.c,v
retrieving revision 1.161
diff -p -u -r1.161 df.c
--- src/df.c    2 Aug 2004 18:43:35 -0000       1.161
+++ src/df.c    21 Sep 2004 20:01:50 -0000
@@ -781,8 +781,6 @@ main (int argc, char **argv)
     {
       switch (c)
        {
-       case 0:                 /* Long option. */
-         break;
        case 'a':
          show_all_fs = true;
          break;
Index: src/dirname.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/dirname.c,v
retrieving revision 1.63
diff -p -u -r1.63 dirname.c
--- src/dirname.c       21 Jun 2004 15:03:35 -0000      1.63
+++ src/dirname.c       21 Sep 2004 20:54:41 -0000
@@ -18,6 +18,7 @@
 /* Written by David MacKenzie and Jim Meyering. */
 
 #include <config.h>
+#include <getopt.h>
 #include <stdio.h>
 #include <sys/types.h>
 
@@ -77,27 +78,22 @@ main (int argc, char **argv)
 
   parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
                      usage, AUTHORS, (char const *) NULL);
-  /* The above handles --help and --version.
-     Since there is no other invocation of getopt, handle `--' here.  */
-  if (argc > 1 && STREQ (argv[1], "--"))
-    {
-      --argc;
-      ++argv;
-    }
+  if (getopt (argc, argv, "+") != -1)
+    usage (EXIT_FAILURE);
 
-  if (argc < 2)
+  if (argc < optind + 1)
     {
       error (0, 0, _("missing operand"));
       usage (EXIT_FAILURE);
     }
 
-  if (2 < argc)
+  if (optind + 1 < argc)
     {
-      error (0, 0, _("extra operand %s"), quote (argv[2]));
+      error (0, 0, _("extra operand %s"), quote (argv[optind + 1]));
       usage (EXIT_FAILURE);
     }
 
-  result = argv[1];
+  result = argv[optind];
   len = dir_len (result);
 
   if (! len)
Index: src/du.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/du.c,v
retrieving revision 1.198
diff -p -u -r1.198 du.c
--- src/du.c    2 Aug 2004 19:43:38 -0000       1.198
+++ src/du.c    21 Sep 2004 20:03:53 -0000
@@ -132,16 +132,16 @@ static struct option const long_options[
 {
   {"all", no_argument, NULL, 'a'},
   {"apparent-size", no_argument, NULL, APPARENT_SIZE_OPTION},
-  {"block-size", required_argument, 0, 'B'},
+  {"block-size", required_argument, NULL, 'B'},
   {"bytes", no_argument, NULL, 'b'},
   {"count-links", no_argument, NULL, 'l'},
   {"dereference", no_argument, NULL, 'L'},
   {"dereference-args", no_argument, NULL, 'D'},
-  {"exclude", required_argument, 0, EXCLUDE_OPTION},
-  {"exclude-from", required_argument, 0, 'X'},
-  {"files0-from", required_argument, 0, FILES0_FROM_OPTION},
+  {"exclude", required_argument, NULL, EXCLUDE_OPTION},
+  {"exclude-from", required_argument, NULL, 'X'},
+  {"files0-from", required_argument, NULL, FILES0_FROM_OPTION},
   {"human-readable", no_argument, NULL, 'h'},
-  {"si", no_argument, 0, HUMAN_SI_OPTION},
+  {"si", no_argument, NULL, HUMAN_SI_OPTION},
   {"kilobytes", no_argument, NULL, 'k'}, /* long form is obsolescent */
   {"max-depth", required_argument, NULL, MAX_DEPTH_OPTION},
   {"null", no_argument, NULL, '0'},
@@ -562,9 +562,6 @@ main (int argc, char **argv)
     {
       switch (c)
        {
-       case 0:                 /* Long option. */
-         break;
-
 #if DU_DEBUG
        case 'd':
          fts_debug = true;
Index: src/env.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/env.c,v
retrieving revision 1.59
diff -p -u -r1.59 env.c
--- src/env.c   3 Aug 2004 06:00:08 -0000       1.59
+++ src/env.c   21 Sep 2004 20:04:05 -0000
@@ -154,8 +154,6 @@ main (register int argc, register char *
     {
       switch (optc)
        {
-       case 0:
-         break;
        case 'i':
          ignore_environment = true;
          break;
Index: src/expand.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/expand.c,v
retrieving revision 1.77
diff -p -u -r1.77 expand.c
--- src/expand.c        24 Aug 2004 07:38:04 -0000      1.77
+++ src/expand.c        21 Sep 2004 20:52:37 -0000
@@ -398,9 +398,6 @@ main (int argc, char **argv)
     {
       switch (c)
        {
-       case 0:
-         break;
-
        case '?':
          usage (EXIT_FAILURE);
        case 'i':
Index: src/factor.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/factor.c,v
retrieving revision 1.67
diff -p -u -r1.67 factor.c
--- src/factor.c        3 Aug 2004 06:15:49 -0000       1.67
+++ src/factor.c        21 Sep 2004 20:55:08 -0000
@@ -19,6 +19,7 @@
    Adapted for GNU, fixed to factor UINT_MAX by Jim Meyering.  */
 
 #include <config.h>
+#include <getopt.h>
 #include <stdio.h>
 #include <sys/types.h>
 
@@ -201,20 +202,15 @@ main (int argc, char **argv)
 
   parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
                      usage, AUTHORS, (char const *) NULL);
-  /* The above handles --help and --version.
-     Since there is no other invocation of getopt, handle `--' here.  */
-  if (argc > 1 && STREQ (argv[1], "--"))
-    {
-      --argc;
-      ++argv;
-    }
+  if (getopt (argc, argv, "") != -1)
+    usage (EXIT_FAILURE);
 
-  if (argc == 1)
+  if (argc <= optind)
     ok = do_stdin ();
   else
     {
       int i;
-      for (i = 1; i < argc; i++)
+      for (i = optind; i < argc; i++)
        if (! print_factors (argv[i]))
          usage (EXIT_FAILURE);
       ok = true;
Index: src/fold.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/fold.c,v
retrieving revision 1.79
diff -p -u -r1.79 fold.c
--- src/fold.c  3 Aug 2004 06:16:05 -0000       1.79
+++ src/fold.c  21 Sep 2004 20:06:08 -0000
@@ -284,9 +284,6 @@ main (int argc, char **argv)
     {
       switch (optc)
        {
-       case 0:
-         break;
-
        case 'b':               /* Count bytes rather than columns. */
          count_bytes = true;
          break;
Index: src/head.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/head.c,v
retrieving revision 1.95
diff -p -u -r1.95 head.c
--- src/head.c  3 Aug 2004 06:16:31 -0000       1.95
+++ src/head.c  21 Sep 2004 20:06:33 -0000
@@ -1027,9 +1027,6 @@ main (int argc, char **argv)
     {
       switch (c)
        {
-       case 0:
-         break;
-
        case PRESUME_INPUT_PIPE_OPTION:
          presume_input_pipe = true;
          break;
Index: src/hostid.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/hostid.c,v
retrieving revision 1.27
diff -p -u -r1.27 hostid.c
--- src/hostid.c        21 Jun 2004 15:03:35 -0000      1.27
+++ src/hostid.c        21 Sep 2004 20:55:42 -0000
@@ -20,6 +20,7 @@
 /* Written by Jim Meyering.  */
 
 #include <config.h>
+#include <getopt.h>
 #include <stdio.h>
 #include <sys/types.h>
 
@@ -73,18 +74,12 @@ main (int argc, char **argv)
 
   parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
                      usage, AUTHORS, (char const *) NULL);
+  if (getopt (argc, argv, "") != -1)
+    usage (EXIT_FAILURE);
 
-  /* The above handles --help and --version.
-     Since there is no other invocation of getopt, handle `--' here.  */
-  if (1 < argc && STREQ (argv[1], "--"))
+  if (optind < argc)
     {
-      --argc;
-      ++argv;
-    }
-
-  if (argc > 1)
-    {
-      error (0, 0, _("extra operand %s"), quote (argv[1]));
+      error (0, 0, _("extra operand %s"), quote (argv[optind]));
       usage (EXIT_FAILURE);
     }
 
Index: src/hostname.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/hostname.c,v
retrieving revision 1.53
diff -p -u -r1.53 hostname.c
--- src/hostname.c      2 Aug 2004 21:52:28 -0000       1.53
+++ src/hostname.c      21 Sep 2004 20:56:00 -0000
@@ -18,6 +18,7 @@
 /* Written by Jim Meyering.  */
 
 #include <config.h>
+#include <getopt.h>
 #include <stdio.h>
 #include <sys/types.h>
 
@@ -90,39 +91,33 @@ main (int argc, char **argv)
 
   parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
                      usage, AUTHORS, (char const *) NULL);
+  if (getopt (argc, argv, "") != -1)
+    usage (EXIT_FAILURE);
 
-  /* The above handles --help and --version.
-     Since there is no other invocation of getopt, handle `--' here.  */
-  if (1 < argc && STREQ (argv[1], "--"))
+  if (argc == optind + 1)
     {
-      --argc;
-      ++argv;
-    }
-
 #ifdef HAVE_SETHOSTNAME
-  if (argc == 2)
-    {
-      /* Set hostname to argv[1].  */
-      if (sethostname (argv[1], strlen (argv[1])) != 0)
-       error (EXIT_FAILURE, errno, _("cannot set hostname to `%s'"), argv[1]);
-      exit (EXIT_SUCCESS);
-    }
+      /* Set hostname to operand.  */
+      char const *name = argv[optind];
+      if (sethostname (name, strlen (name)) != 0)
+       error (EXIT_FAILURE, errno, _("cannot set name to `%s'"), name);
 #else
-  if (argc == 2)
-    error (EXIT_FAILURE, 0,
-          _("cannot set hostname; this system lacks the functionality"));
+      error (EXIT_FAILURE, 0,
+            _("cannot set hostname; this system lacks the functionality"));
 #endif
+    }
 
-  if (argc <= 1)
+  if (argc <= optind)
     {
       hostname = xgethostname ();
       if (hostname == NULL)
        error (EXIT_FAILURE, errno, _("cannot determine hostname"));
       printf ("%s\n", hostname);
     }
-  else
+
+  if (optind + 1 < argc)
     {
-      error (0, 0, _("extra operand %s"), quote (argv[2]));
+      error (0, 0, _("extra operand %s"), quote (argv[optind + 1]));
       usage (EXIT_FAILURE);
     }
 
Index: src/id.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/id.c,v
retrieving revision 1.79
diff -p -u -r1.79 id.c
--- src/id.c    13 Sep 2004 10:43:21 -0000      1.79
+++ src/id.c    21 Sep 2004 20:07:16 -0000
@@ -132,8 +132,6 @@ main (int argc, char **argv)
     {
       switch (optc)
        {
-       case 0:
-         break;
        case 'a':
          /* Ignore -a, for compatibility with SVR4.  */
          break;
Index: src/install.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/install.c,v
retrieving revision 1.169
diff -p -u -r1.169 install.c
--- src/install.c       13 Sep 2004 12:13:04 -0000      1.169
+++ src/install.c       21 Sep 2004 20:07:50 -0000
@@ -232,9 +232,6 @@ main (int argc, char **argv)
     {
       switch (optc)
        {
-       case 0:
-         break;
-
        case 'V':  /* FIXME: this is deprecated.  Remove it in 2001.  */
          error (0, 0,
                 _("warning: --version-control (-V) is obsolete;  support for\
Index: src/join.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/join.c,v
retrieving revision 1.130
diff -p -u -r1.130 join.c
--- src/join.c  21 Jun 2004 15:02:14 -0000      1.130
+++ src/join.c  21 Sep 2004 20:08:34 -0000
@@ -768,9 +768,6 @@ main (int argc, char **argv)
 
       switch (optc)
        {
-       case 0:
-         break;
-
        case 'v':
            print_pairables = false;
            /* Fall through.  */
Index: src/link.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/link.c,v
retrieving revision 1.12
diff -p -u -r1.12 link.c
--- src/link.c  21 Jun 2004 15:03:35 -0000      1.12
+++ src/link.c  20 Sep 2004 07:28:29 -0000
@@ -74,33 +74,27 @@ main (int argc, char **argv)
 
   parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
                      usage, AUTHORS, (char const *) NULL);
+  if (getopt (argc, argv, "") != -1)
+    usage (EXIT_FAILURE);
 
-  /* The above handles --help and --version.
-     Since there is no other invocation of getopt, handle `--' here.  */
-  if (1 < argc && STREQ (argv[1], "--"))
+  if (argc < optind + 2)
     {
-      --argc;
-      ++argv;
-    }
-
-  if (argc < 3)
-    {
-      if (argc < 2)
+      if (argc < optind + 1)
        error (0, 0, _("missing operand"));
       else
-       error (0, 0, _("missing operand after %s"), quote (argv[argc - 1]));
+       error (0, 0, _("missing operand after %s"), quote (argv[optind]));
       usage (EXIT_FAILURE);
     }
 
-  if (3 < argc)
+  if (optind + 2 < argc)
     {
-      error (0, 0, _("extra operand %s"), quote (argv[3]));
+      error (0, 0, _("extra operand %s"), quote (argv[optind + 2]));
       usage (EXIT_FAILURE);
     }
 
-  if (link (argv[1], argv[2]) != 0)
+  if (link (argv[optind], argv[optind + 1]) != 0)
     error (EXIT_FAILURE, errno, _("cannot create link %s to %s"),
-          quote_n (0, argv[2]), quote_n (1, argv[1]));
+          quote_n (0, argv[optind + 1]), quote_n (1, argv[optind]));
 
   exit (EXIT_SUCCESS);
 }
Index: src/ln.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/ln.c,v
retrieving revision 1.144
diff -p -u -r1.144 ln.c
--- src/ln.c    13 Sep 2004 12:15:02 -0000      1.144
+++ src/ln.c    21 Sep 2004 20:09:26 -0000
@@ -431,9 +431,6 @@ main (int argc, char **argv)
     {
       switch (c)
        {
-       case 0:                 /* Long-named option. */
-         break;
-
        case 'V':  /* FIXME: this is deprecated.  Remove it in 2001.  */
          error (0, 0,
                 _("warning: --version-control (-V) is obsolete;  support for\
Index: src/logname.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/logname.c,v
retrieving revision 1.54
diff -p -u -r1.54 logname.c
--- src/logname.c       21 Jun 2004 15:03:35 -0000      1.54
+++ src/logname.c       20 Sep 2004 07:28:45 -0000
@@ -33,11 +33,6 @@
 /* The name this program was run with. */
 char *program_name;
 
-static struct option const long_options[] =
-{
-  {0, 0, 0, 0}
-};
-
 void
 usage (int status)
 {
@@ -74,18 +69,8 @@ main (int argc, char **argv)
 
   parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
                      usage, AUTHORS, (char const *) NULL);
-
-  while ((c = getopt_long (argc, argv, "", long_options, NULL)) != -1)
-    {
-      switch (c)
-       {
-       case 0:
-         break;
-
-       default:
-         usage (EXIT_FAILURE);
-       }
-    }
+  if (getopt (argc, argv, "") != -1)
+    usage (EXIT_FAILURE);
 
   if (optind < argc)
     {
Index: src/ls.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/ls.c,v
retrieving revision 1.359
diff -p -u -r1.359 ls.c
--- src/ls.c    3 Aug 2004 06:44:08 -0000       1.359
+++ src/ls.c    21 Sep 2004 20:10:00 -0000
@@ -1431,9 +1431,6 @@ decode_switches (int argc, char **argv)
     {
       switch (c)
        {
-       case 0:
-         break;
-
        case 'a':
          all_files = true;
          really_all_files = true;
Index: src/mkdir.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/mkdir.c,v
retrieving revision 1.90
diff -p -u -r1.90 mkdir.c
--- src/mkdir.c 30 Jul 2004 20:29:29 -0000      1.90
+++ src/mkdir.c 21 Sep 2004 20:10:18 -0000
@@ -102,8 +102,6 @@ main (int argc, char **argv)
     {
       switch (optc)
        {
-       case 0:                 /* Long option. */
-         break;
        case 'p':
          create_parents = true;
          break;
Index: src/mkfifo.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/mkfifo.c,v
retrieving revision 1.72
diff -p -u -r1.72 mkfifo.c
--- src/mkfifo.c        30 Jul 2004 08:00:40 -0000      1.72
+++ src/mkfifo.c        21 Sep 2004 20:10:35 -0000
@@ -97,8 +97,6 @@ main (int argc, char **argv)
     {
       switch (optc)
        {
-       case 0:
-         break;
        case 'm':
          specified_mode = optarg;
          break;
Index: src/mknod.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/mknod.c,v
retrieving revision 1.83
diff -p -u -r1.83 mknod.c
--- src/mknod.c 26 Jun 2004 13:12:42 -0000      1.83
+++ src/mknod.c 21 Sep 2004 20:10:54 -0000
@@ -108,8 +108,6 @@ main (int argc, char **argv)
     {
       switch (optc)
        {
-       case 0:
-         break;
        case 'm':
          specified_mode = optarg;
          break;
Index: src/mv.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/mv.c,v
retrieving revision 1.161
diff -p -u -r1.161 mv.c
--- src/mv.c    13 Sep 2004 12:15:18 -0000      1.161
+++ src/mv.c    21 Sep 2004 20:11:10 -0000
@@ -384,9 +384,6 @@ main (int argc, char **argv)
     {
       switch (c)
        {
-       case 0:
-         break;
-
        case 'V':  /* FIXME: this is deprecated.  Remove it in 2001.  */
          error (0, 0,
                 _("warning: --version-control (-V) is obsolete;  support for\
Index: src/nl.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/nl.c,v
retrieving revision 1.81
diff -p -u -r1.81 nl.c
--- src/nl.c    3 Aug 2004 06:59:28 -0000       1.81
+++ src/nl.c    21 Sep 2004 20:11:35 -0000
@@ -460,9 +460,6 @@ main (int argc, char **argv)
     {
       switch (c)
        {
-       case 0:
-         break;
-
        case 'h':
          if (! build_type_arg (&header_type, &header_regex))
            {
Index: src/nohup.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/nohup.c,v
retrieving revision 1.17
diff -p -u -r1.17 nohup.c
--- src/nohup.c 3 Aug 2004 15:29:48 -0000       1.17
+++ src/nohup.c 21 Sep 2004 20:56:52 -0000
@@ -18,6 +18,7 @@
 /* Written by Jim Meyering  */
 
 #include <config.h>
+#include <getopt.h>
 #include <stdio.h>
 #include <sys/types.h>
 #include <signal.h>
@@ -86,16 +87,10 @@ main (int argc, char **argv)
 
   parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
                      usage, AUTHORS, (char const *) NULL);
+  if (getopt (argc, argv, "+") != -1)
+    usage (EXIT_FAILURE);
 
-  /* The above handles --help and --version.
-     Now, handle `--'.  */
-  if (argc > 1 && STREQ (argv[1], "--"))
-    {
-      --argc;
-      ++argv;
-    }
-
-  if (argc <= 1)
+  if (argc <= optind)
     {
       error (0, 0, _("missing operand"));
       usage (NOHUP_FAILURE);
@@ -168,7 +163,7 @@ main (int argc, char **argv)
   {
     int exit_status;
     int saved_errno;
-    char **cmd = argv + 1;
+    char **cmd = argv + optind;
 
     execvp (*cmd, cmd);
     exit_status = (errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE);
Index: src/paste.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/paste.c,v
retrieving revision 1.76
diff -p -u -r1.76 paste.c
--- src/paste.c 3 Sep 2004 01:35:33 -0000       1.76
+++ src/paste.c 21 Sep 2004 20:12:22 -0000
@@ -460,9 +460,6 @@ main (int argc, char **argv)
     {
       switch (optc)
        {
-       case 0:
-         break;
-
        case 'd':
          /* Delimiter character(s). */
          if (optarg[0] == '\0')
Index: src/pathchk.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/pathchk.c,v
retrieving revision 1.80
diff -p -u -r1.80 pathchk.c
--- src/pathchk.c       3 Aug 2004 15:30:39 -0000       1.80
+++ src/pathchk.c       21 Sep 2004 21:01:51 -0000
@@ -46,7 +46,6 @@
 #include "system.h"
 #include "error.h"
 #include "euidaccess.h"
-#include "long-options.h"
 
 /* The official name of this program (e.g., no `g' prefix).  */
 #define PROGRAM_NAME "pathchk"
@@ -108,6 +107,8 @@ char *program_name;
 static struct option const longopts[] =
 {
   {"portability", no_argument, NULL, 'p'},
+  {GETOPT_HELP_OPTION_DECL},
+  {GETOPT_VERSION_OPTION_DECL},
   {NULL, 0, NULL, 0}
 };
 
@@ -166,19 +167,17 @@ main (int argc, char **argv)
 
   atexit (close_stdout);
 
-  parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
-                     usage, AUTHORS, (char const *) NULL);
-
-  while ((optc = getopt_long (argc, argv, "p", longopts, NULL)) != -1)
+  while ((optc = getopt_long (argc, argv, "+p", longopts, NULL)) != -1)
     {
       switch (optc)
        {
-       case 0:
-         break;
-
        case 'p':
          check_portability = true;
          break;
+
+       case_GETOPT_HELP_CHAR;
+
+       case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
 
        default:
          usage (EXIT_FAILURE);
Index: src/pinky.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/pinky.c,v
retrieving revision 1.41
diff -p -u -r1.41 pinky.c
--- src/pinky.c 2 Aug 2004 20:55:44 -0000       1.41
+++ src/pinky.c 21 Sep 2004 20:15:28 -0000
@@ -558,9 +558,6 @@ main (int argc, char **argv)
     {
       switch (optc)
        {
-       case 0:
-         break;
-
        case 's':
          do_short_format = true;
          break;
Index: src/pr.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/pr.c,v
retrieving revision 1.120
diff -p -u -r1.120 pr.c
--- src/pr.c    3 Aug 2004 18:26:32 -0000       1.120
+++ src/pr.c    21 Sep 2004 20:53:24 -0000
@@ -888,9 +888,6 @@ main (int argc, char **argv)
 
       switch (c)
        {
-       case 0:                 /* getopt long option */
-         break;
-
        case 1:                 /* Non-option argument. */
          if (*optarg == '+')
            {
Index: src/printenv.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/printenv.c,v
retrieving revision 1.51
diff -p -u -r1.51 printenv.c
--- src/printenv.c      3 Aug 2004 15:31:02 -0000       1.51
+++ src/printenv.c      21 Sep 2004 20:16:15 -0000
@@ -48,11 +48,6 @@ enum { PRINTENV_FAILURE = 2 };
 /* The name this program was run with. */
 char *program_name;
 
-static struct option const long_options[] =
-{
-  {0, 0, 0, 0}
-};
-
 extern char **environ;
 
 void
@@ -97,18 +92,8 @@ main (int argc, char **argv)
 
   parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
                      usage, AUTHORS, (char const *) NULL);
-
-  while ((c = getopt_long (argc, argv, "", long_options, NULL)) != -1)
-    {
-      switch (c)
-       {
-       case 0:
-         break;
-
-       default:
-         usage (PRINTENV_FAILURE);
-       }
-    }
+  if (getopt (argc, argv, "+") != -1)
+    usage (PRINTENV_FAILURE);
 
   if (optind >= argc)
     {
Index: src/ptx.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/ptx.c,v
retrieving revision 1.44
diff -p -u -r1.44 ptx.c
--- src/ptx.c   3 Aug 2004 19:15:59 -0000       1.44
+++ src/ptx.c   21 Sep 2004 20:34:54 -0000
@@ -1985,9 +1985,6 @@ main (int argc, char **argv)
        default:
          usage (EXIT_FAILURE);
 
-       case 0:
-         break;
-
        case 'C':
          fputs (_("\
 This program is free software; you can redistribute it and/or modify\n\
Index: src/pwd.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/pwd.c,v
retrieving revision 1.47
diff -p -u -r1.47 pwd.c
--- src/pwd.c   17 Jun 2004 13:09:47 -0000      1.47
+++ src/pwd.c   21 Sep 2004 20:57:55 -0000
@@ -16,6 +16,7 @@
    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 
 #include <config.h>
+#include <getopt.h>
 #include <stdio.h>
 #include <sys/types.h>
 
@@ -305,16 +306,10 @@ main (int argc, char **argv)
 
   parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
                      usage, AUTHORS, (char const *) NULL);
+  if (getopt (argc, argv, "") != -1)
+    usage (EXIT_FAILURE);
 
-  /* The above handles --help and --version.
-     Since there is no other invocation of getopt, handle `--' here.  */
-  if (1 < argc && STREQ (argv[1], "--"))
-    {
-      --argc;
-      ++argv;
-    }
-
-  if (1 < argc)
+  if (optind < argc)
     error (0, 0, _("ignoring non-option arguments"));
 
   wd = xgetcwd ();
Index: src/readlink.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/readlink.c,v
retrieving revision 1.14
diff -p -u -r1.14 readlink.c
--- src/readlink.c      9 Aug 2004 23:40:49 -0000       1.14
+++ src/readlink.c      21 Sep 2004 21:02:10 -0000
@@ -26,7 +26,6 @@
 #include "canonicalize.h"
 #include "error.h"
 #include "xreadlink.h"
-#include "long-options.h"
 #include "quote.h"
 
 /* The official name of this program (e.g., no `g' prefix).  */
@@ -116,8 +115,6 @@ main (int argc, char *const argv[])
     {
       switch (optc)
        {
-       case 0:
-         break;
        case 'e':
          can_mode = CAN_EXISTING;
          break;
Index: src/rm.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/rm.c,v
retrieving revision 1.130
diff -p -u -r1.130 rm.c
--- src/rm.c    30 Jul 2004 07:59:19 -0000      1.130
+++ src/rm.c    21 Sep 2004 20:35:43 -0000
@@ -79,8 +79,8 @@ static struct option const long_opts[] =
   {"force", no_argument, NULL, 'f'},
   {"interactive", no_argument, NULL, 'i'},
 
-  {"no-preserve-root", no_argument, 0, NO_PRESERVE_ROOT},
-  {"preserve-root", no_argument, 0, PRESERVE_ROOT},
+  {"no-preserve-root", no_argument, NULL, NO_PRESERVE_ROOT},
+  {"preserve-root", no_argument, NULL, PRESERVE_ROOT},
 
   /* This is solely for testing.  Do not document.  */
   /* It is relatively difficult to ensure that there is a tty on stdin.
@@ -179,9 +179,6 @@ main (int argc, char **argv)
     {
       switch (c)
        {
-       case 0:         /* Long option.  */
-         break;
-
        case 'd':
          x.unlink_dirs = true;
          break;
Index: src/rmdir.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/rmdir.c,v
retrieving revision 1.77
diff -p -u -r1.77 rmdir.c
--- src/rmdir.c 3 Aug 2004 20:05:59 -0000       1.77
+++ src/rmdir.c 21 Sep 2004 20:35:52 -0000
@@ -185,8 +185,6 @@ main (int argc, char **argv)
     {
       switch (optc)
        {
-       case 0:                 /* Long option. */
-         break;
        case 'p':
          empty_paths = true;
          break;
Index: src/seq.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/seq.c,v
retrieving revision 1.82
diff -p -u -r1.82 seq.c
--- src/seq.c   2 Aug 2004 22:19:01 -0000       1.82
+++ src/seq.c   21 Sep 2004 20:36:03 -0000
@@ -346,9 +346,6 @@ main (int argc, char **argv)
 
       switch (optc)
        {
-       case 0:
-         break;
-
        case 'f':
          format_str = optarg;
          break;
Index: src/setuidgid.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/setuidgid.c,v
retrieving revision 1.11
diff -p -u -r1.11 setuidgid.c
--- src/setuidgid.c     3 Aug 2004 06:27:33 -0000       1.11
+++ src/setuidgid.c     21 Sep 2004 20:58:18 -0000
@@ -18,6 +18,7 @@
 /* Written by Jim Meyering  */
 
 #include <config.h>
+#include <getopt.h>
 #include <stdio.h>
 #include <sys/types.h>
 #include <pwd.h>
@@ -85,25 +86,19 @@ main (int argc, char **argv)
 
   parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
                      usage, AUTHORS, (char const *) NULL);
+  if (getopt (argc, argv, "+") != -1)
+    usage (SETUIDGID_FAILURE);
 
-  /* The above handles --help and --version.
-     Since there is no other invocation of getopt, handle `--' here.  */
-  if (argc > 1 && STREQ (argv[1], "--"))
+  if (argc <= optind + 1)
     {
-      --argc;
-      ++argv;
-    }
-
-  if (argc <= 2)
-    {
-      if (argc < 2)
+      if (argc < optind + 1)
        error (0, 0, _("missing operand"));
       else
-       error (0, 0, _("missing operand after %s"), quote (argv[argc - 1]));
+       error (0, 0, _("missing operand after %s"), quote (argv[optind]));
       usage (SETUIDGID_FAILURE);
     }
 
-  user_id = argv[1];
+  user_id = argv[optind];
   pwd = getpwnam (user_id);
   if (pwd == NULL)
     error (SETUIDGID_FAILURE, errno,
@@ -121,7 +116,7 @@ main (int argc, char **argv)
           _("cannot set user-ID to %lu"), (unsigned long int) pwd->pw_uid);
 
   {
-    char **cmd = argv + 2;
+    char **cmd = argv + optind + 1;
     int exit_status;
     execvp (*cmd, cmd);
     exit_status = (errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE);
Index: src/shred.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/shred.c,v
retrieving revision 1.103
diff -p -u -r1.103 shred.c
--- src/shred.c 10 Aug 2004 22:07:52 -0000      1.103
+++ src/shred.c 21 Sep 2004 20:37:00 -0000
@@ -1603,9 +1603,6 @@ main (int argc, char **argv)
     {
       switch (c)
        {
-       case 0:
-         break;
-
        case 'f':
          flags.force = true;
          break;
Index: src/sleep.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/sleep.c,v
retrieving revision 1.86
diff -p -u -r1.86 sleep.c
--- src/sleep.c 2 Aug 2004 22:19:27 -0000       1.86
+++ src/sleep.c 21 Sep 2004 19:52:27 -0000
@@ -36,11 +36,6 @@
 /* The name by which this program was run. */
 char *program_name;
 
-static struct option const long_options[] =
-{
-  {0, 0, 0, 0}
-};
-
 void
 usage (int status)
 {
@@ -119,18 +114,8 @@ main (int argc, char **argv)
 
   parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
                      usage, AUTHORS, (char const *) NULL);
-
-  while ((c = getopt_long (argc, argv, "", long_options, NULL)) != -1)
-    {
-      switch (c)
-       {
-       case 0:
-         break;
-
-       default:
-         usage (EXIT_FAILURE);
-       }
-    }
+  if (getopt (argc, argv, "") != -1)
+    usage (EXIT_FAILURE);
 
   if (argc == 1)
     {
Index: src/sort.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/sort.c,v
retrieving revision 1.292
diff -p -u -r1.292 sort.c
--- src/sort.c  7 Sep 2004 05:09:24 -0000       1.292
+++ src/sort.c  21 Sep 2004 21:02:23 -0000
@@ -31,7 +31,6 @@
 #include "error.h"
 #include "hard-locale.h"
 #include "inttostr.h"
-#include "long-options.h"
 #include "physmem.h"
 #include "posixver.h"
 #include "quote.h"
Index: src/split.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/split.c,v
retrieving revision 1.100
diff -p -u -r1.100 split.c
--- src/split.c 3 Aug 2004 19:08:01 -0000       1.100
+++ src/split.c 21 Sep 2004 20:41:03 -0000
@@ -74,9 +74,16 @@ static int input_desc;
 /* Descriptor on which output file is open.  */
 static int output_desc;
 
-/* If nonzero, print a diagnostic on standard error just before each
+/* If true, print a diagnostic on standard error just before each
    output file is opened. */
-static int verbose;
+static bool verbose;
+
+/* For long options that have no equivalent short option, use a
+   non-character as a pseudo short option, starting with CHAR_MAX + 1.  */
+enum
+{
+  VERBOSE_OPTION = CHAR_MAX + 1
+};
 
 static struct option const longopts[] =
 {
@@ -85,7 +92,7 @@ static struct option const longopts[] =
   {"line-bytes", required_argument, NULL, 'C'},
   {"suffix-length", required_argument, NULL, 'a'},
   {"numeric-suffixes", no_argument, NULL, 'd'},
-  {"verbose", no_argument, &verbose, 1},
+  {"verbose", no_argument, NULL, VERBOSE_OPTION},
   {GETOPT_HELP_OPTION_DECL},
   {GETOPT_VERSION_OPTION_DECL},
   {NULL, 0, NULL, 0}
@@ -404,9 +411,6 @@ main (int argc, char **argv)
 
       switch (c)
        {
-       case 0:
-         break;
-
        case 'a':
          {
            unsigned long tmp;
@@ -489,6 +493,10 @@ main (int argc, char **argv)
 
        case 'd':
          suffix_alphabet = "0123456789";
+         break;
+
+       case VERBOSE_OPTION:
+         verbose = true;
          break;
 
        case_GETOPT_HELP_CHAR;
Index: src/stty.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/stty.c,v
retrieving revision 1.127
diff -p -u -r1.127 stty.c
--- src/stty.c  5 Sep 2004 07:27:51 -0000       1.127
+++ src/stty.c  21 Sep 2004 20:58:56 -0000
@@ -55,7 +55,6 @@
 #include <stdarg.h>
 
 #include "system.h"
-#include "long-options.h"
 #include "error.h"
 #include "xstrtol.h"
 
@@ -440,6 +439,8 @@ static struct option longopts[] =
   {"all", no_argument, NULL, 'a'},
   {"save", no_argument, NULL, 'g'},
   {"file", required_argument, NULL, 'F'},
+  {GETOPT_HELP_OPTION_DECL},
+  {GETOPT_VERSION_OPTION_DECL},
   {NULL, 0, NULL, 0}
 };
 
@@ -738,9 +739,6 @@ main (int argc, char **argv)
 
   atexit (close_stdout);
 
-  parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
-                     usage, AUTHORS, (char const *) NULL);
-
   output_type = changed;
   verbose_output = false;
   recoverable_output = false;
@@ -776,6 +774,10 @@ main (int argc, char **argv)
            error (EXIT_FAILURE, 0, _("only one device may be specified"));
          file_name = optarg;
          break;
+
+       case_GETOPT_HELP_CHAR;
+
+       case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
 
        default:
          noargs = false;
Index: src/sum.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/sum.c,v
retrieving revision 1.74
diff -p -u -r1.74 sum.c
--- src/sum.c   3 Aug 2004 20:06:17 -0000       1.74
+++ src/sum.c   21 Sep 2004 20:42:12 -0000
@@ -236,9 +236,6 @@ main (int argc, char **argv)
     {
       switch (optc)
        {
-       case 0:
-         break;
-
        case 'r':               /* For SysV compatibility. */
          sum_func = bsd_sum_file;
          break;
Index: src/sync.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/sync.c,v
retrieving revision 1.47
diff -p -u -r1.47 sync.c
--- src/sync.c  17 Jun 2004 13:09:27 -0000      1.47
+++ src/sync.c  21 Sep 2004 20:59:18 -0000
@@ -18,6 +18,7 @@
 /* Written by Jim Meyering */
 
 #include <config.h>
+#include <getopt.h>
 #include <stdio.h>
 #include <sys/types.h>
 
@@ -66,16 +67,10 @@ main (int argc, char **argv)
 
   parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE, VERSION,
                      usage, AUTHORS, (char const *) NULL);
+  if (getopt (argc, argv, "") != -1)
+    usage (EXIT_FAILURE);
 
-  /* The above handles --help and --version.
-     Since there is no other invocation of getopt, handle `--' here.  */
-  if (1 < argc && STREQ (argv[1], "--"))
-    {
-      --argc;
-      ++argv;
-    }
-
-  if (1 < argc)
+  if (optind < argc)
     error (0, 0, _("ignoring all arguments"));
 
   sync ();
Index: src/tac.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/tac.c,v
retrieving revision 1.110
diff -p -u -r1.110 tac.c
--- src/tac.c   3 Aug 2004 22:40:28 -0000       1.110
+++ src/tac.c   21 Sep 2004 20:44:15 -0000
@@ -620,8 +620,6 @@ main (int argc, char **argv)
     {
       switch (optc)
        {
-       case 0:
-         break;
        case 'b':
          separator_ends_record = false;
          break;
Index: src/tail.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/tail.c,v
retrieving revision 1.227
diff -p -u -r1.227 tail.c
--- src/tail.c  10 Sep 2004 20:54:08 -0000      1.227
+++ src/tail.c  21 Sep 2004 20:44:28 -0000
@@ -1478,9 +1478,6 @@ parse_options (int argc, char **argv,
     {
       switch (c)
        {
-       case 0:
-         break;
-
        case 'F':
          forever = true;
          follow_mode = Follow_name;
Index: src/tee.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/tee.c,v
retrieving revision 1.75
diff -p -u -r1.75 tee.c
--- src/tee.c   10 Aug 2004 22:08:58 -0000      1.75
+++ src/tee.c   21 Sep 2004 20:44:38 -0000
@@ -98,9 +98,6 @@ main (int argc, char **argv)
     {
       switch (optc)
        {
-       case 0:
-         break;
-
        case 'a':
          append = true;
          break;
Index: src/touch.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/touch.c,v
retrieving revision 1.124
diff -p -u -r1.124 touch.c
--- src/touch.c 7 Sep 2004 06:21:46 -0000       1.124
+++ src/touch.c 21 Sep 2004 20:45:10 -0000
@@ -282,9 +282,6 @@ main (int argc, char **argv)
     {
       switch (c)
        {
-       case 0:
-         break;
-
        case 'a':
          change_times |= CH_ATIME;
          break;
Index: src/tr.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/tr.c,v
retrieving revision 1.138
diff -p -u -r1.138 tr.c
--- src/tr.c    9 Sep 2004 00:27:56 -0000       1.138
+++ src/tr.c    21 Sep 2004 20:45:22 -0000
@@ -1685,9 +1685,6 @@ main (int argc, char **argv)
     {
       switch (c)
        {
-       case 0:
-         break;
-
        case 'c':
        case 'C':
          complement = true;
Index: src/tsort.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/tsort.c,v
retrieving revision 1.42
diff -p -u -r1.42 tsort.c
--- src/tsort.c 3 Aug 2004 23:13:20 -0000       1.42
+++ src/tsort.c 20 Sep 2004 07:30:53 -0000
@@ -77,11 +77,6 @@ static struct item *loop = NULL;
 
 /* The number of strings to sort.  */
 static size_t n_strings = 0;
-
-static struct option const long_options[] =
-{
-  { NULL, 0, NULL, 0}
-};
 
 void
 usage (int status)
@@ -558,15 +553,8 @@ main (int argc, char **argv)
 
   parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE, VERSION,
                      usage, AUTHORS, (char const *) NULL);
-
-  while ((opt = getopt_long (argc, argv, "", long_options, NULL)) != -1)
-    switch (opt)
-      {
-      case 0:                  /* long option */
-       break;
-      default:
-       usage (EXIT_FAILURE);
-      }
+  if (getopt (argc, argv, "") != -1)
+    usage (EXIT_FAILURE);
 
   have_read_stdin = false;
 
Index: src/tty.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/tty.c,v
retrieving revision 1.56
diff -p -u -r1.56 tty.c
--- src/tty.c   3 Aug 2004 23:14:08 -0000       1.56
+++ src/tty.c   21 Sep 2004 20:45:40 -0000
@@ -100,9 +100,6 @@ main (int argc, char **argv)
     {
       switch (optc)
        {
-       case 0:
-         break;
-
        case 's':
          silent = true;
          break;
Index: src/uname.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/uname.c,v
retrieving revision 1.61
diff -p -u -r1.61 uname.c
--- src/uname.c 4 Aug 2004 05:58:43 -0000       1.61
+++ src/uname.c 21 Sep 2004 20:46:01 -0000
@@ -166,9 +166,6 @@ main (int argc, char **argv)
     {
       switch (c)
        {
-       case 0:
-         break;
-
        case 'a':
          toprint = -1;
          break;
Index: src/unexpand.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/unexpand.c,v
retrieving revision 1.82
diff -p -u -r1.82 unexpand.c
--- src/unexpand.c      24 Aug 2004 07:38:49 -0000      1.82
+++ src/unexpand.c      21 Sep 2004 20:46:36 -0000
@@ -492,9 +492,6 @@ main (int argc, char **argv)
     {
       switch (c)
        {
-       case 0:
-         break;
-
        case '?':
          usage (EXIT_FAILURE);
        case 'a':
Index: src/unlink.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/unlink.c,v
retrieving revision 1.13
diff -p -u -r1.13 unlink.c
--- src/unlink.c        21 Jun 2004 15:03:35 -0000      1.13
+++ src/unlink.c        20 Sep 2004 07:31:43 -0000
@@ -73,29 +73,23 @@ main (int argc, char **argv)
 
   parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
                      usage, AUTHORS, (char const *) NULL);
+  if (getopt (argc, argv, "") != -1)
+    usage (EXIT_FAILURE);
 
-  /* The above handles --help and --version.
-     Since there is no other invocation of getopt, handle `--' here.  */
-  if (1 < argc && STREQ (argv[1], "--"))
-    {
-      --argc;
-      ++argv;
-    }
-
-  if (argc < 2)
+  if (argc < optind + 1)
     {
       error (0, 0, _("missing operand"));
       usage (EXIT_FAILURE);
     }
 
-  if (2 < argc)
+  if (optind + 1 < argc)
     {
-      error (0, 0, _("extra operand %s"), quote (argv[2]));
+      error (0, 0, _("extra operand %s"), quote (argv[optind + 1]));
       usage (EXIT_FAILURE);
     }
 
-  if (unlink (argv[1]) != 0)
-    error (EXIT_FAILURE, errno, _("cannot unlink %s"), quote (argv[1]));
+  if (unlink (argv[optind]) != 0)
+    error (EXIT_FAILURE, errno, _("cannot unlink %s"), quote (argv[optind]));
 
   exit (EXIT_SUCCESS);
 }
Index: src/uptime.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/uptime.c,v
retrieving revision 1.44
diff -p -u -r1.44 uptime.c
--- src/uptime.c        2 Aug 2004 20:56:10 -0000       1.44
+++ src/uptime.c        20 Sep 2004 07:31:53 -0000
@@ -44,11 +44,6 @@ int getloadavg ();
 /* The name this program was run with. */
 char *program_name;
 
-static struct option const longopts[] =
-{
-  {NULL, 0, NULL, 0}
-};
-
 static void
 print_uptime (size_t n, const STRUCT_UTMP *this)
 {
@@ -225,18 +220,8 @@ main (int argc, char **argv)
 
   parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
                      usage, AUTHORS, (char const *) NULL);
-
-  while ((optc = getopt_long (argc, argv, "", longopts, NULL)) != -1)
-    {
-      switch (optc)
-       {
-       case 0:
-         break;
-
-       default:
-         usage (EXIT_FAILURE);
-       }
-    }
+  if (getopt (argc, argv, "") != -1)
+    usage (EXIT_FAILURE);
 
   switch (argc - optind)
     {
Index: src/users.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/users.c,v
retrieving revision 1.36
diff -p -u -r1.36 users.c
--- src/users.c 2 Aug 2004 20:56:30 -0000       1.36
+++ src/users.c 20 Sep 2004 07:32:05 -0000
@@ -37,11 +37,6 @@
 /* The name this program was run with. */
 char *program_name;
 
-static struct option const longopts[] =
-{
-  {NULL, 0, NULL, 0}
-};
-
 static int
 userid_compare (const void *v_a, const void *v_b)
 {
@@ -130,7 +125,6 @@ If FILE is not specified, use %s.  %s as
 int
 main (int argc, char **argv)
 {
-  int optc;
   initialize_main (&argc, &argv);
   program_name = argv[0];
   setlocale (LC_ALL, "");
@@ -141,18 +135,8 @@ main (int argc, char **argv)
 
   parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
                      usage, AUTHORS, (char const *) NULL);
-
-  while ((optc = getopt_long (argc, argv, "", longopts, NULL)) != -1)
-    {
-      switch (optc)
-       {
-       case 0:
-         break;
-
-       default:
-         usage (EXIT_FAILURE);
-       }
-    }
+  if (getopt (argc, argv, "") != -1)
+    usage (EXIT_FAILURE);
 
   switch (argc - optind)
     {
Index: src/wc.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/wc.c,v
retrieving revision 1.100
diff -p -u -r1.100 wc.c
--- src/wc.c    3 Aug 2004 23:38:10 -0000       1.100
+++ src/wc.c    21 Sep 2004 20:47:43 -0000
@@ -615,9 +615,6 @@ main (int argc, char **argv)
   while ((optc = getopt_long (argc, argv, "clLmw", longopts, NULL)) != -1)
     switch (optc)
       {
-      case 0:
-       break;
-
       case 'c':
        print_bytes = true;
        break;
Index: src/who.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/who.c,v
retrieving revision 1.102
diff -p -u -r1.102 who.c
--- src/who.c   3 Aug 2004 23:38:21 -0000       1.102
+++ src/who.c   21 Sep 2004 20:48:12 -0000
@@ -703,9 +703,6 @@ main (int argc, char **argv)
     {
       switch (optc)
        {
-       case 0:
-         break;
-
        case 'a':
          need_boottime = true;
          need_deadprocs = true;
Index: src/whoami.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/whoami.c,v
retrieving revision 1.43
diff -p -u -r1.43 whoami.c
--- src/whoami.c        3 Aug 2004 23:38:40 -0000       1.43
+++ src/whoami.c        20 Sep 2004 07:32:19 -0000
@@ -39,11 +39,6 @@
 /* The name this program was run with. */
 char *program_name;
 
-static struct option const long_options[] =
-{
-  {0, 0, 0, 0}
-};
-
 void
 usage (int status)
 {
@@ -70,7 +65,6 @@ main (int argc, char **argv)
 {
   register struct passwd *pw;
   register uid_t uid;
-  int c;
 
   initialize_main (&argc, &argv);
   program_name = argv[0];
@@ -82,18 +76,8 @@ main (int argc, char **argv)
 
   parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
                      usage, AUTHORS, (char const *) NULL);
-
-  while ((c = getopt_long (argc, argv, "", long_options, NULL)) != -1)
-    {
-      switch (c)
-       {
-       case 0:
-         break;
-
-       default:
-         usage (EXIT_FAILURE);
-       }
-    }
+  if (getopt (argc, argv, "") != -1)
+    usage (EXIT_FAILURE);
 
   if (optind != argc)
     {
Index: src/yes.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/yes.c,v
retrieving revision 1.54
diff -p -u -r1.54 yes.c
--- src/yes.c   17 Jun 2004 14:34:19 -0000      1.54
+++ src/yes.c   20 Sep 2004 07:32:52 -0000
@@ -73,25 +73,19 @@ main (int argc, char **argv)
 
   parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
                      usage, AUTHORS, (char const *) NULL);
+  if (getopt (argc, argv, "+") != -1)
+    usage (EXIT_FAILURE);
 
-  /* The above handles --help and --version.
-     Since there is no other invocation of getopt, handle `--' here.  */
-  if (1 < argc && STREQ (argv[1], "--"))
+  if (argc <= optind)
     {
-      --argc;
-      ++argv;
-    }
-
-  if (argc == 1)
-    {
-      argv[1] = "y";
-      argc = 2;
+      optind = argc;
+      argv[argc++] = "y";
     }
 
   for (;;)
     {
       int i;
-      for (i = 1; i < argc; i++)
+      for (i = optind; i < argc; i++)
        if (fputs (argv[i], stdout) == EOF
            || putchar (i == argc - 1 ? '\n' : ' ') == EOF)
          {
Index: tests/factor/basic
===================================================================
RCS file: /home/eggert/coreutils/cu/tests/factor/basic,v
retrieving revision 1.4
diff -p -u -r1.4 basic
--- tests/factor/basic  8 Apr 2003 10:55:01 -0000       1.4
+++ tests/factor/basic  21 Sep 2004 21:21:47 -0000
@@ -55,7 +55,7 @@ my @Tests =
      ['x', '4294966896', {OUT => '2 2 2 2 3 3 3 11 607 1489'}],
      ['y', '4294966998', {OUT => '2 3 7 3917 26107'}],
      ['z', '-1',
-      {ERR => "$prog: `-1' is not a valid positive integer\n"
+      {ERR => "$prog: invalid option -- 1\n"
        . "Try `$prog --help' for more information.\n"},
       {EXIT => 1}],
     );




reply via email to

[Prev in Thread] Current Thread [Next in Thread]