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>
48 #include <netdnet/dn.h>
51 char *Release
= RELEASE
, *Version
= "hostname 1.100 (2001-04-14)";
53 static char *program_name
;
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);
68 static void setnname(char *nname
)
71 fprintf(stderr
, _("Setting nodename to `%s'\n"),
73 if (setnodename(nname
, strlen(nname
))) {
76 fprintf(stderr
, _("%s: you must be root to change the node name\n"), program_name
);
79 fprintf(stderr
, _("%s: name too long\n"), program_name
);
87 #endif /* HAVE_AFDECnet */
89 static void sethname(char *hname
)
92 fprintf(stderr
, _("Setting hostname to `%s'\n"),
94 if (sethostname(hname
, strlen(hname
))) {
97 fprintf(stderr
, _("%s: you must be root to change the host name\n"), program_name
);
100 fprintf(stderr
, _("%s: name too long\n"), program_name
);
107 static void setdname(char *dname
)
110 fprintf(stderr
, _("Setting domainname to `%s'\n"),
112 if (setdomainname(dname
, strlen(dname
))) {
115 fprintf(stderr
, _("%s: you must be root to change the domain name\n"), program_name
);
118 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
);
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 setlocale (LC_ALL
, "");
284 bindtextdomain("net-tools", "/usr/share/locale");
285 textdomain("net-tools");
287 program_name
= (rindex(argv
[0], '/')) ? rindex(argv
[0], '/') + 1 : argv
[0];
289 if (!strcmp(program_name
, "ypdomainname") ||
290 !strcmp(program_name
, "domainname") ||
291 !strcmp(program_name
, "nisdomainname"))
293 if (!strcmp(program_name
, "dnsdomainname"))
296 if (!strcmp(program_name
, "nodename"))
298 #endif /* HAVE_AFDECnet */
300 while ((c
= getopt_long(argc
, argv
, "adfF:h?isVvyn", long_options
, &option_index
)) != EOF
)
319 #endif /* HAVE_AFDECnet */
328 break; // not reached
333 break; // not reached
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 */