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