* src/dd.c (flags): noatime and nofollow now depend on
[coreutils/bo.git] / src / hostname.c
blobf6748a66d829b8dbb2c475c1142804796b13666a
1 /* hostname - set or print the name of current host system
2 Copyright (C) 1994-1997, 1999-2005 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 2, or (at your option)
7 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, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Written by Jim Meyering. */
20 #include <config.h>
21 #include <getopt.h>
22 #include <stdio.h>
23 #include <sys/types.h>
25 #include "system.h"
26 #include "long-options.h"
27 #include "error.h"
28 #include "quote.h"
29 #include "xgethostname.h"
31 /* The official name of this program (e.g., no `g' prefix). */
32 #define PROGRAM_NAME "hostname"
34 #define AUTHORS "Jim Meyering"
36 #if HAVE_SETHOSTNAME && !defined sethostname
37 int sethostname ();
38 #endif
40 #if !defined HAVE_SETHOSTNAME && defined HAVE_SYSINFO && \
41 defined HAVE_SYS_SYSTEMINFO_H
42 # include <sys/systeminfo.h>
44 int
45 sethostname (char *name, size_t namelen)
47 /* Using sysinfo() is the SVR4 mechanism to set a hostname. */
48 return (sysinfo (SI_SET_HOSTNAME, name, namelen) < 0 ? -1 : 0);
51 # define HAVE_SETHOSTNAME 1 /* Now we have it... */
52 #endif
54 /* The name this program was run with. */
55 char *program_name;
57 void
58 usage (int status)
60 if (status != EXIT_SUCCESS)
61 fprintf (stderr, _("Try `%s --help' for more information.\n"),
62 program_name);
63 else
65 printf (_("\
66 Usage: %s [NAME]\n\
67 or: %s OPTION\n\
68 Print or set the hostname of the current system.\n\
69 \n\
70 "),
71 program_name, program_name);
72 fputs (HELP_OPTION_DESCRIPTION, stdout);
73 fputs (VERSION_OPTION_DESCRIPTION, stdout);
74 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
76 exit (status);
79 int
80 main (int argc, char **argv)
82 char *hostname;
84 initialize_main (&argc, &argv);
85 program_name = argv[0];
86 setlocale (LC_ALL, "");
87 bindtextdomain (PACKAGE, LOCALEDIR);
88 textdomain (PACKAGE);
90 atexit (close_stdout);
92 parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
93 usage, AUTHORS, (char const *) NULL);
94 if (getopt_long (argc, argv, "", NULL, NULL) != -1)
95 usage (EXIT_FAILURE);
97 if (argc == optind + 1)
99 #ifdef HAVE_SETHOSTNAME
100 /* Set hostname to operand. */
101 char const *name = argv[optind];
102 if (sethostname (name, strlen (name)) != 0)
103 error (EXIT_FAILURE, errno, _("cannot set name to %s"), quote (name));
104 #else
105 error (EXIT_FAILURE, 0,
106 _("cannot set hostname; this system lacks the functionality"));
107 #endif
110 if (argc <= optind)
112 hostname = xgethostname ();
113 if (hostname == NULL)
114 error (EXIT_FAILURE, errno, _("cannot determine hostname"));
115 printf ("%s\n", hostname);
118 if (optind + 1 < argc)
120 error (0, 0, _("extra operand %s"), quote (argv[optind + 1]));
121 usage (EXIT_FAILURE);
124 exit (EXIT_SUCCESS);