1 /* hostname - set or print the name of current host system
2 Copyright (C) 94, 1995 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
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* Jim Meyering <meyering@comco.com> */
22 #include <sys/types.h>
26 #include "long-options.h"
29 #if !defined(HAVE_SETHOSTNAME) && defined(HAVE_SYSINFO) && \
30 defined (HAVE_SYS_SYSTEMINFO_H) && defined(HAVE_LIMITS_H)
32 #include <sys/systeminfo.h>
35 sethostname (name
, namelen
)
39 /* Using sysinfo() is the SVR4 mechanism to set a hostname. */
42 result
= sysinfo (SI_SET_HOSTNAME
, name
, namelen
);
44 return (result
== -1 ? result
: 0);
47 #define HAVE_SETHOSTNAME 1 /* Now we have it... */
50 char *xgethostname ();
52 /* The name this program was run with. */
59 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
66 Print the hostname of the current system.\n\
68 --help display this help and exit\n\
69 --version output version information and exit\n\
71 , program_name
, program_name
);
77 main (int argc
, char **argv
)
81 program_name
= argv
[0];
82 setlocale (LC_ALL
, "");
83 bindtextdomain (PACKAGE
, LOCALEDIR
);
86 parse_long_options (argc
, argv
, "hostname", version_string
, usage
);
88 #ifdef HAVE_SETHOSTNAME
93 /* Set hostname to argv[1]. */
94 err
= sethostname (argv
[1], strlen (argv
[1]));
96 error (1, errno
, "%s", argv
[1]);
102 _("cannot set hostname; this system lacks the functionality"));
107 hostname
= xgethostname ();
108 if (hostname
== NULL
)
109 error (1, errno
, _("cannot determine hostname"));
110 printf ("%s\n", hostname
);
114 error (2, 0, _("too many arguments"));