From 4fa016cbc9907ef9da5272c5dcfa76f8ade216f1 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Tue, 21 Sep 2004 22:01:50 +0000 Subject: Include . (main): Reject unknown options. --- src/dirname.c | 18 +++++++----------- src/factor.c | 14 +++++--------- src/hostid.c | 15 +++++---------- src/hostname.c | 35 +++++++++++++++-------------------- src/nohup.c | 15 +++++---------- src/pwd.c | 13 ++++--------- src/setuidgid.c | 21 ++++++++------------- src/sync.c | 13 ++++--------- 8 files changed, 53 insertions(+), 91 deletions(-) (limited to 'src') diff --git a/src/dirname.c b/src/dirname.c index 552b6da0d..40a9b29ce 100644 --- a/src/dirname.c +++ b/src/dirname.c @@ -18,6 +18,7 @@ /* Written by David MacKenzie and Jim Meyering. */ #include +#include #include #include @@ -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) diff --git a/src/factor.c b/src/factor.c index 984b250ee..79adc2882 100644 --- a/src/factor.c +++ b/src/factor.c @@ -19,6 +19,7 @@ Adapted for GNU, fixed to factor UINT_MAX by Jim Meyering. */ #include +#include #include #include @@ -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; diff --git a/src/hostid.c b/src/hostid.c index 1ca34bb89..8bb76abf1 100644 --- a/src/hostid.c +++ b/src/hostid.c @@ -20,6 +20,7 @@ /* Written by Jim Meyering. */ #include +#include #include #include @@ -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); } diff --git a/src/hostname.c b/src/hostname.c index ae74a42cf..929449ef6 100644 --- a/src/hostname.c +++ b/src/hostname.c @@ -18,6 +18,7 @@ /* Written by Jim Meyering. */ #include +#include #include #include @@ -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); } diff --git a/src/nohup.c b/src/nohup.c index 96b2b2330..79a8c5043 100644 --- a/src/nohup.c +++ b/src/nohup.c @@ -18,6 +18,7 @@ /* Written by Jim Meyering */ #include +#include #include #include #include @@ -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); diff --git a/src/pwd.c b/src/pwd.c index 6c64be740..4bac5bb10 100644 --- a/src/pwd.c +++ b/src/pwd.c @@ -16,6 +16,7 @@ Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include +#include #include #include @@ -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 (); diff --git a/src/setuidgid.c b/src/setuidgid.c index 653642f07..7b9804609 100644 --- a/src/setuidgid.c +++ b/src/setuidgid.c @@ -18,6 +18,7 @@ /* Written by Jim Meyering */ #include +#include #include #include #include @@ -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); diff --git a/src/sync.c b/src/sync.c index 045b820c2..04e99d800 100644 --- a/src/sync.c +++ b/src/sync.c @@ -18,6 +18,7 @@ /* Written by Jim Meyering */ #include +#include #include #include @@ -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 (); -- cgit v1.2.3-54-g00ecf