Replace outdated NLS support with GNU gettext (patch from
[platform/upstream/net-tools.git] / hostname.c
1 /*
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.
5  *
6  * Usage:       hostname [-d|-f|-s|-a|-i|-y]
7  *              hostname [-h|-V]
8  *              hostname {name|-F file}
9  *              dnsdmoainname   
10  *              nisdomainname {name|-F file}
11  *
12  * Version:     hostname 1.96 (1996-02-18)
13  *
14  * Author:      Peter Tobias <tobias@et-inf.fho-emden.de>
15  *
16  * Changes:
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/
21  *                                      setdomainname added 
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  *
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.
30  */
31 #include <stdio.h>
32 #include <unistd.h>
33 #include <getopt.h>
34 #include <string.h>
35 #include <netdb.h>
36 #include <errno.h>
37 #include <sys/param.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
40 #include "config.h"
41 #include "version.h"
42 #include "../intl.h"
43
44 char *Release = RELEASE,
45      *Version = "hostname 1.97 (1998-06-29)";
46      
47 static char *program_name;
48 static int  opt_v;
49
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);
56
57 #define SETHOST         1
58 #define SETDOMAIN       2
59
60 static void sethname(char *hname)
61 {
62         if (opt_v)
63                 fprintf(stderr,_("Setting hostname to `%s'\n"),
64                         hname);
65         if(sethostname(hname, strlen(hname))) {
66                 switch(errno) {
67                         case EPERM:
68                                 fprintf(stderr,_("%s: you must be root to change the host name\n"), program_name);
69                                 break;
70                         case EINVAL:
71                                 fprintf(stderr,_("%s: name too long\n"), program_name);
72                                 break;
73                         default:
74                 }
75                 exit(1);
76         };
77 }
78
79 static void setdname(char *dname)
80 {
81         if (opt_v)
82                 fprintf(stderr,_("Setting domainname to `%s'\n"),
83                         dname);
84         if(setdomainname(dname, strlen(dname))) {
85                 switch(errno) {
86                         case EPERM:
87                                 fprintf(stderr,_("%s: you must be root to change the domain name\n"), program_name);
88                                 break;
89                         case EINVAL:
90                                 fprintf(stderr,_("%s: name too long\n"), program_name);
91                                 break;
92                         default:
93                 }
94                 exit(1);
95         };
96 }
97
98 static void showhname(char *hname, int c)
99 {
100         struct hostent *hp;
101         register char *p,**alias;
102         struct in_addr **ip;
103         
104         if (opt_v)
105                 fprintf(stderr,_("Resolving `%s' ...\n"),hname);
106         if (!(hp = gethostbyname(hname))) {
107                 herror(program_name);
108                 exit(1);
109         }
110
111         if (opt_v) { 
112                 fprintf(stderr,_("Result: h_name=`%s'\n"),
113                         hp->h_name);
114                         
115                 alias=hp->h_aliases;
116                 while(alias[0])
117                         fprintf(stderr,_("Result: h_aliases=`%s'\n"),
118                                 *alias++);
119                                 
120                 ip=(struct in_addr **)hp->h_addr_list;
121                 while(ip[0])
122                         fprintf(stderr,_("Result: h_addr_list=`%s'\n"),
123                                 inet_ntoa(**ip++));
124         }
125
126         if (!(p = strchr(hp->h_name, '.')) && (c == 'd')) return;
127
128         switch(c) {
129                 case 'a':
130                         while (hp->h_aliases[0])
131                                 printf("%s ", *hp->h_aliases++);
132                         printf("\n");
133                         break;
134                 case 'i':
135                         while (hp->h_addr_list[0])
136                                 printf("%s ", inet_ntoa(*(struct in_addr *) * hp->h_addr_list++));
137                         printf("\n");
138                         break;
139                 case 'd':
140                         printf("%s\n", ++p);
141                         break;
142                 case 'f':
143                         printf("%s\n", hp->h_name);
144                         break;
145                 case 's':
146                         if (p != NULL) *p = '\0';
147                         printf("%s\n", hp->h_name);
148                         break;
149                 default:
150         }
151 }
152
153 static void setfilename(char * name,int what)
154 {               
155   register FILE *fd;
156   register char *p;
157   char fline[MAXHOSTNAMELEN];
158
159   if ((fd = fopen(name, "r")) != NULL) {
160         while (fgets(fline, sizeof(fline), fd) != NULL) {
161                 if ((p = index(fline, '\n')) != NULL)
162                         *p = '\0';
163                         if (opt_v) fprintf(stderr, ">> %s\n", fline);
164                         if (fline[0] == '#')
165                                 continue;
166                         if (what == SETHOST) {
167                                 sethname(fline);
168                         } else {
169                                 setdname(fline);
170                         }
171         }
172         (void) fclose(fd);
173   } else {
174         fprintf(stderr,_("%s: can't open `%s'\n"),
175                         program_name, optarg);
176         exit(1);
177   }
178 }
179
180 static void version(void)
181 {
182         fprintf(stderr,"%s\n%s\n",Release,Version);
183         exit(-1);
184 }
185
186 static void usage(void)
187 {
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"));
206         exit(-1);
207 }
208
209
210 int main(int argc, char **argv)
211 {
212         int c;
213         char type='\0';
214         int option_index = 0;
215         int what = 0;
216         char myname[MAXHOSTNAMELEN+1] = { 0 };
217         char *file=NULL;
218
219         static const struct option long_options[] =
220         {
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'},
233                 {0, 0, 0, 0}
234         };
235 #if I18N
236         bindtextdomain("net-tools", "/usr/share/locale");
237         textdomain("net-tools");
238 #endif
239         program_name = (rindex(argv[0], '/')) ? rindex(argv[0], '/') + 1 : argv[0];
240
241         if (!strcmp(program_name,"ypdomainname") || 
242             !strcmp(program_name,"domainname")   || 
243             !strcmp(program_name,"nisdomainname"))
244                 what = 3;
245         if (!strcmp(program_name,"dnsdomainname"))
246                 what = 2;
247         
248         while((c = getopt_long(argc, argv, "adfF:h?isVvy", long_options, &option_index)) != EOF)
249         switch(c)
250         {
251                 case 'd':
252                         what = 2;
253                         break;
254                 case 'a':
255                 case 'f':
256                 case 'i':
257                 case 's':
258                         what = 1;
259                         type=c;
260                         break;
261                 case 'y':
262                         what = 3;
263                         break;
264                 case 'F':
265                         file=optarg;                                            
266                         break;
267                 case 'v':
268                         opt_v++;
269                         break;
270                 case 'V':
271                         version();
272                 case '?':
273                 case 'h':
274                 default:
275                         usage();
276
277         };
278
279
280         switch(what) {
281                 case 2:
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"));
286                                 exit(1);
287                         }
288                         type='d';                       
289                         /* NOBREAK */
290                 case 0:
291                         if (file) {
292                                 setfilename(file,SETHOST);
293                                 break;
294                         }
295                         if (optind < argc) {
296                                 sethname(argv[optind]);
297                                 break;
298                         }
299                 case 1:
300                         gethostname(myname, sizeof(myname));
301                         if (opt_v)
302                                 fprintf(stderr,_("gethostname()=`%s'\n"),myname);
303                         if (!type)
304                                 printf("%s\n",myname);
305                         else
306                                 showhname(myname,type);
307                         break;
308                 case 3:
309                         if (file) {
310                                 setfilename(file,SETDOMAIN);
311                                 break;
312                         }
313                         if (optind < argc) {
314                                 setdname(argv[optind]);
315                                 break;
316                         }
317                         getdomainname(myname,sizeof(myname));
318                         if (opt_v)
319                                 fprintf(stderr,_("getdomainname()=`%s'\n"),myname);
320                         printf("%s\n",myname);
321                         break;
322         }
323         exit(0);
324 }