ce17ba91cb751a301cadbf7ce3d5dc099ee01b7f
[oweals/busybox.git] / hostname.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * $Id: hostname.c,v 1.23 2001/02/01 16:49:29 kraai Exp $
4  * Mini hostname implementation for busybox
5  *
6  * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
7  *
8  * adjusted by Erik Andersen <andersee@debian.org> to remove
9  * use of long options and GNU getopt.  Improved the usage info.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24  */
25
26 #include "busybox.h"
27 #include <errno.h>
28 #include <arpa/inet.h>
29 #include <netdb.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <stdio.h>
33
34 void do_sethostname(char *s, int isfile)
35 {
36         FILE *f;
37         char buf[255];
38
39         if (!s)
40                 return;
41         if (!isfile) {
42                 if (sethostname(s, strlen(s)) < 0) {
43                         if (errno == EPERM)
44                                 error_msg_and_die("you must be root to change the hostname");
45                         else
46                                 perror_msg_and_die("sethostname");
47                 }
48         } else {
49                 f = xfopen(s, "r");
50                 fgets(buf, 255, f);
51                 fclose(f);
52                 chomp(buf);
53                 if (sethostname(buf, strlen(buf)) < 0)
54                         perror_msg_and_die("sethostname");
55         }
56 }
57
58 int hostname_main(int argc, char **argv)
59 {
60         int opt_short = 0;
61         int opt_domain = 0;
62         int opt_ip = 0;
63         struct hostent *h;
64         char *filename = NULL;
65         char buf[255];
66         char *s = NULL;
67
68         if (argc < 1)
69                 usage(hostname_usage);
70
71         while (--argc > 0 && **(++argv) == '-') {
72                 while (*(++(*argv))) {
73                         switch (**argv) {
74                         case 's':
75                                 opt_short = 1;
76                                 break;
77                         case 'i':
78                                 opt_ip = 1;
79                                 break;
80                         case 'd':
81                                 opt_domain = 1;
82                                 break;
83                         case 'F':
84                                 if (--argc == 0) {
85                                         usage(hostname_usage);
86                                 }
87                                 filename = *(++argv);
88                                 break;
89                         case '-':
90                                 if (strcmp(++(*argv), "file") || --argc ==0 ) {
91                                         usage(hostname_usage);
92                                 }
93                                 filename = *(++argv);
94                                 break;
95                         default:
96                                 usage(hostname_usage);
97                         }
98                         if (filename != NULL)
99                                 break;
100                 }
101         }
102
103         if (argc >= 1) {
104                 do_sethostname(*argv, 0);
105         } else if (filename != NULL) {
106                 do_sethostname(filename, 1);
107         } else {
108                 gethostname(buf, 255);
109                 if (opt_short) {
110                         s = strchr(buf, '.');
111                         if (!s)
112                                 s = buf;
113                         *s = 0;
114                         printf("%s\n", buf);
115                 } else if (opt_domain) {
116                         s = strchr(buf, '.');
117                         printf("%s\n", (s ? s + 1 : ""));
118                 } else if (opt_ip) {
119                         h = gethostbyname(buf);
120                         if (!h) {
121                                 printf("Host not found\n");
122                                 exit(1);
123                         }
124                         printf("%s\n", inet_ntoa(*(struct in_addr *) (h->h_addr)));
125                 } else {
126                         printf("%s\n", buf);
127                 }
128         }
129         return(0);
130 }