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
25 * This program is free software; you can redistribute it
26 * and/or modify it under the terms of the GNU General
27 * Public License as published by the Free Software
28 * Foundation; either version 2 of the License, or (at
29 * your option) any later version.
37 #include <sys/param.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
45 #include <netdnet/dn.h>
48 char *Release = RELEASE, *Version = "hostname 1.98 (1998-02-27)";
50 static char *program_name;
53 static void sethname(char *);
54 static void setdname(char *);
55 static void showhname(char *, int);
56 static void usage(void);
57 static void version(void);
58 static void setfilename(char *, int);
65 static void setnname(char *nname)
68 fprintf(stderr, _("Setting nodename to `%s'\n"),
70 if (setnodename(nname, strlen(nname))) {
73 fprintf(stderr, _("%s: you must be root to change the node name\n"), program_name);
76 fprintf(stderr, _("%s: name too long\n"), program_name);
83 #endif /* HAVE_AFDECnet */
85 static void sethname(char *hname)
88 fprintf(stderr, _("Setting hostname to `%s'\n"),
90 if (sethostname(hname, strlen(hname))) {
93 fprintf(stderr, _("%s: you must be root to change the host name\n"), program_name);
96 fprintf(stderr, _("%s: name too long\n"), program_name);
104 static void setdname(char *dname)
107 fprintf(stderr, _("Setting domainname to `%s'\n"),
109 if (setdomainname(dname, strlen(dname))) {
112 fprintf(stderr, _("%s: you must be root to change the domain name\n"), program_name);
115 fprintf(stderr, _("%s: name too long\n"), program_name);
123 static void showhname(char *hname, int c)
126 register char *p, **alias;
130 fprintf(stderr, _("Resolving `%s' ...\n"), hname);
131 if (!(hp = gethostbyname(hname))) {
132 herror(program_name);
136 fprintf(stderr, _("Result: h_name=`%s'\n"),
139 alias = hp->h_aliases;
141 fprintf(stderr, _("Result: h_aliases=`%s'\n"),
144 ip = (struct in_addr **) hp->h_addr_list;
146 fprintf(stderr, _("Result: h_addr_list=`%s'\n"),
149 if (!(p = strchr(hp->h_name, '.')) && (c == 'd'))
154 while (hp->h_aliases[0])
155 printf("%s ", *hp->h_aliases++);
159 while (hp->h_addr_list[0])
160 printf("%s ", inet_ntoa(*(struct in_addr *) *hp->h_addr_list++));
167 printf("%s\n", hp->h_name);
172 printf("%s\n", hp->h_name);
178 static void setfilename(char *name, int what)
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)
189 fprintf(stderr, ">> %s\n", fline);
203 #endif /* HAVE_AFDECnet */
208 fprintf(stderr, _("%s: can't open `%s'\n"),
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"));
225 fprintf(stderr, _(" nodename [-v] {nodename|-F file} set DECnet node name (from file)\n"));
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"));
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, _(" This comand can get or set the hostname or the NIS domainname. You can\n"));
242 fprintf(stderr, _(" also get the DNS domain or the FQDN (fully qualified domain name).\n"));
243 fprintf(stderr, _(" Unless you are using bind or NIS for host lookups you can change the\n"));
244 fprintf(stderr, _(" FQDN (Fully Qualified Domain Name) and the DNS domain name (which is\n"));
245 fprintf(stderr, _(" part of the FQDN) in the /etc/hosts file.\n"));
246 exit(4); /* E_USAGE */
250 int main(int argc, char **argv)
254 int option_index = 0;
256 char myname[MAXHOSTNAMELEN + 1] =
260 static const struct option long_options[] =
262 {"domain", no_argument, 0, 'd'},
263 {"file", required_argument, 0, 'F'},
264 {"fqdn", no_argument, 0, 'f'},
265 {"help", no_argument, 0, 'h'},
266 {"long", no_argument, 0, 'f'},
267 {"short", no_argument, 0, 's'},
268 {"version", no_argument, 0, 'V'},
269 {"verbose", no_argument, 0, 'v'},
270 {"alias", no_argument, 0, 'a'},
271 {"ip-address", no_argument, 0, 'i'},
272 {"nis", no_argument, 0, 'y'},
273 {"yp", no_argument, 0, 'y'},
275 {"node", no_argument, 0, 'n'},
276 #endif /* HAVE_AFDECnet */
280 bindtextdomain("net-tools", "/usr/share/locale");
281 textdomain("net-tools");
283 program_name = (rindex(argv[0], '/')) ? rindex(argv[0], '/') + 1 : argv[0];
285 if (!strcmp(program_name, "ypdomainname") ||
286 !strcmp(program_name, "domainname") ||
287 !strcmp(program_name, "nisdomainname"))
289 if (!strcmp(program_name, "dnsdomainname"))
292 if (!strcmp(program_name, "nodename"))
294 #endif /* HAVE_AFDECnet */
296 while ((c = getopt_long(argc, argv, "adfF:h?isVvyn", long_options, &option_index)) != EOF)
315 #endif /* HAVE_AFDECnet */
334 if (file || (optind < argc)) {
335 fprintf(stderr, _("%s: You can't change the DNS domain name with this command\n"), program_name);
336 fprintf(stderr, _("\nUnless you are using bind or NIS for host lookups you can change the DNS\n"));
337 fprintf(stderr, _("domain name (which is part of the FQDN) in the /etc/hosts file.\n"));
344 setfilename(file, SETHOST);
348 sethname(argv[optind]);
352 gethostname(myname, sizeof(myname));
354 fprintf(stderr, _("gethostname()=`%s'\n"), myname);
356 printf("%s\n", myname);
358 showhname(myname, type);
362 setfilename(file, SETDOMAIN);
366 setdname(argv[optind]);
369 getdomainname(myname, sizeof(myname));
371 fprintf(stderr, _("getdomainname()=`%s'\n"), myname);
372 printf("%s\n", myname);
377 setfilename(file, SETNODE);
381 setnname(argv[optind]);
384 getnodename(myname, sizeof(myname));
386 fprintf(stderr, _("getnodename()=`%s'\n"), myname);
387 printf("%s\n", myname);
389 #endif /* HAVE_AFDECnet */