Warn about apps that will be going away in release 0.50
[oweals/busybox.git] / hostname.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * $Id: hostname.c,v 1.18 2001/01/22 22:48:42 andersen 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 #warning This applet has moved to netkit-tiny.  After BusyBox 0.49, this
27 #warning applet will be removed from BusyBox.  All maintainence efforts
28 #warning should be done in the netkit-tiny source tree.
29
30 #include "busybox.h"
31 #include <errno.h>
32 #include <arpa/inet.h>
33 #include <netdb.h>
34 #include <unistd.h>
35 #include <stdio.h>
36
37 void do_sethostname(char *s, int isfile)
38 {
39         FILE *f;
40         char buf[255];
41
42         if (!s)
43                 return;
44         if (!isfile) {
45                 if (sethostname(s, strlen(s)) < 0) {
46                         if (errno == EPERM)
47                                 error_msg_and_die("you must be root to change the hostname\n");
48                         else
49                                 perror_msg_and_die("sethostname");
50                 }
51         } else {
52                 f = xfopen(s, "r");
53                 fgets(buf, 255, f);
54                 fclose(f);
55                 if (buf[strlen(buf) - 1] == '\n')
56                         buf[strlen(buf) - 1] = 0;
57                 if (sethostname(buf, strlen(buf)) < 0)
58                         perror_msg_and_die("sethostname");
59         }
60 }
61
62 int hostname_main(int argc, char **argv)
63 {
64         int opt_short = 0;
65         int opt_domain = 0;
66         int opt_ip = 0;
67         struct hostent *h;
68         char *filename = NULL;
69         char buf[255];
70         char *s = NULL;
71
72         if (argc < 1)
73                 usage(hostname_usage);
74
75         while (--argc > 0 && **(++argv) == '-') {
76                 while (*(++(*argv))) {
77                         switch (**argv) {
78                         case 's':
79                                 opt_short = 1;
80                                 break;
81                         case 'i':
82                                 opt_ip = 1;
83                                 break;
84                         case 'd':
85                                 opt_domain = 1;
86                                 break;
87                         case 'F':
88                                 if (--argc == 0) {
89                                         usage(hostname_usage);
90                                 }
91                                 filename = *(++argv);
92                                 break;
93                         case '-':
94                                 if (strcmp(++(*argv), "file") || --argc ==0 ) {
95                                         usage(hostname_usage);
96                                 }
97                                 filename = *(++argv);
98                                 break;
99                         default:
100                                 usage(hostname_usage);
101                         }
102                         if (filename != NULL)
103                                 break;
104                 }
105         }
106
107         if (argc >= 1) {
108                 do_sethostname(*argv, 0);
109         } else if (filename != NULL) {
110                 do_sethostname(filename, 1);
111         } else {
112                 gethostname(buf, 255);
113                 if (opt_short) {
114                         s = strchr(buf, '.');
115                         if (!s)
116                                 s = buf;
117                         *s = 0;
118                         printf("%s\n", buf);
119                 } else if (opt_domain) {
120                         s = strchr(buf, '.');
121                         printf("%s\n", (s ? s + 1 : ""));
122                 } else if (opt_ip) {
123                         h = gethostbyname(buf);
124                         if (!h) {
125                                 printf("Host not found\n");
126                                 exit(1);
127                         }
128                         printf("%s\n", inet_ntoa(*(struct in_addr *) (h->h_addr)));
129                 } else {
130                         printf("%s\n", buf);
131                 }
132         }
133         return(0);
134 }