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