1
0
mirror of git://git.sv.gnu.org/coreutils.git synced 2026-04-15 08:15:49 +02:00

Include <getopt.h>.

(main): Reject unknown options.
This commit is contained in:
Paul Eggert
2004-09-21 22:01:50 +00:00
parent 0d219ee57d
commit 4fa016cbc9
8 changed files with 53 additions and 91 deletions

View 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)

View 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;

View 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);
}

View 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);
}

View 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);

View File

@@ -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 ();

View 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);

View 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 ();