make hostname compile warnign free with gcc 3.3 (Ec)
[oss-qm-packages.git] / hostname.c
blob5573ee16cba3f180a6b59a9741a0de6b3e2ebf14
1 /*
2 * hostname This file contains an implementation of the command
3 * that maintains the hostname and the domainname. It
4 * is also used to show the FQDN and the IP-Addresses.
6 * Usage: hostname [-d|-f|-s|-a|-i|-y|-n]
7 * hostname [-h|-V]
8 * hostname {name|-F file}
9 * dnsdmoainname
10 * nisdomainname {name|-F file}
12 * Version: hostname 1.96 (1996-02-18)
14 * Author: Peter Tobias <tobias@et-inf.fho-emden.de>
16 * Changes:
17 * {1.90} Peter Tobias : Added -a and -i options.
18 * {1.91} Bernd Eckenfels : -v,-V rewritten, long_opts
19 * (major rewrite), usage.
20 *960120 {1.95} Bernd Eckenfels : -y/nisdomainname - support for get/
21 * setdomainname added
22 *960218 {1.96} Bernd Eckenfels : netinet/in.h added
23 *980629 {1.97} Arnaldo Carvalho de Melo : gettext instead of catgets for i18n
24 *20000213 {1.99} Arnaldo Carvalho de Melo : fixed some i18n strings
25 *20010404 {1.100} Arnaldo Carvalho de Melo: use setlocale
27 * This program is free software; you can redistribute it
28 * and/or modify it under the terms of the GNU General
29 * Public License as published by the Free Software
30 * Foundation; either version 2 of the License, or (at
31 * your option) any later version.
33 #include <stdio.h>
34 #include <unistd.h>
35 #include <stdlib.h>
36 #include <getopt.h>
37 #include <string.h>
38 #include <netdb.h>
39 #include <errno.h>
40 #include <sys/param.h>
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
43 #include "config.h"
44 #include "version.h"
45 #include "../intl.h"
47 #if HAVE_AFDECnet
48 #include <netdnet/dn.h>
49 #endif
51 char *Release = RELEASE, *Version = "hostname 1.100 (2001-04-14)";
53 static char *program_name;
54 static int opt_v;
56 static void sethname(char *);
57 static void setdname(char *);
58 static void showhname(char *, int);
59 static void usage(void);
60 static void version(void);
61 static void setfilename(char *, int);
63 #define SETHOST 1
64 #define SETDOMAIN 2
65 #define SETNODE 3
67 #if HAVE_AFDECnet
68 static void setnname(char *nname)
70 if (opt_v)
71 fprintf(stderr, _("Setting nodename to `%s'\n"),
72 nname);
73 if (setnodename(nname, strlen(nname))) {
74 switch(errno) {
75 case EPERM:
76 fprintf(stderr, _("%s: you must be root to change the node name\n"), program_name);
77 break;
78 case EINVAL:
79 fprintf(stderr, _("%s: name too long\n"), program_name);
80 break;
81 default:
83 exit(1);
86 #endif /* HAVE_AFDECnet */
88 static void sethname(char *hname)
90 if (opt_v)
91 fprintf(stderr, _("Setting hostname to `%s'\n"),
92 hname);
93 if (sethostname(hname, strlen(hname))) {
94 switch (errno) {
95 case EPERM:
96 fprintf(stderr, _("%s: you must be root to change the host name\n"), program_name);
97 break;
98 case EINVAL:
99 fprintf(stderr, _("%s: name too long\n"), program_name);
100 break;
102 exit(1);
106 static void setdname(char *dname)
108 if (opt_v)
109 fprintf(stderr, _("Setting domainname to `%s'\n"),
110 dname);
111 if (setdomainname(dname, strlen(dname))) {
112 switch (errno) {
113 case EPERM:
114 fprintf(stderr, _("%s: you must be root to change the domain name\n"), program_name);
115 break;
116 case EINVAL:
117 fprintf(stderr, _("%s: name too long\n"), program_name);
118 break;
120 exit(1);
124 static void showhname(char *hname, int c)
126 struct hostent *hp;
127 register char *p, **alias;
128 struct in_addr **ip;
130 if (opt_v)
131 fprintf(stderr, _("Resolving `%s' ...\n"), hname);
132 if (!(hp = gethostbyname(hname))) {
133 herror(program_name);
134 exit(1);
136 if (opt_v) {
137 fprintf(stderr, _("Result: h_name=`%s'\n"),
138 hp->h_name);
140 alias = hp->h_aliases;
141 while (alias[0])
142 fprintf(stderr, _("Result: h_aliases=`%s'\n"),
143 *alias++);
145 ip = (struct in_addr **) hp->h_addr_list;
146 while (ip[0])
147 fprintf(stderr, _("Result: h_addr_list=`%s'\n"),
148 inet_ntoa(**ip++));
150 if (!(p = strchr(hp->h_name, '.')) && (c == 'd'))
151 return;
153 switch (c) {
154 case 'a':
155 while (hp->h_aliases[0])
156 printf("%s ", *hp->h_aliases++);
157 printf("\n");
158 break;
159 case 'i':
160 while (hp->h_addr_list[0])
161 printf("%s ", inet_ntoa(*(struct in_addr *) *hp->h_addr_list++));
162 printf("\n");
163 break;
164 case 'd':
165 printf("%s\n", ++p);
166 break;
167 case 'f':
168 printf("%s\n", hp->h_name);
169 break;
170 case 's':
171 if (p != NULL)
172 *p = '\0';
173 printf("%s\n", hp->h_name);
174 break;
178 static void setfilename(char *name, int what)
180 register FILE *fd;
181 register char *p;
182 char fline[MAXHOSTNAMELEN];
184 if ((fd = fopen(name, "r")) != NULL) {
185 while (fgets(fline, sizeof(fline), fd) != NULL) {
186 if ((p = index(fline, '\n')) != NULL)
187 *p = '\0';
188 if (opt_v)
189 fprintf(stderr, ">> %s\n", fline);
190 if (fline[0] == '#')
191 continue;
192 switch(what) {
193 case SETHOST:
194 sethname(fline);
195 break;
196 case SETDOMAIN:
197 setdname(fline);
198 break;
199 #if HAVE_AFDECnet
200 case SETNODE:
201 setnname(fline);
202 break;
203 #endif /* HAVE_AFDECnet */
206 (void) fclose(fd);
207 } else {
208 fprintf(stderr, _("%s: can't open `%s'\n"),
209 program_name, name);
210 exit(1);
214 static void version(void)
216 fprintf(stderr, "%s\n%s\n", Release, Version);
217 exit(5); /* E_VERSION */
220 static void usage(void)
222 fprintf(stderr, _("Usage: hostname [-v] {hostname|-F file} set hostname (from file)\n"));
223 fprintf(stderr, _(" domainname [-v] {nisdomain|-F file} set NIS domainname (from file)\n"));
224 #if HAVE_AFDECnet
225 fprintf(stderr, _(" nodename [-v] {nodename|-F file} set DECnet node name (from file)\n"));
226 #endif
227 fprintf(stderr, _(" hostname [-v] [-d|-f|-s|-a|-i|-y|-n] display formatted name\n"));
228 fprintf(stderr, _(" hostname [-v] display hostname\n\n"));
229 fprintf(stderr, _(" hostname -V|--version|-h|--help print info and exit\n\n"));
230 fprintf(stderr, _(" dnsdomainname=hostname -d, {yp,nis,}domainname=hostname -y\n\n"));
231 fprintf(stderr, _(" -s, --short short host name\n"));
232 fprintf(stderr, _(" -a, --alias alias names\n"));
233 fprintf(stderr, _(" -i, --ip-address addresses for the hostname\n"));
234 fprintf(stderr, _(" -f, --fqdn, --long long host name (FQDN)\n"));
235 fprintf(stderr, _(" -d, --domain DNS domain name\n"));
236 fprintf(stderr, _(" -y, --yp, --nis NIS/YP domainname\n"));
237 #if HAVE_AFDECnet
238 fprintf(stderr, _(" -n, --node DECnet node name\n"));
239 #endif /* HAVE_AFDECnet */
240 fprintf(stderr, _(" -F, --file read hostname or NIS domainname from given file\n\n"));
241 fprintf(stderr, _(
242 " This command can read or set the hostname or the NIS domainname. You can\n"
243 " also read the DNS domain or the FQDN (fully qualified domain name).\n"
244 " Unless you are using bind or NIS for host lookups you can change the\n"
245 " FQDN (Fully Qualified Domain Name) and the DNS domain name (which is\n"
246 " part of the FQDN) in the /etc/hosts file.\n"));
248 exit(4); /* E_USAGE */
252 int main(int argc, char **argv)
254 int c;
255 char type = '\0';
256 int option_index = 0;
257 int what = 0;
258 char myname[MAXHOSTNAMELEN + 1] =
259 {0};
260 char *file = NULL;
262 static const struct option long_options[] =
264 {"domain", no_argument, 0, 'd'},
265 {"file", required_argument, 0, 'F'},
266 {"fqdn", no_argument, 0, 'f'},
267 {"help", no_argument, 0, 'h'},
268 {"long", no_argument, 0, 'f'},
269 {"short", no_argument, 0, 's'},
270 {"version", no_argument, 0, 'V'},
271 {"verbose", no_argument, 0, 'v'},
272 {"alias", no_argument, 0, 'a'},
273 {"ip-address", no_argument, 0, 'i'},
274 {"nis", no_argument, 0, 'y'},
275 {"yp", no_argument, 0, 'y'},
276 #if HAVE_AFDECnet
277 {"node", no_argument, 0, 'n'},
278 #endif /* HAVE_AFDECnet */
279 {0, 0, 0, 0}
281 #if I18N
282 setlocale (LC_ALL, "");
283 bindtextdomain("net-tools", "/usr/share/locale");
284 textdomain("net-tools");
285 #endif
286 program_name = (rindex(argv[0], '/')) ? rindex(argv[0], '/') + 1 : argv[0];
288 if (!strcmp(program_name, "ypdomainname") ||
289 !strcmp(program_name, "domainname") ||
290 !strcmp(program_name, "nisdomainname"))
291 what = 3;
292 if (!strcmp(program_name, "dnsdomainname"))
293 what = 2;
294 #if HAVE_AFDECnet
295 if (!strcmp(program_name, "nodename"))
296 what = 4;
297 #endif /* HAVE_AFDECnet */
299 while ((c = getopt_long(argc, argv, "adfF:h?isVvyn", long_options, &option_index)) != EOF)
300 switch (c) {
301 case 'd':
302 what = 2;
303 break;
304 case 'a':
305 case 'f':
306 case 'i':
307 case 's':
308 what = 1;
309 type = c;
310 break;
311 case 'y':
312 what = 3;
313 break;
314 #if HAVE_AFDECnet
315 case 'n':
316 what = 4;
317 break;
318 #endif /* HAVE_AFDECnet */
319 case 'F':
320 file = optarg;
321 break;
322 case 'v':
323 opt_v++;
324 break;
325 case 'V':
326 version();
327 case '?':
328 case 'h':
329 default:
330 usage();
335 switch (what) {
336 case 2:
337 if (file || (optind < argc)) {
338 fprintf(stderr, _("%s: You can't change the DNS domain name with this command\n"), program_name);
339 fprintf(stderr, _("\nUnless you are using bind or NIS for host lookups you can change the DNS\n"));
340 fprintf(stderr, _("domain name (which is part of the FQDN) in the /etc/hosts file.\n"));
341 exit(1);
343 type = 'd';
344 /* NOBREAK */
345 case 0:
346 if (file) {
347 setfilename(file, SETHOST);
348 break;
350 if (optind < argc) {
351 sethname(argv[optind]);
352 break;
354 case 1:
355 gethostname(myname, sizeof(myname));
356 if (opt_v)
357 fprintf(stderr, _("gethostname()=`%s'\n"), myname);
358 if (!type)
359 printf("%s\n", myname);
360 else
361 showhname(myname, type);
362 break;
363 case 3:
364 if (file) {
365 setfilename(file, SETDOMAIN);
366 break;
368 if (optind < argc) {
369 setdname(argv[optind]);
370 break;
372 getdomainname(myname, sizeof(myname));
373 if (opt_v)
374 fprintf(stderr, _("getdomainname()=`%s'\n"), myname);
375 printf("%s\n", myname);
376 break;
377 #if HAVE_AFDECnet
378 case 4:
379 if (file) {
380 setfilename(file, SETNODE);
381 break;
383 if (optind < argc) {
384 setnname(argv[optind]);
385 break;
387 getnodename(myname, sizeof(myname));
388 if (opt_v)
389 fprintf(stderr, _("getnodename()=`%s'\n"), myname);
390 printf("%s\n", myname);
391 break;
392 #endif /* HAVE_AFDECnet */
394 exit(0);