| 1 | /* hostname - set or print the name of current host system
|
|---|
| 2 | Copyright (C) 1994-1997, 1999-2005 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 2, or (at your option)
|
|---|
| 7 | 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, write to the Free Software Foundation,
|
|---|
| 16 | Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
|
|---|
| 17 |
|
|---|
| 18 | /* Written by Jim Meyering. */
|
|---|
| 19 |
|
|---|
| 20 | #include <config.h>
|
|---|
| 21 | #include <getopt.h>
|
|---|
| 22 | #include <stdio.h>
|
|---|
| 23 | #include <sys/types.h>
|
|---|
| 24 |
|
|---|
| 25 | #include "system.h"
|
|---|
| 26 | #include "long-options.h"
|
|---|
| 27 | #include "error.h"
|
|---|
| 28 | #include "quote.h"
|
|---|
| 29 | #include "xgethostname.h"
|
|---|
| 30 |
|
|---|
| 31 | /* The official name of this program (e.g., no `g' prefix). */
|
|---|
| 32 | #define PROGRAM_NAME "hostname"
|
|---|
| 33 |
|
|---|
| 34 | #define AUTHORS "Jim Meyering"
|
|---|
| 35 |
|
|---|
| 36 | #if HAVE_SETHOSTNAME && !defined sethostname
|
|---|
| 37 | int sethostname ();
|
|---|
| 38 | #endif
|
|---|
| 39 |
|
|---|
| 40 | #if !defined HAVE_SETHOSTNAME && defined HAVE_SYSINFO && \
|
|---|
| 41 | defined HAVE_SYS_SYSTEMINFO_H
|
|---|
| 42 | # include <sys/systeminfo.h>
|
|---|
| 43 |
|
|---|
| 44 | int
|
|---|
| 45 | sethostname (char *name, size_t namelen)
|
|---|
| 46 | {
|
|---|
| 47 | /* Using sysinfo() is the SVR4 mechanism to set a hostname. */
|
|---|
| 48 | return (sysinfo (SI_SET_HOSTNAME, name, namelen) < 0 ? -1 : 0);
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | # define HAVE_SETHOSTNAME 1 /* Now we have it... */
|
|---|
| 52 | #endif
|
|---|
| 53 |
|
|---|
| 54 | /* The name this program was run with. */
|
|---|
| 55 | char *program_name;
|
|---|
| 56 |
|
|---|
| 57 | void
|
|---|
| 58 | usage (int status)
|
|---|
| 59 | {
|
|---|
| 60 | if (status != EXIT_SUCCESS)
|
|---|
| 61 | fprintf (stderr, _("Try `%s --help' for more information.\n"),
|
|---|
| 62 | program_name);
|
|---|
| 63 | else
|
|---|
| 64 | {
|
|---|
| 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);
|
|---|
| 75 | }
|
|---|
| 76 | exit (status);
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | int
|
|---|
| 80 | main (int argc, char **argv)
|
|---|
| 81 | {
|
|---|
| 82 | char *hostname;
|
|---|
| 83 |
|
|---|
| 84 | initialize_main (&argc, &argv);
|
|---|
| 85 | program_name = argv[0];
|
|---|
| 86 | setlocale (LC_ALL, "");
|
|---|
| 87 | bindtextdomain (PACKAGE, LOCALEDIR);
|
|---|
| 88 | textdomain (PACKAGE);
|
|---|
| 89 |
|
|---|
| 90 | atexit (close_stdout);
|
|---|
| 91 |
|
|---|
| 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);
|
|---|
| 96 |
|
|---|
| 97 | if (argc == optind + 1)
|
|---|
| 98 | {
|
|---|
| 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
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | if (argc <= optind)
|
|---|
| 111 | {
|
|---|
| 112 | hostname = xgethostname ();
|
|---|
| 113 | if (hostname == NULL)
|
|---|
| 114 | error (EXIT_FAILURE, errno, _("cannot determine hostname"));
|
|---|
| 115 | printf ("%s\n", hostname);
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | if (optind + 1 < argc)
|
|---|
| 119 | {
|
|---|
| 120 | error (0, 0, _("extra operand %s"), quote (argv[optind + 1]));
|
|---|
| 121 | usage (EXIT_FAILURE);
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | exit (EXIT_SUCCESS);
|
|---|
| 125 | }
|
|---|