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]
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>
44 char *Release = RELEASE,
45 *Version = "hostname 1.97 (1998-06-29)";
47 static char *program_name;
50 static void sethname(char *);
51 static void setdname(char *);
52 static void showhname(char *, int);
53 static void usage(void);
54 static void version(void);
55 static void setfilename(char *,int);
60 static void sethname(char *hname)
63 fprintf(stderr,_("Setting hostname to `%s'\n"),
65 if(sethostname(hname, strlen(hname))) {
68 fprintf(stderr,_("%s: you must be root to change the host name\n"), program_name);
71 fprintf(stderr,_("%s: name too long\n"), program_name);
79 static void setdname(char *dname)
82 fprintf(stderr,_("Setting domainname to `%s'\n"),
84 if(setdomainname(dname, strlen(dname))) {
87 fprintf(stderr,_("%s: you must be root to change the domain name\n"), program_name);
90 fprintf(stderr,_("%s: name too long\n"), program_name);
98 static void showhname(char *hname, int c)
101 register char *p,**alias;
105 fprintf(stderr,_("Resolving `%s' ...\n"),hname);
106 if (!(hp = gethostbyname(hname))) {
107 herror(program_name);
112 fprintf(stderr,_("Result: h_name=`%s'\n"),
117 fprintf(stderr,_("Result: h_aliases=`%s'\n"),
120 ip=(struct in_addr **)hp->h_addr_list;
122 fprintf(stderr,_("Result: h_addr_list=`%s'\n"),
126 if (!(p = strchr(hp->h_name, '.')) && (c == 'd')) return;
130 while (hp->h_aliases[0])
131 printf("%s ", *hp->h_aliases++);
135 while (hp->h_addr_list[0])
136 printf("%s ", inet_ntoa(*(struct in_addr *) * hp->h_addr_list++));
143 printf("%s\n", hp->h_name);
146 if (p != NULL) *p = '\0';
147 printf("%s\n", hp->h_name);
153 static void setfilename(char * name,int what)
157 char fline[MAXHOSTNAMELEN];
159 if ((fd = fopen(name, "r")) != NULL) {
160 while (fgets(fline, sizeof(fline), fd) != NULL) {
161 if ((p = index(fline, '\n')) != NULL)
163 if (opt_v) fprintf(stderr, ">> %s\n", fline);
166 if (what == SETHOST) {
174 fprintf(stderr,_("%s: can't open `%s'\n"),
175 program_name, optarg);
180 static void version(void)
182 fprintf(stderr,"%s\n%s\n",Release,Version);
186 static void usage(void)
188 fprintf(stderr,_("Usage: hostname [-v] {hostname|-F file} set hostname (from file)\n"));
189 fprintf(stderr,_(" domainname [-v] {nisdomain|-F file} set NIS domainname (from file)\n"));
190 fprintf(stderr,_(" hostname [-v] [-d|-f|-s|-a|-i|-y] display formated name\n"));
191 fprintf(stderr,_(" hostname [-v] display hostname\n\n"));
192 fprintf(stderr,_(" hostname -V|--version|-h|--help print info and exit\n\n"));
193 fprintf(stderr,_(" dnsdomainname=hostname -d, {yp,nis,}domainname=hostname -y\n\n"));
194 fprintf(stderr,_(" -s, --short short host name\n"));
195 fprintf(stderr,_(" -a, --alias alias names\n"));
196 fprintf(stderr,_(" -i, --ip-address addresses for the hostname\n"));
197 fprintf(stderr,_(" -f, --fqdn, --long long host name (FQDN)\n"));
198 fprintf(stderr,_(" -d, --domain DNS domain name\n"));
199 fprintf(stderr,_(" -y, --yp, --nis NIS/YP domainname\n"));
200 fprintf(stderr,_(" -F, --file read hostname or nis domainname from given File\n\n"));
201 fprintf(stderr,_(" This comand can get or set the hostname or the NIS domainname. You can\n"));
202 fprintf(stderr,_(" also get the DNS domain or the FQDN (fully qualified domain name).\n"));
203 fprintf(stderr,_(" Unless you are using bind or NIS for host lookups you can change the\n"));
204 fprintf(stderr,_(" FQDN (Fully Qualified Domain Name) and the DNS domain name (which is\n"));
205 fprintf(stderr,_(" part of the FQDN) in the /etc/hosts file.\n"));
210 int main(int argc, char **argv)
214 int option_index = 0;
216 char myname[MAXHOSTNAMELEN+1] = { 0 };
219 static const struct option long_options[] =
221 {"domain", no_argument, 0, 'd'},
222 {"file", required_argument, 0, 'F'},
223 {"fqdn", no_argument, 0, 'f'},
224 {"help", no_argument, 0, 'h'},
225 {"long", no_argument, 0, 'f'},
226 {"short", no_argument, 0, 's'},
227 {"version", no_argument, 0, 'V'},
228 {"verbose", no_argument, 0, 'v'},
229 {"alias", no_argument, 0, 'a'},
230 {"ip-address", no_argument, 0, 'i'},
231 {"nis", no_argument, 0, 'y'},
232 {"yp", no_argument, 0, 'y'},
236 bindtextdomain("net-tools", "/usr/share/locale");
237 textdomain("net-tools");
239 program_name = (rindex(argv[0], '/')) ? rindex(argv[0], '/') + 1 : argv[0];
241 if (!strcmp(program_name,"ypdomainname") ||
242 !strcmp(program_name,"domainname") ||
243 !strcmp(program_name,"nisdomainname"))
245 if (!strcmp(program_name,"dnsdomainname"))
248 while((c = getopt_long(argc, argv, "adfF:h?isVvy", long_options, &option_index)) != EOF)
282 if (file || (optind < argc)) {
283 fprintf(stderr,_("%s: You can't change the DNS domain name with this command\n"), program_name);
284 fprintf(stderr,_("\nUnless you are using bind or NIS for host lookups you can change the DNS\n"));
285 fprintf(stderr,_("domain name (which is part of the FQDN) in the /etc/hosts file.\n"));
292 setfilename(file,SETHOST);
296 sethname(argv[optind]);
300 gethostname(myname, sizeof(myname));
302 fprintf(stderr,_("gethostname()=`%s'\n"),myname);
304 printf("%s\n",myname);
306 showhname(myname,type);
310 setfilename(file,SETDOMAIN);
314 setdname(argv[optind]);
317 getdomainname(myname,sizeof(myname));
319 fprintf(stderr,_("getdomainname()=`%s'\n"),myname);
320 printf("%s\n",myname);