blob: 0df9fed75924427998d069ac71fd801b262ba0ea [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/*
Erik Andersen330fd2b2000-05-19 05:35:19 +00003 * $Id: hostname.c,v 1.9 2000/05/19 05:35:18 erik 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
Erik Andersene49d5ec2000-02-08 19:58:47 +000033static const char *hostname_usage =
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000034 "hostname [OPTION] {hostname | -F file}\n"
35#ifndef BB_FEATURE_TRIVIAL_HELP
36 "\nGet or set the hostname or DNS domain name. If a hostname is given\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +000037 "(or a file with the -F parameter), the host name will be set.\n\n"
38 "Options:\n"
39 "\t-s\t\tShort\n"
40
41 "\t-i\t\tAddresses for the hostname\n"
42 "\t-d\t\tDNS domain name\n"
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000043 "\t-F FILE\t\tUse the contents of FILE to specify the hostname\n"
44#endif
45 ;
Eric Andersen485b9551999-12-07 23:14:59 +000046
Eric Andersen485b9551999-12-07 23:14:59 +000047
48void do_sethostname(char *s, int isfile)
49{
Erik Andersene49d5ec2000-02-08 19:58:47 +000050 FILE *f;
51 char buf[255];
52
53 if (!s)
54 return;
55 if (!isfile) {
56 if (sethostname(s, strlen(s)) < 0) {
57 if (errno == EPERM)
58 fprintf(stderr,
59 "hostname: you must be root to change the hostname\n");
60 else
61 perror("sethostname");
62 exit(1);
63 }
Eric Andersen485b9551999-12-07 23:14:59 +000064 } else {
Erik Andersene49d5ec2000-02-08 19:58:47 +000065 if ((f = fopen(s, "r")) == NULL) {
66 perror(s);
67 exit(1);
68 } else {
69 fgets(buf, 255, f);
70 fclose(f);
71 if (buf[strlen(buf) - 1] == '\n')
72 buf[strlen(buf) - 1] = 0;
73 if (sethostname(buf, strlen(buf)) < 0) {
74 perror("sethostname");
75 exit(1);
76 }
77 }
Eric Andersen485b9551999-12-07 23:14:59 +000078 }
Eric Andersen485b9551999-12-07 23:14:59 +000079}
80
81int hostname_main(int argc, char **argv)
82{
Erik Andersene49d5ec2000-02-08 19:58:47 +000083 int opt_short = 0;
84 int opt_domain = 0;
85 int opt_ip = 0;
86 struct hostent *h;
87 char *filename = NULL;
88 char buf[255];
89 char *s = NULL;
Eric Andersen485b9551999-12-07 23:14:59 +000090
Erik Andersene49d5ec2000-02-08 19:58:47 +000091 if (argc < 1)
Eric Andersend29edf31999-12-08 04:13:44 +000092 usage(hostname_usage);
Eric Andersen485b9551999-12-07 23:14:59 +000093
Erik Andersene49d5ec2000-02-08 19:58:47 +000094 while (--argc > 0 && **(++argv) == '-') {
95 while (*(++(*argv))) {
96 switch (**argv) {
97 case 's':
98 opt_short = 1;
99 break;
100 case 'i':
101 opt_ip = 1;
102 break;
103 case 'd':
104 opt_domain = 1;
105 break;
106 case 'F':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000107 if (--argc == 0) {
108 usage(hostname_usage);
109 }
110 filename = *(++argv);
111 break;
112 default:
113 usage(hostname_usage);
114 }
115 if (filename != NULL)
116 break;
117 }
118 }
119
120 if (argc >= 1) {
121 do_sethostname(*argv, 0);
122 } else if (filename != NULL) {
123 do_sethostname(filename, 1);
124 } else {
125 gethostname(buf, 255);
126 if (opt_short) {
127 s = strchr(buf, '.');
128 if (!s)
129 s = buf;
130 *s = 0;
131 printf("%s\n", buf);
132 } else if (opt_domain) {
133 s = strchr(buf, '.');
134 printf("%s\n", (s ? s + 1 : ""));
135 } else if (opt_ip) {
136 h = gethostbyname(buf);
137 if (!h) {
138 printf("Host not found\n");
139 exit(1);
140 }
141 printf("%s\n", inet_ntoa(*(struct in_addr *) (h->h_addr)));
142 } else {
143 printf("%s\n", buf);
144 }
145 }
146 exit(0);
Eric Andersen485b9551999-12-07 23:14:59 +0000147}