blob: 44d529c83fc287e2625fc658bdbe5b282d6b61fc [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersen485b9551999-12-07 23:14:59 +00002/*
Eric Andersen0d5835a2000-10-12 22:30:31 +00003 * $Id: hostname.c,v 1.15 2000/10/12 22:30:31 andersen Exp $
Eric Andersen485b9551999-12-07 23:14:59 +00004 * Mini hostname implementation for busybox
5 *
6 * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
7 *
Eric Andersend29edf31999-12-08 04:13:44 +00008 * adjusted by Erik Andersen <andersee@debian.org> to remove
9 * use of long options and GNU getopt. Improved the usage info.
10 *
Eric Andersen485b9551999-12-07 23:14:59 +000011 * 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
Eric Andersen3570a342000-09-25 21:45:58 +000026#include "busybox.h"
Eric Andersen485b9551999-12-07 23:14:59 +000027#include <errno.h>
Eric Andersen485b9551999-12-07 23:14:59 +000028#include <arpa/inet.h>
29#include <netdb.h>
30#include <unistd.h>
31#include <stdio.h>
32
Eric Andersen485b9551999-12-07 23:14:59 +000033void do_sethostname(char *s, int isfile)
34{
Erik Andersene49d5ec2000-02-08 19:58:47 +000035 FILE *f;
36 char buf[255];
37
38 if (!s)
39 return;
40 if (!isfile) {
41 if (sethostname(s, strlen(s)) < 0) {
42 if (errno == EPERM)
Matt Kraaid537a952000-07-14 01:51:25 +000043 errorMsg("you must be root to change the hostname\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +000044 else
45 perror("sethostname");
46 exit(1);
47 }
Eric Andersen485b9551999-12-07 23:14:59 +000048 } else {
Matt Kraaibbaef662000-09-27 02:43:35 +000049 f = xfopen(s, "r");
50 fgets(buf, 255, f);
51 fclose(f);
52 if (buf[strlen(buf) - 1] == '\n')
53 buf[strlen(buf) - 1] = 0;
54 if (sethostname(buf, strlen(buf)) < 0) {
55 perror("sethostname");
Erik Andersene49d5ec2000-02-08 19:58:47 +000056 exit(1);
Erik Andersene49d5ec2000-02-08 19:58:47 +000057 }
Eric Andersen485b9551999-12-07 23:14:59 +000058 }
Eric Andersen485b9551999-12-07 23:14:59 +000059}
60
61int hostname_main(int argc, char **argv)
62{
Erik Andersene49d5ec2000-02-08 19:58:47 +000063 int opt_short = 0;
64 int opt_domain = 0;
65 int opt_ip = 0;
66 struct hostent *h;
67 char *filename = NULL;
68 char buf[255];
69 char *s = NULL;
Eric Andersen485b9551999-12-07 23:14:59 +000070
Erik Andersene49d5ec2000-02-08 19:58:47 +000071 if (argc < 1)
Eric Andersend29edf31999-12-08 04:13:44 +000072 usage(hostname_usage);
Eric Andersen485b9551999-12-07 23:14:59 +000073
Erik Andersene49d5ec2000-02-08 19:58:47 +000074 while (--argc > 0 && **(++argv) == '-') {
75 while (*(++(*argv))) {
76 switch (**argv) {
77 case 's':
78 opt_short = 1;
79 break;
80 case 'i':
81 opt_ip = 1;
82 break;
83 case 'd':
84 opt_domain = 1;
85 break;
86 case 'F':
Erik Andersene49d5ec2000-02-08 19:58:47 +000087 if (--argc == 0) {
88 usage(hostname_usage);
89 }
90 filename = *(++argv);
91 break;
Eric Andersen0d5835a2000-10-12 22:30:31 +000092 case '-':
93 if (strcmp(++(*argv), "file") || --argc ==0 ) {
94 usage(hostname_usage);
95 }
96 filename = *(++argv);
97 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +000098 default:
99 usage(hostname_usage);
100 }
101 if (filename != NULL)
102 break;
103 }
104 }
105
106 if (argc >= 1) {
107 do_sethostname(*argv, 0);
108 } else if (filename != NULL) {
109 do_sethostname(filename, 1);
110 } else {
111 gethostname(buf, 255);
112 if (opt_short) {
113 s = strchr(buf, '.');
114 if (!s)
115 s = buf;
116 *s = 0;
117 printf("%s\n", buf);
118 } else if (opt_domain) {
119 s = strchr(buf, '.');
120 printf("%s\n", (s ? s + 1 : ""));
121 } else if (opt_ip) {
122 h = gethostbyname(buf);
123 if (!h) {
124 printf("Host not found\n");
125 exit(1);
126 }
127 printf("%s\n", inet_ntoa(*(struct in_addr *) (h->h_addr)));
128 } else {
129 printf("%s\n", buf);
130 }
131 }
Eric Andersenb6106152000-06-19 17:25:40 +0000132 return(0);
Eric Andersen485b9551999-12-07 23:14:59 +0000133}