1994-05-08 16:16:03 +00:00
|
|
|
/* hostname - set or print the name of current host system
|
2005-05-18 19:32:28 +00:00
|
|
|
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>
|
2004-09-21 22:01:50 +00:00
|
|
|
#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"
|
1994-12-20 05:26:44 +00:00
|
|
|
#include "error.h"
|
2004-06-21 15:03:35 +00:00
|
|
|
#include "quote.h"
|
2004-08-02 21:52:28 +00:00
|
|
|
#include "xgethostname.h"
|
1994-05-08 16:16:03 +00:00
|
|
|
|
1999-03-31 04:11:35 +00:00
|
|
|
/* The official name of this program (e.g., no `g' prefix). */
|
|
|
|
|
#define PROGRAM_NAME "hostname"
|
|
|
|
|
|
2003-10-18 10:05:47 +00:00
|
|
|
#define AUTHORS "Jim Meyering"
|
1999-03-31 04:11:35 +00:00
|
|
|
|
2004-03-29 07:30:09 +00:00
|
|
|
#if HAVE_SETHOSTNAME && !defined sethostname
|
|
|
|
|
int sethostname ();
|
|
|
|
|
#endif
|
|
|
|
|
|
2005-08-23 15:06:55 +00:00
|
|
|
#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
|
2004-08-02 21:52:28 +00:00
|
|
|
sethostname (char *name, size_t namelen)
|
1994-05-20 13:50:43 +00:00
|
|
|
{
|
|
|
|
|
/* Using sysinfo() is the SVR4 mechanism to set a hostname. */
|
2004-08-02 21:52:28 +00:00
|
|
|
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
|
|
|
{
|
2004-01-21 23:09:07 +00:00
|
|
|
if (status != EXIT_SUCCESS)
|
1995-08-08 04:37:34 +00:00
|
|
|
fprintf (stderr, _("Try `%s --help' for more information.\n"),
|
1994-05-08 16:16:03 +00:00
|
|
|
program_name);
|
|
|
|
|
else
|
|
|
|
|
{
|
1995-08-08 04:37:34 +00:00
|
|
|
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\
|
2001-12-15 20:46:30 +00:00
|
|
|
"),
|
|
|
|
|
program_name, program_name);
|
|
|
|
|
fputs (HELP_OPTION_DESCRIPTION, stdout);
|
|
|
|
|
fputs (VERSION_OPTION_DESCRIPTION, stdout);
|
2002-07-02 09:09:10 +00:00
|
|
|
printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
|
1994-05-08 16:16:03 +00:00
|
|
|
}
|
|
|
|
|
exit (status);
|
|
|
|
|
}
|
|
|
|
|
|
1996-03-21 22:47:02 +00:00
|
|
|
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];
|
1996-03-12 23:49:29 +00:00
|
|
|
setlocale (LC_ALL, "");
|
|
|
|
|
bindtextdomain (PACKAGE, LOCALEDIR);
|
|
|
|
|
textdomain (PACKAGE);
|
1994-05-08 16:16:03 +00:00
|
|
|
|
2000-05-07 14:53:15 +00:00
|
|
|
atexit (close_stdout);
|
|
|
|
|
|
1999-03-31 04:11:35 +00:00
|
|
|
parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
|
2003-11-05 03:53:19 +00:00
|
|
|
usage, AUTHORS, (char const *) NULL);
|
2004-11-17 00:56:25 +00:00
|
|
|
if (getopt_long (argc, argv, "", NULL, NULL) != -1)
|
2004-09-21 22:01:50 +00:00
|
|
|
usage (EXIT_FAILURE);
|
1994-05-08 16:16:03 +00:00
|
|
|
|
2004-09-21 22:01:50 +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
|
2004-09-21 22:01:50 +00:00
|
|
|
/* Set hostname to operand. */
|
|
|
|
|
char const *name = argv[optind];
|
|
|
|
|
if (sethostname (name, strlen (name)) != 0)
|
2005-06-16 21:33:43 +00:00
|
|
|
error (EXIT_FAILURE, errno, _("cannot set name to %s"), quote (name));
|
1994-05-08 16:42:40 +00:00
|
|
|
#else
|
2004-09-21 22:01:50 +00:00
|
|
|
error (EXIT_FAILURE, 0,
|
|
|
|
|
_("cannot set hostname; this system lacks the functionality"));
|
1994-05-08 16:16:03 +00:00
|
|
|
#endif
|
2004-09-21 22:01:50 +00:00
|
|
|
}
|
1994-05-08 16:16:03 +00:00
|
|
|
|
2004-09-21 22:01:50 +00:00
|
|
|
if (argc <= optind)
|
1994-05-08 16:16:03 +00:00
|
|
|
{
|
|
|
|
|
hostname = xgethostname ();
|
|
|
|
|
if (hostname == NULL)
|
2002-08-30 23:04:53 +00:00
|
|
|
error (EXIT_FAILURE, errno, _("cannot determine hostname"));
|
1994-05-08 16:16:03 +00:00
|
|
|
printf ("%s\n", hostname);
|
|
|
|
|
}
|
2004-09-21 22:01:50 +00:00
|
|
|
|
|
|
|
|
if (optind + 1 < argc)
|
1994-05-08 16:16:03 +00:00
|
|
|
{
|
2004-09-21 22:01:50 +00:00
|
|
|
error (0, 0, _("extra operand %s"), quote (argv[optind + 1]));
|
2002-08-31 08:52:10 +00:00
|
|
|
usage (EXIT_FAILURE);
|
1994-05-08 16:16:03 +00:00
|
|
|
}
|
|
|
|
|
|
2002-08-31 08:52:10 +00:00
|
|
|
exit (EXIT_SUCCESS);
|
1994-05-08 16:16:03 +00:00
|
|
|
}
|