(add_tabstop): Give correct size when reallocating tab_list buffer.
[platform/upstream/coreutils.git] / src / hostname.c
1 /* hostname - set or print the name of current host system
2    Copyright (C) 94, 1995 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
16    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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 "version.h"
26 #include "long-options.h"
27 #include "error.h"
28
29 #if !defined(HAVE_SETHOSTNAME) && defined(HAVE_SYSINFO) && \
30      defined (HAVE_SYS_SYSTEMINFO_H) && defined(HAVE_LIMITS_H)
31 #include <limits.h>
32 #include <sys/systeminfo.h>
33
34 int
35 sethostname (name, namelen)
36      char *name;
37      int namelen;
38 {
39   /* Using sysinfo() is the SVR4 mechanism to set a hostname. */
40   int result;
41   
42   result = sysinfo (SI_SET_HOSTNAME, name, namelen);
43   
44   return (result == -1 ? result : 0);
45 }
46
47 #define HAVE_SETHOSTNAME 1  /* Now we have it... */
48 #endif
49
50 char *xgethostname ();
51
52 /* The name this program was run with. */
53 char *program_name;
54
55 static void
56 usage (status)
57      int status;
58 {
59   if (status != 0)
60     fprintf (stderr, "Try `%s --help' for more information.\n",
61              program_name);
62   else
63     {
64       printf ("\
65 Usage: %s [NAME]\n\
66   or:  %s OPTION\n\
67 \n\
68   --help      display this help and exit\n\
69   --version   output version information and exit\n\
70 "
71              , program_name, program_name);
72     }
73   exit (status);
74 }
75
76 void
77 main (argc, argv)
78      int argc;
79      char **argv;
80 {
81   char *hostname;
82
83   program_name = argv[0];
84
85   parse_long_options (argc, argv, "hostname", version_string, usage);
86
87 #ifdef HAVE_SETHOSTNAME
88   if (argc == 2)
89     {
90       int err;
91
92       /* Set hostname to argv[1].  */
93       err = sethostname (argv[1], strlen (argv[1]));
94       if (err != 0)
95         error (1, errno, "%s", argv[1]);
96       exit (0);
97     }
98 #else
99   if (argc == 2)
100     error (1, 0, "cannot set hostname; this system lacks the functionality");
101 #endif
102
103   if (argc == 1)
104     {
105       hostname = xgethostname ();
106       if (hostname == NULL)
107         error (1, errno, "cannot determine hostname");
108       printf ("%s\n", hostname);
109     }
110   else
111     {
112       error (2, 0, "too many arguments");
113       usage (1);
114     }
115
116   exit (0);
117 }