*** empty log message ***
[coreutils.git] / src / hostname.c
blob2171ff053208def4b33f3ae714922af4f9d6aff8
1 /* hostname - set or print the name of current host system
2 Copyright (C) 1994-1997, 1999, 2000, 2001 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)
7 any later version.
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> */
20 #include <config.h>
21 #include <stdio.h>
22 #include <sys/types.h>
24 #include "system.h"
25 #include "closeout.h"
26 #include "long-options.h"
27 #include "error.h"
29 /* The official name of this program (e.g., no `g' prefix). */
30 #define PROGRAM_NAME "hostname"
32 #define AUTHORS "Jim Meyering"
34 #if !defined(HAVE_SETHOSTNAME) && defined(HAVE_SYSINFO) && \
35 defined (HAVE_SYS_SYSTEMINFO_H) && defined(HAVE_LIMITS_H)
36 # include <sys/systeminfo.h>
38 int
39 sethostname (name, namelen)
40 char *name;
41 int namelen;
43 /* Using sysinfo() is the SVR4 mechanism to set a hostname. */
44 int result;
46 result = sysinfo (SI_SET_HOSTNAME, name, namelen);
48 return (result == -1 ? result : 0);
51 # define HAVE_SETHOSTNAME 1 /* Now we have it... */
52 #endif
54 char *xgethostname ();
56 /* The name this program was run with. */
57 char *program_name;
59 void
60 usage (int status)
62 if (status != 0)
63 fprintf (stderr, _("Try `%s --help' for more information.\n"),
64 program_name);
65 else
67 printf (_("\
68 Usage: %s [NAME]\n\
69 or: %s OPTION\n\
70 Print or set the hostname of the current system.\n\
71 \n\
72 "),
73 program_name, program_name);
74 fputs (HELP_OPTION_DESCRIPTION, stdout);
75 fputs (VERSION_OPTION_DESCRIPTION, stdout);
76 puts (_("\nReport bugs to <bug-sh-utils@gnu.org>."));
78 exit (status);
81 int
82 main (int argc, char **argv)
84 char *hostname;
86 program_name = argv[0];
87 setlocale (LC_ALL, "");
88 bindtextdomain (PACKAGE, LOCALEDIR);
89 textdomain (PACKAGE);
91 atexit (close_stdout);
93 parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
94 AUTHORS, usage);
96 #ifdef HAVE_SETHOSTNAME
97 if (argc == 2)
99 int err;
101 /* Set hostname to argv[1]. */
102 err = sethostname (argv[1], strlen (argv[1]));
103 if (err != 0)
104 error (1, errno, _("cannot set hostname to `%s'"), argv[1]);
105 exit (0);
107 #else
108 if (argc == 2)
109 error (1, 0,
110 _("cannot set hostname; this system lacks the functionality"));
111 #endif
113 if (argc == 1)
115 hostname = xgethostname ();
116 if (hostname == NULL)
117 error (1, errno, _("cannot determine hostname"));
118 printf ("%s\n", hostname);
120 else
122 error (2, 0, _("too many arguments"));
123 usage (1);
126 exit (0);