(main): Reject unknown options.
/* Written by David MacKenzie and Jim Meyering. */
#include <config.h>
+#include <getopt.h>
#include <stdio.h>
#include <sys/types.h>
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)
Adapted for GNU, fixed to factor UINT_MAX by Jim Meyering. */
#include <config.h>
+#include <getopt.h>
#include <stdio.h>
#include <sys/types.h>
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;
/* Written by Jim Meyering. */
#include <config.h>
+#include <getopt.h>
#include <stdio.h>
#include <sys/types.h>
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);
}
/* Written by Jim Meyering. */
#include <config.h>
+#include <getopt.h>
#include <stdio.h>
#include <sys/types.h>
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);
}
/* Written by Jim Meyering */
#include <config.h>
+#include <getopt.h>
#include <stdio.h>
#include <sys/types.h>
#include <signal.h>
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);
{
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);
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
#include <config.h>
+#include <getopt.h>
#include <stdio.h>
#include <sys/types.h>
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 ();
/* Written by Jim Meyering */
#include <config.h>
+#include <getopt.h>
#include <stdio.h>
#include <sys/types.h>
#include <pwd.h>
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,
_("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);
/* Written by Jim Meyering */
#include <config.h>
+#include <getopt.h>
#include <stdio.h>
#include <sys/types.h>
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 ();