blob: 75e4d2e574e9cc635dca8112f745e33badf1af05 [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/*
Matt Kraaic55b8d42001-05-16 15:40:51 +00003 * $Id: hostname.c,v 1.28 2001/05/16 15:40:48 kraai 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>
Eric Andersened3ef502001-01-27 08:24:39 +000031#include <string.h>
Eric Andersen485b9551999-12-07 23:14:59 +000032#include <stdio.h>
Eric Anderseneba8ed72001-03-09 14:36:42 +000033#include <stdlib.h>
Eric Andersen485b9551999-12-07 23:14:59 +000034
Eric Andersen3e6ff902001-03-09 21:24:12 +000035static void do_sethostname(char *s, int isfile)
Eric Andersen485b9551999-12-07 23:14:59 +000036{
Erik Andersene49d5ec2000-02-08 19:58:47 +000037 FILE *f;
38 char buf[255];
39
40 if (!s)
41 return;
42 if (!isfile) {
43 if (sethostname(s, strlen(s)) < 0) {
44 if (errno == EPERM)
Matt Kraaidd19c692001-01-31 19:00:21 +000045 error_msg_and_die("you must be root to change the hostname");
Erik Andersene49d5ec2000-02-08 19:58:47 +000046 else
Matt Kraaia9819b22000-12-22 01:48:07 +000047 perror_msg_and_die("sethostname");
Erik Andersene49d5ec2000-02-08 19:58:47 +000048 }
Eric Andersen485b9551999-12-07 23:14:59 +000049 } else {
Matt Kraaibbaef662000-09-27 02:43:35 +000050 f = xfopen(s, "r");
51 fgets(buf, 255, f);
52 fclose(f);
Matt Kraai05e782d2001-02-01 16:49:30 +000053 chomp(buf);
Matt Kraaia9819b22000-12-22 01:48:07 +000054 if (sethostname(buf, strlen(buf)) < 0)
55 perror_msg_and_die("sethostname");
Eric Andersen485b9551999-12-07 23:14:59 +000056 }
Eric Andersen485b9551999-12-07 23:14:59 +000057}
58
59int hostname_main(int argc, char **argv)
60{
Erik Andersene49d5ec2000-02-08 19:58:47 +000061 int opt_short = 0;
62 int opt_domain = 0;
63 int opt_ip = 0;
64 struct hostent *h;
65 char *filename = NULL;
66 char buf[255];
67 char *s = NULL;
Eric Andersen485b9551999-12-07 23:14:59 +000068
Erik Andersene49d5ec2000-02-08 19:58:47 +000069 if (argc < 1)
Eric Andersen67991cf2001-02-14 21:23:06 +000070 show_usage();
Eric Andersen485b9551999-12-07 23:14:59 +000071
Erik Andersene49d5ec2000-02-08 19:58:47 +000072 while (--argc > 0 && **(++argv) == '-') {
73 while (*(++(*argv))) {
74 switch (**argv) {
75 case 's':
76 opt_short = 1;
77 break;
78 case 'i':
79 opt_ip = 1;
80 break;
81 case 'd':
82 opt_domain = 1;
83 break;
84 case 'F':
Erik Andersene49d5ec2000-02-08 19:58:47 +000085 if (--argc == 0) {
Eric Andersen67991cf2001-02-14 21:23:06 +000086 show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +000087 }
88 filename = *(++argv);
89 break;
Eric Andersen0d5835a2000-10-12 22:30:31 +000090 case '-':
91 if (strcmp(++(*argv), "file") || --argc ==0 ) {
Eric Andersen67991cf2001-02-14 21:23:06 +000092 show_usage();
Eric Andersen0d5835a2000-10-12 22:30:31 +000093 }
94 filename = *(++argv);
95 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +000096 default:
Eric Andersen67991cf2001-02-14 21:23:06 +000097 show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +000098 }
99 if (filename != NULL)
100 break;
101 }
102 }
103
104 if (argc >= 1) {
105 do_sethostname(*argv, 0);
106 } else if (filename != NULL) {
107 do_sethostname(filename, 1);
108 } else {
109 gethostname(buf, 255);
110 if (opt_short) {
111 s = strchr(buf, '.');
112 if (!s)
113 s = buf;
114 *s = 0;
Matt Kraai59df6f72001-05-16 14:21:09 +0000115 puts(buf);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000116 } else if (opt_domain) {
117 s = strchr(buf, '.');
Matt Kraai59df6f72001-05-16 14:21:09 +0000118 puts(s ? s + 1 : "");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000119 } else if (opt_ip) {
Matt Kraaic55b8d42001-05-16 15:40:51 +0000120 h = xgethostbyname(buf);
Matt Kraai59df6f72001-05-16 14:21:09 +0000121 puts(inet_ntoa(*(struct in_addr *) (h->h_addr)));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000122 } else {
Matt Kraai59df6f72001-05-16 14:21:09 +0000123 puts(buf);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000124 }
125 }
Eric Andersenb6106152000-06-19 17:25:40 +0000126 return(0);
Eric Andersen485b9551999-12-07 23:14:59 +0000127}