update copyrights for 1997
[platform/upstream/coreutils.git] / src / hostname.c
1 /* hostname - set or print the name of current host system
2    Copyright (C) 94, 95, 96, 1997 Free Software Foundation, Inc.
3
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.
8
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.
13
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.  */
17
18 /* Jim Meyering <meyering@comco.com> */
19
20 #include <config.h>
21 #include <stdio.h>
22 #include <sys/types.h>
23
24 #include "system.h"
25 #include "long-options.h"
26 #include "error.h"
27
28 #if !defined(HAVE_SETHOSTNAME) && defined(HAVE_SYSINFO) && \
29      defined (HAVE_SYS_SYSTEMINFO_H) && defined(HAVE_LIMITS_H)
30 #include <limits.h>
31 #include <sys/systeminfo.h>
32
33 int
34 sethostname (name, namelen)
35      char *name;
36      int namelen;
37 {
38   /* Using sysinfo() is the SVR4 mechanism to set a hostname. */
39   int result;
40
41   result = sysinfo (SI_SET_HOSTNAME, name, namelen);
42
43   return (result == -1 ? result : 0);
44 }
45
46 #define HAVE_SETHOSTNAME 1  /* Now we have it... */
47 #endif
48
49 char *xgethostname ();
50
51 /* The name this program was run with. */
52 char *program_name;
53
54 static void
55 usage (int status)
56 {
57   if (status != 0)
58     fprintf (stderr, _("Try `%s --help' for more information.\n"),
59              program_name);
60   else
61     {
62       printf (_("\
63 Usage: %s [NAME]\n\
64   or:  %s OPTION\n\
65 Print the hostname of the current system.\n\
66 \n\
67   --help      display this help and exit\n\
68   --version   output version information and exit\n\
69 ")
70              , program_name, program_name);
71       puts (_("\nReport bugs to <sh-utils-bugs@gnu.ai.mit.edu>."));
72     }
73   exit (status);
74 }
75
76 int
77 main (int argc, char **argv)
78 {
79   char *hostname;
80
81   program_name = argv[0];
82   setlocale (LC_ALL, "");
83   bindtextdomain (PACKAGE, LOCALEDIR);
84   textdomain (PACKAGE);
85
86   parse_long_options (argc, argv, "hostname", GNU_PACKAGE, VERSION, usage);
87
88 #ifdef HAVE_SETHOSTNAME
89   if (argc == 2)
90     {
91       int err;
92
93       /* Set hostname to argv[1].  */
94       err = sethostname (argv[1], strlen (argv[1]));
95       if (err != 0)
96         error (1, errno, "%s", argv[1]);
97       exit (0);
98     }
99 #else
100   if (argc == 2)
101     error (1, 0,
102            _("cannot set hostname; this system lacks the functionality"));
103 #endif
104
105   if (argc == 1)
106     {
107       hostname = xgethostname ();
108       if (hostname == NULL)
109         error (1, errno, _("cannot determine hostname"));
110       printf ("%s\n", hostname);
111     }
112   else
113     {
114       error (2, 0, _("too many arguments"));
115       usage (1);
116     }
117
118   exit (0);
119 }