]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
Include <getopt.h>.
authorPaul Eggert <eggert@cs.ucla.edu>
Tue, 21 Sep 2004 22:01:50 +0000 (22:01 +0000)
committerPaul Eggert <eggert@cs.ucla.edu>
Tue, 21 Sep 2004 22:01:50 +0000 (22:01 +0000)
(main): Reject unknown options.

src/dirname.c
src/factor.c
src/hostid.c
src/hostname.c
src/nohup.c
src/pwd.c
src/setuidgid.c
src/sync.c

index 552b6da0d8148abdff458af10500c5b9cb0bddd6..40a9b29ce16e98160be7b89c7c38858a4a91bae2 100644 (file)
@@ -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 984b250ee8a670ce412b6ad395b6284b9c0a45fb..79adc2882e70daaf034887617cb9899bbc06431b 100644 (file)
@@ -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 1ca34bb89d3133162d0beacff95f21b750f587f7..8bb76abf1892a77e1d0c0cdee1bf0b262cd53eb2 100644 (file)
@@ -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 ae74a42cffd4936d411bc1ab2cf403ac7c9b22f9..929449ef6e7040687599e0729879e7e2518ff6ff 100644 (file)
@@ -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 96b2b23302c2720e5b3ce30ef553de4e55b897eb..79a8c5043220021629a4cbdbee6de8308c5f429c 100644 (file)
@@ -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 6c64be74046981b7830c3bfe71d570f0f49b97fb..4bac5bb10b95cebffef9e4e723fde3ef01bec51b 100644 (file)
--- a/src/pwd.c
+++ b/src/pwd.c
@@ -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 653642f07ecf05aecfcfe6e46bac280fb84e5cf5..7b98046092145856610e22c4dc370657b15a77c9 100644 (file)
@@ -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 045b820c24bb1b7d48093891f2370425e5cf6abb..04e99d800adb5d754c0581b0f62be29c365b83d6 100644 (file)
@@ -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 ();