summaryrefslogtreecommitdiff
path: root/src/hostname.c
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2004-09-21 22:01:50 +0000
committerPaul Eggert <eggert@cs.ucla.edu>2004-09-21 22:01:50 +0000
commit4fa016cbc9907ef9da5272c5dcfa76f8ade216f1 (patch)
tree04e1e2f72b4a504957d92701df36c278df2f8a13 /src/hostname.c
parent0d219ee57d8cab4a78432c97754e22dd9248951e (diff)
downloadcoreutils-4fa016cbc9907ef9da5272c5dcfa76f8ade216f1.tar.xz
Include <getopt.h>.
(main): Reject unknown options.
Diffstat (limited to 'src/hostname.c')
-rw-r--r--src/hostname.c35
1 files changed, 15 insertions, 20 deletions
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 <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);
}