blob: 77b545dedbae65b5a4a16f48650f33d05ee34a11 [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 Kraaibf181b92000-07-16 20:57:15 +00003 * $Id: hostname.c,v 1.12 2000/07/16 20:57:15 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
26#include "internal.h"
27#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 {
Erik Andersene49d5ec2000-02-08 19:58:47 +000049 if ((f = fopen(s, "r")) == NULL) {
50 perror(s);
51 exit(1);
52 } else {
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("sethostname");
59 exit(1);
60 }
61 }
Eric Andersen485b9551999-12-07 23:14:59 +000062 }
Eric Andersen485b9551999-12-07 23:14:59 +000063}
64
65int hostname_main(int argc, char **argv)
66{
Erik Andersene49d5ec2000-02-08 19:58:47 +000067 int opt_short = 0;
68 int opt_domain = 0;
69 int opt_ip = 0;
70 struct hostent *h;
71 char *filename = NULL;
72 char buf[255];
73 char *s = NULL;
Eric Andersen485b9551999-12-07 23:14:59 +000074
Erik Andersene49d5ec2000-02-08 19:58:47 +000075 if (argc < 1)
Eric Andersend29edf31999-12-08 04:13:44 +000076 usage(hostname_usage);
Eric Andersen485b9551999-12-07 23:14:59 +000077
Erik Andersene49d5ec2000-02-08 19:58:47 +000078 while (--argc > 0 && **(++argv) == '-') {
79 while (*(++(*argv))) {
80 switch (**argv) {
81 case 's':
82 opt_short = 1;
83 break;
84 case 'i':
85 opt_ip = 1;
86 break;
87 case 'd':
88 opt_domain = 1;
89 break;
90 case 'F':
Erik Andersene49d5ec2000-02-08 19:58:47 +000091 if (--argc == 0) {
92 usage(hostname_usage);
93 }
94 filename = *(++argv);
95 break;
96 default:
97 usage(hostname_usage);
98 }
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;
115 printf("%s\n", buf);
116 } else if (opt_domain) {
117 s = strchr(buf, '.');
118 printf("%s\n", (s ? s + 1 : ""));
119 } else if (opt_ip) {
120 h = gethostbyname(buf);
121 if (!h) {
122 printf("Host not found\n");
123 exit(1);
124 }
125 printf("%s\n", inet_ntoa(*(struct in_addr *) (h->h_addr)));
126 } else {
127 printf("%s\n", buf);
128 }
129 }
Eric Andersenb6106152000-06-19 17:25:40 +0000130 return(0);
Eric Andersen485b9551999-12-07 23:14:59 +0000131}