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]
8 * hostname {name|-F file}
10 * nisdomainname {name|-F file}
12 * Version: hostname 1.96 (1996-02-18)
14 * Author: Peter Tobias <tobias@et-inf.fho-emden.de>
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/
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
26 * This program is free software; you can redistribute it
27 * and/or modify it under the terms of the GNU General
28 * Public License as published by the Free Software
29 * Foundation; either version 2 of the License, or (at
30 * your option) any later version.
38 #include <sys/param.h>
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
46 #include <netdnet/dn.h>
49 char *Release = RELEASE, *Version = "hostname 1.99 (2000-02-13)";
51 static char *program_name;
54 static void sethname(char *);
55 static void setdname(char *);
56 static void showhname(char *, int);
57 static void usage(void);
58 static void version(void);
59 static void setfilename(char *, int);
66 static void setnname(char *nname)
69 fprintf(stderr, _("Setting nodename to `%s'\n"),
71 if (setnodename(nname, strlen(nname))) {
74 fprintf(stderr, _("%s: you must be root to change the node name\n"), program_name);
77 fprintf(stderr, _("%s: name too long\n"), program_name);
84 #endif /* HAVE_AFDECnet */
86 static void sethname(char *hname)
89 fprintf(stderr, _("Setting hostname to `%s'\n"),
91 if (sethostname(hname, strlen(hname))) {
94 fprintf(stderr, _("%s: you must be root to change the host name\n"), program_name);
97 fprintf(stderr, _("%s: name too long\n"), program_name);
105 static void setdname(char *dname)
108 fprintf(stderr, _("Setting domainname to `%s'\n"),
110 if (setdomainname(dname, strlen(dname))) {
113 fprintf(stderr, _("%s: you must be root to change the domain name\n"), program_name);
116 fprintf(stderr, _("%s: name too long\n"), program_name);
124 static void showhname(char *hname, int c)
127 register char *p, **alias;
131 fprintf(stderr, _("Resolving `%s' ...\n"), hname);
132 if (!(hp = gethostbyname(hname))) {
133 herror(program_name);
137 fprintf(stderr, _("Result: h_name=`%s'\n"),
140 alias = hp->h_aliases;
142 fprintf(stderr, _("Result: h_aliases=`%s'\n"),
145 ip = (struct in_addr **) hp->h_addr_list;
147 fprintf(stderr, _("Result: h_addr_list=`%s'\n"),
150 if (!(p = strchr(hp->h_name, '.')) && (c == 'd'))
155 while (hp->h_aliases[0])
156 printf("%s ", *hp->h_aliases++);
160 while (hp->h_addr_list[0])
161 printf("%s ", inet_ntoa(*(struct in_addr *) *hp->h_addr_list++));
168 printf("%s\n", hp->h_name);
173 printf("%s\n", hp->h_name);
179 static void setfilename(char *name, int what)
183 char fline[MAXHOSTNAMELEN];
185 if ((fd = fopen(name, "r")) != NULL) {
186 while (fgets(fline, sizeof(fline), fd) != NULL) {
187 if ((p = index(fline, '\n')) != NULL)
190 fprintf(stderr, ">> %s\n", fline);
204 #endif /* HAVE_AFDECnet */
209 fprintf(stderr, _("%s: can't open `%s'\n"),
215 static void version(void)
217 fprintf(stderr, "%s\n%s\n", Release, Version);
218 exit(5); /* E_VERSION */
221 static void usage(void)
223 fprintf(stderr, _("Usage: hostname [-v] {hostname|-F file} set hostname (from file)\n"));
224 fprintf(stderr, _(" domainname [-v] {nisdomain|-F file} set NIS domainname (from file)\n"));
226 fprintf(stderr, _(" nodename [-v] {nodename|-F file} set DECnet node name (from file)\n"));
228 fprintf(stderr, _(" hostname [-v] [-d|-f|-s|-a|-i|-y|-n] display formatted name\n"));
229 fprintf(stderr, _(" hostname [-v] display hostname\n\n"));
230 fprintf(stderr, _(" hostname -V|--version|-h|--help print info and exit\n\n"));
231 fprintf(stderr, _(" dnsdomainname=hostname -d, {yp,nis,}domainname=hostname -y\n\n"));
232 fprintf(stderr, _(" -s, --short short host name\n"));
233 fprintf(stderr, _(" -a, --alias alias names\n"));
234 fprintf(stderr, _(" -i, --ip-address addresses for the hostname\n"));
235 fprintf(stderr, _(" -f, --fqdn, --long long host name (FQDN)\n"));
236 fprintf(stderr, _(" -d, --domain DNS domain name\n"));
237 fprintf(stderr, _(" -y, --yp, --nis NIS/YP domainname\n"));
239 fprintf(stderr, _(" -n, --node DECnet node name\n"));
240 #endif /* HAVE_AFDECnet */
241 fprintf(stderr, _(" -F, --file read hostname or NIS domainname from given file\n\n"));
243 " This command can read or set the hostname or the NIS domainname. You can\n"
244 " also read the DNS domain or the FQDN (fully qualified domain name).\n"
245 " Unless you are using bind or NIS for host lookups you can change the\n"
246 " FQDN (Fully Qualified Domain Name) and the DNS domain name (which is\n"
247 " part of the FQDN) in the /etc/hosts file.\n"));
249 exit(4); /* E_USAGE */
253 int main(int argc, char **argv)
257 int option_index = 0;
259 char myname[MAXHOSTNAMELEN + 1] =
263 static const struct option long_options[] =
265 {"domain", no_argument, 0, 'd'},
266 {"file", required_argument, 0, 'F'},
267 {"fqdn", no_argument, 0, 'f'},
268 {"help", no_argument, 0, 'h'},
269 {"long", no_argument, 0, 'f'},
270 {"short", no_argument, 0, 's'},
271 {"version", no_argument, 0, 'V'},
272 {"verbose", no_argument, 0, 'v'},
273 {"alias", no_argument, 0, 'a'},
274 {"ip-address", no_argument, 0, 'i'},
275 {"nis", no_argument, 0, 'y'},
276 {"yp", no_argument, 0, 'y'},
278 {"node", no_argument, 0, 'n'},
279 #endif /* HAVE_AFDECnet */
283 bindtextdomain("net-tools", "/usr/share/locale");
284 textdomain("net-tools");
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"))
292 if (!strcmp(program_name, "dnsdomainname"))
295 if (!strcmp(program_name, "nodename"))
297 #endif /* HAVE_AFDECnet */
299 while ((c = getopt_long(argc, argv, "adfF:h?isVvyn", long_options, &option_index)) != EOF)
318 #endif /* HAVE_AFDECnet */
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"));
347 setfilename(file, SETHOST);
351 sethname(argv[optind]);
355 gethostname(myname, sizeof(myname));
357 fprintf(stderr, _("gethostname()=`%s'\n"), myname);
359 printf("%s\n", myname);
361 showhname(myname, type);
365 setfilename(file, SETDOMAIN);
369 setdname(argv[optind]);
372 getdomainname(myname, sizeof(myname));
374 fprintf(stderr, _("getdomainname()=`%s'\n"), myname);
375 printf("%s\n", myname);
380 setfilename(file, SETNODE);
384 setnname(argv[optind]);
387 getnodename(myname, sizeof(myname));
389 fprintf(stderr, _("getnodename()=`%s'\n"), myname);
390 printf("%s\n", myname);
392 #endif /* HAVE_AFDECnet */