1 /* hostname - set or print the name of current host system
2    Copyright (C) 1994-2023 Free Software Foundation, Inc.
3 
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.
8 
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.
13 
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
16 
17 /* Written by Jim Meyering.  */
18 
19 #include <config.h>
20 #include <stdio.h>
21 #include <sys/types.h>
22 
23 #include "system.h"
24 #include "long-options.h"
25 #include "quote.h"
26 #include "xgethostname.h"
27 
28 /* The official name of this program (e.g., no 'g' prefix).  */
29 #define PROGRAM_NAME "hostname"
30 
31 #define AUTHORS proper_name ("Jim Meyering")
32 
33 #ifndef HAVE_SETHOSTNAME
34 # if defined HAVE_SYSINFO && defined HAVE_SYS_SYSTEMINFO_H
35 #  include <sys/systeminfo.h>
36 # endif
37 
38 static int
sethostname(char const * name,size_t namelen)39 sethostname (char const *name, size_t namelen)
40 {
41 # if defined HAVE_SYSINFO && defined HAVE_SYS_SYSTEMINFO_H
42   /* Using sysinfo() is the SVR4 mechanism to set a hostname. */
43   return (sysinfo (SI_SET_HOSTNAME, name, namelen) < 0 ? -1 : 0);
44 # else
45   errno = ENOTSUP;
46   return -1;
47 # endif
48 }
49 #endif
50 
51 void
usage(int status)52 usage (int status)
53 {
54   if (status != EXIT_SUCCESS)
55     emit_try_help ();
56   else
57     {
58       printf (_("\
59 Usage: %s [NAME]\n\
60   or:  %s OPTION\n\
61 Print or set the hostname of the current system.\n\
62 \n\
63 "),
64              program_name, program_name);
65       fputs (HELP_OPTION_DESCRIPTION, stdout);
66       fputs (VERSION_OPTION_DESCRIPTION, stdout);
67       emit_ancillary_info (PROGRAM_NAME);
68     }
69   exit (status);
70 }
71 
72 int
main(int argc,char ** argv)73 main (int argc, char **argv)
74 {
75   char *hostname;
76 
77   initialize_main (&argc, &argv);
78   set_program_name (argv[0]);
79   setlocale (LC_ALL, "");
80   bindtextdomain (PACKAGE, LOCALEDIR);
81   textdomain (PACKAGE);
82 
83   atexit (close_stdout);
84 
85   parse_gnu_standard_options_only (argc, argv, PROGRAM_NAME, PACKAGE_NAME,
86                                    Version, true, usage, AUTHORS,
87                                    (char const *) nullptr);
88 
89   if (optind + 1 < argc)
90      {
91        error (0, 0, _("extra operand %s"), quote (argv[optind + 1]));
92        usage (EXIT_FAILURE);
93      }
94 
95   if (optind + 1 == argc)
96     {
97       /* Set hostname to operand.  */
98       char const *name = argv[optind];
99       if (sethostname (name, strlen (name)) != 0)
100         error (EXIT_FAILURE, errno, _("cannot set name to %s"),
101                quote (name));
102     }
103   else
104     {
105       hostname = xgethostname ();
106       if (hostname == nullptr)
107         error (EXIT_FAILURE, errno, _("cannot determine hostname"));
108       puts (hostname);
109     }
110 
111   main_exit (EXIT_SUCCESS);
112 }
113