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
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.
39 #include <sys/param.h>
40 #include <netinet/in.h>
41 #include <arpa/inet.h>
47 #include <netdnet/dn.h>
50 char *Release
= RELEASE
, *Version
= "hostname 1.100 (2001-04-14)";
52 static char *program_name
;
55 static void sethname(char *);
56 static void setdname(char *);
57 static void showhname(char *, int);
58 static void usage(void);
59 static void version(void);
60 static void setfilename(char *, int);
67 static void setnname(char *nname
)
70 fprintf(stderr
, _("Setting nodename to `%s'\n"),
72 if (setnodename(nname
, strlen(nname
))) {
75 fprintf(stderr
, _("%s: you must be root to change the node name\n"), program_name
);
78 fprintf(stderr
, _("%s: name too long\n"), program_name
);
85 #endif /* HAVE_AFDECnet */
87 static void sethname(char *hname
)
90 fprintf(stderr
, _("Setting hostname to `%s'\n"),
92 if (sethostname(hname
, strlen(hname
))) {
95 fprintf(stderr
, _("%s: you must be root to change the host name\n"), program_name
);
98 fprintf(stderr
, _("%s: name too long\n"), program_name
);
106 static void setdname(char *dname
)
109 fprintf(stderr
, _("Setting domainname to `%s'\n"),
111 if (setdomainname(dname
, strlen(dname
))) {
114 fprintf(stderr
, _("%s: you must be root to change the domain name\n"), program_name
);
117 fprintf(stderr
, _("%s: name too long\n"), program_name
);
125 static void showhname(char *hname
, int c
)
128 register char *p
, **alias
;
132 fprintf(stderr
, _("Resolving `%s' ...\n"), hname
);
133 if (!(hp
= gethostbyname(hname
))) {
134 herror(program_name
);
138 fprintf(stderr
, _("Result: h_name=`%s'\n"),
141 alias
= hp
->h_aliases
;
143 fprintf(stderr
, _("Result: h_aliases=`%s'\n"),
146 ip
= (struct in_addr
**) hp
->h_addr_list
;
148 fprintf(stderr
, _("Result: h_addr_list=`%s'\n"),
151 if (!(p
= strchr(hp
->h_name
, '.')) && (c
== 'd'))
156 while (hp
->h_aliases
[0])
157 printf("%s ", *hp
->h_aliases
++);
161 while (hp
->h_addr_list
[0])
162 printf("%s ", inet_ntoa(*(struct in_addr
*) *hp
->h_addr_list
++));
169 printf("%s\n", hp
->h_name
);
174 printf("%s\n", hp
->h_name
);
180 static void setfilename(char *name
, int what
)
184 char fline
[MAXHOSTNAMELEN
];
186 if ((fd
= fopen(name
, "r")) != NULL
) {
187 while (fgets(fline
, sizeof(fline
), fd
) != NULL
) {
188 if ((p
= index(fline
, '\n')) != NULL
)
191 fprintf(stderr
, ">> %s\n", fline
);
205 #endif /* HAVE_AFDECnet */
210 fprintf(stderr
, _("%s: can't open `%s'\n"),
216 static void version(void)
218 fprintf(stderr
, "%s\n%s\n", Release
, Version
);
219 exit(5); /* E_VERSION */
222 static void usage(void)
224 fprintf(stderr
, _("Usage: hostname [-v] {hostname|-F file} set hostname (from file)\n"));
225 fprintf(stderr
, _(" domainname [-v] {nisdomain|-F file} set NIS domainname (from file)\n"));
227 fprintf(stderr
, _(" nodename [-v] {nodename|-F file} set DECnet node name (from file)\n"));
229 fprintf(stderr
, _(" hostname [-v] [-d|-f|-s|-a|-i|-y|-n] display formatted name\n"));
230 fprintf(stderr
, _(" hostname [-v] display hostname\n\n"));
231 fprintf(stderr
, _(" hostname -V|--version|-h|--help print info and exit\n\n"));
232 fprintf(stderr
, _(" dnsdomainname=hostname -d, {yp,nis,}domainname=hostname -y\n\n"));
233 fprintf(stderr
, _(" -s, --short short host name\n"));
234 fprintf(stderr
, _(" -a, --alias alias names\n"));
235 fprintf(stderr
, _(" -i, --ip-address addresses for the hostname\n"));
236 fprintf(stderr
, _(" -f, --fqdn, --long long host name (FQDN)\n"));
237 fprintf(stderr
, _(" -d, --domain DNS domain name\n"));
238 fprintf(stderr
, _(" -y, --yp, --nis NIS/YP domainname\n"));
240 fprintf(stderr
, _(" -n, --node DECnet node name\n"));
241 #endif /* HAVE_AFDECnet */
242 fprintf(stderr
, _(" -F, --file read hostname or NIS domainname from given file\n\n"));
244 " This command can read or set the hostname or the NIS domainname. You can\n"
245 " also read the DNS domain or the FQDN (fully qualified domain name).\n"
246 " Unless you are using bind or NIS for host lookups you can change the\n"
247 " FQDN (Fully Qualified Domain Name) and the DNS domain name (which is\n"
248 " part of the FQDN) in the /etc/hosts file.\n"));
250 exit(4); /* E_USAGE */
254 int main(int argc
, char **argv
)
258 int option_index
= 0;
260 char myname
[MAXHOSTNAMELEN
+ 1] =
264 static const struct option long_options
[] =
266 {"domain", no_argument
, 0, 'd'},
267 {"file", required_argument
, 0, 'F'},
268 {"fqdn", no_argument
, 0, 'f'},
269 {"help", no_argument
, 0, 'h'},
270 {"long", no_argument
, 0, 'f'},
271 {"short", no_argument
, 0, 's'},
272 {"version", no_argument
, 0, 'V'},
273 {"verbose", no_argument
, 0, 'v'},
274 {"alias", no_argument
, 0, 'a'},
275 {"ip-address", no_argument
, 0, 'i'},
276 {"nis", no_argument
, 0, 'y'},
277 {"yp", no_argument
, 0, 'y'},
279 {"node", no_argument
, 0, 'n'},
280 #endif /* HAVE_AFDECnet */
284 setlocale (LC_ALL
, "");
285 bindtextdomain("net-tools", "/usr/share/locale");
286 textdomain("net-tools");
288 program_name
= (rindex(argv
[0], '/')) ? rindex(argv
[0], '/') + 1 : argv
[0];
290 if (!strcmp(program_name
, "ypdomainname") ||
291 !strcmp(program_name
, "domainname") ||
292 !strcmp(program_name
, "nisdomainname"))
294 if (!strcmp(program_name
, "dnsdomainname"))
297 if (!strcmp(program_name
, "nodename"))
299 #endif /* HAVE_AFDECnet */
301 while ((c
= getopt_long(argc
, argv
, "adfF:h?isVvyn", long_options
, &option_index
)) != EOF
)
320 #endif /* HAVE_AFDECnet */
339 if (file
|| (optind
< argc
)) {
340 fprintf(stderr
, _("%s: You can't change the DNS domain name with this command\n"), program_name
);
341 fprintf(stderr
, _("\nUnless you are using bind or NIS for host lookups you can change the DNS\n"));
342 fprintf(stderr
, _("domain name (which is part of the FQDN) in the /etc/hosts file.\n"));
349 setfilename(file
, SETHOST
);
353 sethname(argv
[optind
]);
357 gethostname(myname
, sizeof(myname
));
359 fprintf(stderr
, _("gethostname()=`%s'\n"), myname
);
361 printf("%s\n", myname
);
363 showhname(myname
, type
);
367 setfilename(file
, SETDOMAIN
);
371 setdname(argv
[optind
]);
374 getdomainname(myname
, sizeof(myname
));
376 fprintf(stderr
, _("getdomainname()=`%s'\n"), myname
);
377 printf("%s\n", myname
);
382 setfilename(file
, SETNODE
);
386 setnname(argv
[optind
]);
389 getnodename(myname
, sizeof(myname
));
391 fprintf(stderr
, _("getnodename()=`%s'\n"), myname
);
392 printf("%s\n", myname
);
394 #endif /* HAVE_AFDECnet */