Most .c files (AUTHORS): Revert the WRITTEN_BY/AUTHORS change
[platform/upstream/coreutils.git] / src / hostname.c
1 /* hostname - set or print the name of current host system
2    Copyright (C) 1994-1997, 1999-2003 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 /* Written by Jim Meyering.  */
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 /* The official name of this program (e.g., no `g' prefix).  */
29 #define PROGRAM_NAME "hostname"
30
31 #define AUTHORS "Jim Meyering"
32
33 #if !defined(HAVE_SETHOSTNAME) && defined(HAVE_SYSINFO) && \
34      defined (HAVE_SYS_SYSTEMINFO_H) && defined(HAVE_LIMITS_H)
35 # include <sys/systeminfo.h>
36
37 int
38 sethostname (name, namelen)
39      char *name;
40      int namelen;
41 {
42   /* Using sysinfo() is the SVR4 mechanism to set a hostname. */
43   int result;
44
45   result = sysinfo (SI_SET_HOSTNAME, name, namelen);
46
47   return (result == -1 ? result : 0);
48 }
49
50 # define HAVE_SETHOSTNAME 1  /* Now we have it... */
51 #endif
52
53 char *xgethostname ();
54
55 /* The name this program was run with. */
56 char *program_name;
57
58 void
59 usage (int status)
60 {
61   if (status != 0)
62     fprintf (stderr, _("Try `%s --help' for more information.\n"),
63              program_name);
64   else
65     {
66       printf (_("\
67 Usage: %s [NAME]\n\
68   or:  %s OPTION\n\
69 Print or set the hostname of the current system.\n\
70 \n\
71 "),
72              program_name, program_name);
73       fputs (HELP_OPTION_DESCRIPTION, stdout);
74       fputs (VERSION_OPTION_DESCRIPTION, stdout);
75       printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
76     }
77   exit (status);
78 }
79
80 int
81 main (int argc, char **argv)
82 {
83   char *hostname;
84
85   initialize_main (&argc, &argv);
86   program_name = argv[0];
87   setlocale (LC_ALL, "");
88   bindtextdomain (PACKAGE, LOCALEDIR);
89   textdomain (PACKAGE);
90
91   atexit (close_stdout);
92
93   parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
94                       usage, AUTHORS, NULL);
95
96 #ifdef HAVE_SETHOSTNAME
97   if (argc == 2)
98     {
99       int err;
100
101       /* Set hostname to argv[1].  */
102       err = sethostname (argv[1], strlen (argv[1]));
103       if (err != 0)
104         error (EXIT_FAILURE, errno, _("cannot set hostname to `%s'"), argv[1]);
105       exit (EXIT_SUCCESS);
106     }
107 #else
108   if (argc == 2)
109     error (EXIT_FAILURE, 0,
110            _("cannot set hostname; this system lacks the functionality"));
111 #endif
112
113   if (argc <= 1)
114     {
115       hostname = xgethostname ();
116       if (hostname == NULL)
117         error (EXIT_FAILURE, errno, _("cannot determine hostname"));
118       printf ("%s\n", hostname);
119     }
120   else
121     {
122       error (2, 0, _("too many arguments"));
123       usage (EXIT_FAILURE);
124     }
125
126   exit (EXIT_SUCCESS);
127 }