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.101 (2003-10-11)
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 (major rewrite), usage.
19 *19960120 {1.95} Bernd Eckenfels : -y/nisdomainname - support for get/setdomainname added
20 *19960218 {1.96} Bernd Eckenfels : netinet/in.h added
21 *19980629 {1.97} Arnaldo Carvalho de Melo : gettext instead of catgets for i18n
22 *20000213 {1.99} Arnaldo Carvalho de Melo : fixed some i18n strings
23 *20010404 {1.100} Arnaldo Carvalho de Melo: use setlocale
24 *20031011 {1.101} Maik Broemme: gcc 3.x fixes (default: break)
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.
40 #include <sys/param.h>
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
45 #include "net-support.h"
49 #include <sys/socket.h> /* for PF_INET6 */
50 #include <sys/types.h> /* for inet_ntop */
54 #include <netdnet/dn.h>
57 char *Release = RELEASE, *Version = "hostname 1.100 (2001-04-14)";
59 static char *program_name;
62 static void sethname(char *);
63 static void setdname(char *);
64 static void showhname(char *, int);
65 static void usage(void);
66 static void version(void);
67 static void setfilename(char *, int);
74 static void setnname(char *nname)
77 fprintf(stderr, _("Setting nodename to `%s'\n"),
79 if (setnodename(nname, strlen(nname))) {
82 fprintf(stderr, _("%s: you must be root to change the node name\n"), program_name);
85 fprintf(stderr, _("%s: name too long\n"), program_name);
93 #endif /* HAVE_AFDECnet */
95 static void sethname(char *hname)
98 fprintf(stderr, _("Setting hostname to `%s'\n"),
100 if (sethostname(hname, strlen(hname))) {
103 fprintf(stderr, _("%s: you must be root to change the host name\n"), program_name);
106 fprintf(stderr, _("%s: name too long\n"), program_name);
113 static void setdname(char *dname)
116 fprintf(stderr, _("Setting domainname to `%s'\n"),
118 if (setdomainname(dname, strlen(dname))) {
121 fprintf(stderr, _("%s: you must be root to change the domain name\n"), program_name);
124 fprintf(stderr, _("%s: name too long\n"), program_name);
131 static void showhname(char *hname, int c)
135 struct in6_addr **ip6;
137 register char *p, **alias;
141 fprintf(stderr, _("Resolving `%s' ...\n"), hname);
144 !(hp = gethostbyname2(hname, PF_INET6)) &&
146 !(hp = gethostbyname(hname))) {
147 herror(program_name);
152 fprintf(stderr, _("Result: h_name=`%s'\n"),
155 alias = hp->h_aliases;
157 fprintf(stderr, _("Result: h_aliases=`%s'\n"),
160 if (hp->h_addrtype == PF_INET6) {
161 char addr[INET6_ADDRSTRLEN + 1];
162 addr[INET6_ADDRSTRLEN] = '\0';
163 ip6 = (struct in6_addr **) hp->h_addr_list;
165 if (inet_ntop(PF_INET6, *ip6++, addr, INET6_ADDRSTRLEN))
166 fprintf(stderr, _("Result: h_addr_list=`%s'\n"), addr);
167 else if (errno == EAFNOSUPPORT)
168 fprintf(stderr, _("%s: protocol family not supported\n"),
170 else if (errno == ENOSPC)
171 fprintf(stderr, _("%s: name too long\n"), program_name);
176 ip = (struct in_addr **) hp->h_addr_list;
178 fprintf(stderr, _("Result: h_addr_list=`%s'\n"),
182 if (!(p = strchr(hp->h_name, '.')) && (c == 'd'))
187 while (hp->h_aliases[0])
188 printf("%s ", *hp->h_aliases++);
193 if (hp->h_addrtype == PF_INET6) {
194 char addr[INET6_ADDRSTRLEN + 1];
195 addr[INET6_ADDRSTRLEN] = '\0';
196 while (hp->h_addr_list[0]) {
197 if (inet_ntop(PF_INET6, (struct in6_addr *)*hp->h_addr_list++,
198 addr, INET6_ADDRSTRLEN)) {
200 } else if (errno == EAFNOSUPPORT) {
201 fprintf(stderr, _("\n%s: protocol family not supported\n"),
204 } else if (errno == ENOSPC) {
205 fprintf(stderr, _("\n%s: name too long\n"), program_name);
212 while (hp->h_addr_list[0])
213 printf("%s ", inet_ntoa(*(struct in_addr *)*hp->h_addr_list++));
221 printf("%s\n", hp->h_name);
226 printf("%s\n", hp->h_name);
231 static void setfilename(char *name, int what)
235 char fline[MAXHOSTNAMELEN];
237 if ((fd = fopen(name, "r")) != NULL) {
238 while (fgets(fline, sizeof(fline), fd) != NULL) {
239 if ((p = index(fline, '\n')) != NULL)
242 fprintf(stderr, ">> %s\n", fline);
256 #endif /* HAVE_AFDECnet */
261 fprintf(stderr, _("%s: can't open `%s'\n"),
267 static void version(void)
269 fprintf(stderr, "%s\n%s\n", Release, Version);
273 static void usage(void)
275 fprintf(stderr, _("Usage: hostname [-v] {hostname|-F file} set hostname (from file)\n"));
276 fprintf(stderr, _(" domainname [-v] {nisdomain|-F file} set NIS domainname (from file)\n"));
278 fprintf(stderr, _(" nodename [-v] {nodename|-F file} set DECnet node name (from file)\n"));
280 fprintf(stderr, _(" hostname [-v] [-d|-f|-s|-a|-i|-y|-n] display formatted name\n"));
281 fprintf(stderr, _(" hostname [-v] display hostname\n\n"));
282 fprintf(stderr, _(" hostname -V|--version|-h|--help print info and exit\n\n"));
283 fprintf(stderr, _(" dnsdomainname=hostname -d, {yp,nis,}domainname=hostname -y\n\n"));
284 fprintf(stderr, _(" -s, --short short host name\n"));
285 fprintf(stderr, _(" -a, --alias alias names\n"));
286 fprintf(stderr, _(" -i, --ip-address addresses for the hostname\n"));
287 fprintf(stderr, _(" -f, --fqdn, --long long host name (FQDN)\n"));
288 fprintf(stderr, _(" -d, --domain DNS domain name\n"));
289 fprintf(stderr, _(" -y, --yp, --nis NIS/YP domainname\n"));
291 fprintf(stderr, _(" -n, --node DECnet node name\n"));
292 #endif /* HAVE_AFDECnet */
293 fprintf(stderr, _(" -F, --file read hostname or NIS domainname from given file\n\n"));
295 " This command can read or set the hostname or the NIS domainname. You can\n"
296 " also read the DNS domain or the FQDN (fully qualified domain name).\n"
297 " Unless you are using bind or NIS for host lookups you can change the\n"
298 " FQDN (Fully Qualified Domain Name) and the DNS domain name (which is\n"
299 " part of the FQDN) in the /etc/hosts file.\n"));
305 int main(int argc, char **argv)
309 int option_index = 0;
311 char myname[MAXHOSTNAMELEN + 1] =
315 static const struct option long_options[] =
317 {"domain", no_argument, 0, 'd'},
318 {"file", required_argument, 0, 'F'},
319 {"fqdn", no_argument, 0, 'f'},
320 {"help", no_argument, 0, 'h'},
321 {"long", no_argument, 0, 'f'},
322 {"short", no_argument, 0, 's'},
323 {"version", no_argument, 0, 'V'},
324 {"verbose", no_argument, 0, 'v'},
325 {"alias", no_argument, 0, 'a'},
326 {"ip-address", no_argument, 0, 'i'},
327 {"nis", no_argument, 0, 'y'},
328 {"yp", no_argument, 0, 'y'},
330 {"node", no_argument, 0, 'n'},
331 #endif /* HAVE_AFDECnet */
335 setlocale (LC_ALL, "");
336 bindtextdomain("net-tools", "/usr/share/locale");
337 textdomain("net-tools");
339 program_name = (rindex(argv[0], '/')) ? rindex(argv[0], '/') + 1 : argv[0];
341 if (!strcmp(program_name, "ypdomainname") ||
342 !strcmp(program_name, "domainname") ||
343 !strcmp(program_name, "nisdomainname"))
345 if (!strcmp(program_name, "dnsdomainname"))
348 if (!strcmp(program_name, "nodename"))
350 #endif /* HAVE_AFDECnet */
352 while ((c = getopt_long(argc, argv, "adfF:h?isVvyn", long_options, &option_index)) != EOF)
371 #endif /* HAVE_AFDECnet */
380 break; // not reached
385 break; // not reached
391 if (file || (optind < argc)) {
392 fprintf(stderr, _("%s: You can't change the DNS domain name with this command\n"), program_name);
393 fprintf(stderr, _("\nUnless you are using bind or NIS for host lookups you can change the DNS\n"));
394 fprintf(stderr, _("domain name (which is part of the FQDN) in the /etc/hosts file.\n"));
401 setfilename(file, SETHOST);
405 sethname(argv[optind]);
409 gethostname(myname, sizeof(myname));
411 fprintf(stderr, _("gethostname()=`%s'\n"), myname);
413 printf("%s\n", myname);
415 showhname(myname, type);
419 setfilename(file, SETDOMAIN);
423 setdname(argv[optind]);
426 if (getdomainname(myname, sizeof(myname)) < 0) {
427 perror("getdomainname()");
431 fprintf(stderr, _("getdomainname()=`%s'\n"), myname);
432 printf("%s\n", myname);
437 setfilename(file, SETNODE);
441 setnname(argv[optind]);
444 getnodename(myname, sizeof(myname));
446 fprintf(stderr, _("getnodename()=`%s'\n"), myname);
447 printf("%s\n", myname);
449 #endif /* HAVE_AFDECnet */