maint: move all id(1) tests to the same directory
[coreutils.git] / src / hostname.c
blobd0a4d754d77a4b3924bd84f3f681e827d610038a
1 /* hostname - set or print the name of current host system
2 Copyright (C) 1994-2013 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by Jim Meyering. */
19 #include <config.h>
20 #include <getopt.h>
21 #include <stdio.h>
22 #include <sys/types.h>
24 #include "system.h"
25 #include "long-options.h"
26 #include "error.h"
27 #include "quote.h"
28 #include "xgethostname.h"
30 /* The official name of this program (e.g., no 'g' prefix). */
31 #define PROGRAM_NAME "hostname"
33 #define AUTHORS proper_name ("Jim Meyering")
35 #if !defined HAVE_SETHOSTNAME && defined HAVE_SYSINFO && \
36 defined HAVE_SYS_SYSTEMINFO_H
37 # include <sys/systeminfo.h>
39 static int
40 sethostname (char *name, size_t namelen)
42 /* Using sysinfo() is the SVR4 mechanism to set a hostname. */
43 return (sysinfo (SI_SET_HOSTNAME, name, namelen) < 0 ? -1 : 0);
46 # define HAVE_SETHOSTNAME 1 /* Now we have it... */
47 #endif
49 void
50 usage (int status)
52 if (status != EXIT_SUCCESS)
53 emit_try_help ();
54 else
56 printf (_("\
57 Usage: %s [NAME]\n\
58 or: %s OPTION\n\
59 Print or set the hostname of the current system.\n\
60 \n\
61 "),
62 program_name, program_name);
63 fputs (HELP_OPTION_DESCRIPTION, stdout);
64 fputs (VERSION_OPTION_DESCRIPTION, stdout);
65 emit_ancillary_info ();
67 exit (status);
70 int
71 main (int argc, char **argv)
73 char *hostname;
75 initialize_main (&argc, &argv);
76 set_program_name (argv[0]);
77 setlocale (LC_ALL, "");
78 bindtextdomain (PACKAGE, LOCALEDIR);
79 textdomain (PACKAGE);
81 atexit (close_stdout);
83 parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
84 usage, AUTHORS, (char const *) NULL);
85 if (getopt_long (argc, argv, "", NULL, NULL) != -1)
86 usage (EXIT_FAILURE);
88 if (argc == optind + 1)
90 #ifdef HAVE_SETHOSTNAME
91 /* Set hostname to operand. */
92 char const *name = argv[optind];
93 if (sethostname (name, strlen (name)) != 0)
94 error (EXIT_FAILURE, errno, _("cannot set name to %s"), quote (name));
95 #else
96 error (EXIT_FAILURE, 0,
97 _("cannot set hostname; this system lacks the functionality"));
98 #endif
101 if (argc <= optind)
103 hostname = xgethostname ();
104 if (hostname == NULL)
105 error (EXIT_FAILURE, errno, _("cannot determine hostname"));
106 printf ("%s\n", hostname);
109 if (optind + 1 < argc)
111 error (0, 0, _("extra operand %s"), quote (argv[optind + 1]));
112 usage (EXIT_FAILURE);
115 exit (EXIT_SUCCESS);