/* hostname - set or print the name of current host system This is the hostname utility
Copyright (C) 1994-2018 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */ The GNUv3 license
/* Written by Jim Meyering. */
#include <config.h> Provides system specific information
#include <getopt.h> ...!includes auto-comment...
#include <stdio.h> Provides standard I/O capability
#include <sys/types.h> Provides system data types
#include "system.h" ...!includes auto-comment...
#include "long-options.h" ...!includes auto-comment...
#include "die.h" ...!includes auto-comment...
#include "error.h" ...!includes auto-comment...
#include "quote.h" ...!includes auto-comment...
#include "xgethostname.h" ...!includes auto-comment...
/* The official name of this program (e.g., no 'g' prefix). */
#define PROGRAM_NAME "hostname" Line 32
#define AUTHORS proper_name ("Jim Meyering") Line 34
static struct option const long_options[] = Line 36
{
{NULL, 0, NULL, 0} Line 38
}; Block 1
#if !defined HAVE_SETHOSTNAME && defined HAVE_SYSINFO && \ Line 41
defined HAVE_SYS_SYSTEMINFO_H Line 42
# include <sys/systeminfo.h> Line 43
static int Line 45
sethostname (char *name, size_t namelen) Line 46
{
/* Using sysinfo() is the SVR4 mechanism to set a hostname. */
return (sysinfo (SI_SET_HOSTNAME, name, namelen) < 0 ? -1 : 0); Line 49
}
# define HAVE_SETHOSTNAME 1 /* Now we have it... */ Line 52
#endif Line 53
void Line 55
usage (int status) Line 56
{
if (status != EXIT_SUCCESS) Line 58
emit_try_help (); ...!common auto-comment...
else Line 60
{
printf (_("\ Line 62
Usage: %s [NAME]\n\ Line 63
or: %s OPTION\n\ Line 64
Print or set the hostname of the current system.\n\ Line 65
\n\
"), Line 67
program_name, program_name); Line 68
fputs (HELP_OPTION_DESCRIPTION, stdout); Line 69
fputs (VERSION_OPTION_DESCRIPTION, stdout); Line 70
emit_ancillary_info (PROGRAM_NAME); Line 71
}
exit (status); Line 73
} Block 3
int
main (int argc, char **argv) Line 77
{
char *hostname; Line 79
initialize_main (&argc, &argv); VMS-specific entry point handling wildcard expansion
set_program_name (argv[0]); Retains program name and discards path
setlocale (LC_ALL, ""); Sets up internationalization (i18n)
bindtextdomain (PACKAGE, LOCALEDIR); Assigns i18n directorySets text domain for _() [gettext()] function
textdomain (PACKAGE); Sets text domain for _() [gettext()] function
atexit (close_stdout); Close stdout on exit (see gnulib)
parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version, ...!common auto-comment...
usage, AUTHORS, (char const *) NULL); Line 90
if (getopt_long (argc, argv, "", long_options, NULL) != -1) Line 91
usage (EXIT_FAILURE); Line 92
if (argc == optind + 1) Line 94
{
#ifdef HAVE_SETHOSTNAME Line 96
/* Set hostname to operand. */
char const *name = argv[optind]; Line 98
if (sethostname (name, strlen (name)) != 0) Line 99
die (EXIT_FAILURE, errno, _("cannot set name to %s"), Line 100
quote (name)); Line 101
#else Line 102
die (EXIT_FAILURE, 0, Line 103
_("cannot set hostname; this system lacks the functionality")); Line 104
#endif Line 105
}
if (argc <= optind) Line 108
{
hostname = xgethostname (); Line 110
if (hostname == NULL) Line 111
die (EXIT_FAILURE, errno, _("cannot determine hostname")); Line 112
printf ("%s\n", hostname); Line 113
}
if (optind + 1 < argc) Line 116
{
error (0, 0, _("extra operand %s"), quote (argv[optind + 1])); Line 118
usage (EXIT_FAILURE); Line 119
}
return EXIT_SUCCESS; Line 122
} Block 4