1 /* hostname - set or print the name of current host system
2 Copyright (C) 1994-1997, 1999 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)
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Jim Meyering <meyering@comco.com> */
22 #include <sys/types.h>
25 #include "long-options.h"
28 /* The official name of this program (e.g., no `g' prefix). */
29 #define PROGRAM_NAME "hostname"
31 #define AUTHORS "Jim Meyering"
33 #if !defined(HAVE_SETHOSTNAME) && defined(HAVE_SYSINFO) && \
34 defined (HAVE_SYS_SYSTEMINFO_H) && defined(HAVE_LIMITS_H)
35 # include <sys/systeminfo.h>
38 sethostname (name
, namelen
)
42 /* Using sysinfo() is the SVR4 mechanism to set a hostname. */
45 result
= sysinfo (SI_SET_HOSTNAME
, name
, namelen
);
47 return (result
== -1 ? result
: 0);
50 # define HAVE_SETHOSTNAME 1 /* Now we have it... */
53 char *xgethostname ();
55 /* The name this program was run with. */
62 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
69 Print or set the hostname of the current system.\n\
71 --help display this help and exit\n\
72 --version output version information and exit\n\
74 , program_name
, program_name
);
75 puts (_("\nReport bugs to <bug-sh-utils@gnu.org>."));
81 main (int argc
, char **argv
)
85 program_name
= argv
[0];
86 setlocale (LC_ALL
, "");
87 bindtextdomain (PACKAGE
, LOCALEDIR
);
90 parse_long_options (argc
, argv
, PROGRAM_NAME
, GNU_PACKAGE
, VERSION
,
93 #ifdef HAVE_SETHOSTNAME
98 /* Set hostname to argv[1]. */
99 err
= sethostname (argv
[1], strlen (argv
[1]));
101 error (1, errno
, _("cannot set hostname to `%s'"), argv
[1]);
107 _("cannot set hostname; this system lacks the functionality"));
112 hostname
= xgethostname ();
113 if (hostname
== NULL
)
114 error (1, errno
, _("cannot determine hostname"));
115 printf ("%s\n", hostname
);
119 error (2, 0, _("too many arguments"));