1
0
mirror of git://git.sv.gnu.org/coreutils.git synced 2026-04-19 10:15:48 +02:00
Files
coreutils/src/hostname.c

126 lines
3.3 KiB
C
Raw Normal View History

1994-05-08 16:16:03 +00:00
/* hostname - set or print the name of current host system
Copyright (C) 1994-1997, 1999-2005 Free Software Foundation, Inc.
1994-05-08 16:16:03 +00:00
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 2, 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
1996-03-24 18:33:12 +00:00
along with this program; if not, write to the Free Software Foundation,
2005-05-14 07:58:31 +00:00
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
1994-05-08 16:16:03 +00:00
2003-05-10 15:00:00 +00:00
/* Written by Jim Meyering. */
1994-05-08 16:16:03 +00:00
#include <config.h>
#include <getopt.h>
1994-05-08 16:16:03 +00:00
#include <stdio.h>
1994-05-20 13:50:43 +00:00
#include <sys/types.h>
1994-05-08 16:16:03 +00:00
#include "system.h"
#include "long-options.h"
#include "error.h"
#include "quote.h"
#include "xgethostname.h"
1994-05-08 16:16:03 +00:00
/* The official name of this program (e.g., no `g' prefix). */
#define PROGRAM_NAME "hostname"
#define AUTHORS "Jim Meyering"
#if HAVE_SETHOSTNAME && !defined sethostname
int sethostname ();
#endif
#if !defined HAVE_SETHOSTNAME && defined HAVE_SYSINFO && \
defined HAVE_SYS_SYSTEMINFO_H
1997-05-01 20:51:16 +00:00
# include <sys/systeminfo.h>
1994-05-20 13:50:43 +00:00
int
sethostname (char *name, size_t namelen)
1994-05-20 13:50:43 +00:00
{
/* Using sysinfo() is the SVR4 mechanism to set a hostname. */
return (sysinfo (SI_SET_HOSTNAME, name, namelen) < 0 ? -1 : 0);
1994-05-20 13:50:43 +00:00
}
1997-05-01 20:51:16 +00:00
# define HAVE_SETHOSTNAME 1 /* Now we have it... */
1994-05-20 13:50:43 +00:00
#endif
1994-05-08 16:16:03 +00:00
/* The name this program was run with. */
char *program_name;
1999-01-25 14:33:38 +00:00
void
1996-01-06 11:44:05 +00:00
usage (int status)
1994-05-08 16:16:03 +00:00
{
if (status != EXIT_SUCCESS)
fprintf (stderr, _("Try `%s --help' for more information.\n"),
1994-05-08 16:16:03 +00:00
program_name);
else
{
printf (_("\
1994-05-08 16:16:03 +00:00
Usage: %s [NAME]\n\
or: %s OPTION\n\
1999-08-30 14:36:06 +00:00
Print or set the hostname of the current system.\n\
1994-05-08 16:16:03 +00:00
\n\
"),
program_name, program_name);
fputs (HELP_OPTION_DESCRIPTION, stdout);
fputs (VERSION_OPTION_DESCRIPTION, stdout);
printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
1994-05-08 16:16:03 +00:00
}
exit (status);
}
int
1996-01-06 11:44:05 +00:00
main (int argc, char **argv)
1994-05-08 16:16:03 +00:00
{
char *hostname;
2003-06-17 18:13:23 +00:00
initialize_main (&argc, &argv);
1994-05-08 16:16:03 +00:00
program_name = argv[0];
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
1994-05-08 16:16:03 +00:00
atexit (close_stdout);
parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
usage, AUTHORS, (char const *) NULL);
if (getopt_long (argc, argv, "", NULL, NULL) != -1)
usage (EXIT_FAILURE);
1994-05-08 16:16:03 +00:00
if (argc == optind + 1)
2004-06-17 13:09:21 +00:00
{
1994-05-08 16:16:03 +00:00
#ifdef HAVE_SETHOSTNAME
/* Set hostname to operand. */
char const *name = argv[optind];
if (sethostname (name, strlen (name)) != 0)
error (EXIT_FAILURE, errno, _("cannot set name to %s"), quote (name));
1994-05-08 16:42:40 +00:00
#else
error (EXIT_FAILURE, 0,
_("cannot set hostname; this system lacks the functionality"));
1994-05-08 16:16:03 +00:00
#endif
}
1994-05-08 16:16:03 +00:00
if (argc <= optind)
1994-05-08 16:16:03 +00:00
{
hostname = xgethostname ();
if (hostname == NULL)
error (EXIT_FAILURE, errno, _("cannot determine hostname"));
1994-05-08 16:16:03 +00:00
printf ("%s\n", hostname);
}
if (optind + 1 < argc)
1994-05-08 16:16:03 +00:00
{
error (0, 0, _("extra operand %s"), quote (argv[optind + 1]));
usage (EXIT_FAILURE);
1994-05-08 16:16:03 +00:00
}
exit (EXIT_SUCCESS);
1994-05-08 16:16:03 +00:00
}