blob: 20e1741870c0e7f9983fd15c48b635082b452344 [file] [log] [blame]
Eric Andersen485b9551999-12-07 23:14:59 +00001/*
Eric Andersen1792f8c1999-12-09 06:11:36 +00002 * $Id: hostname.c,v 1.5 1999/12/09 06:11:36 andersen Exp $
Eric Andersen485b9551999-12-07 23:14:59 +00003 * Mini hostname implementation for busybox
4 *
5 * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
6 *
Eric Andersend29edf31999-12-08 04:13:44 +00007 * adjusted by Erik Andersen <andersee@debian.org> to remove
8 * use of long options and GNU getopt. Improved the usage info.
9 *
Eric Andersen485b9551999-12-07 23:14:59 +000010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25#include "internal.h"
26#include <errno.h>
Eric Andersen485b9551999-12-07 23:14:59 +000027#include <arpa/inet.h>
28#include <netdb.h>
29#include <unistd.h>
30#include <stdio.h>
31
Eric Andersend29edf31999-12-08 04:13:44 +000032static const char* hostname_usage =
33"hostname [OPTION] {hostname | -F file}\n\n"
34"Get or set the hostname or DNS domain name. If a hostname is given\n"
35"(or a file with the -F parameter), the host name will be set.\n\n"
Eric Andersen485b9551999-12-07 23:14:59 +000036"Options:\n"
Eric Andersend29edf31999-12-08 04:13:44 +000037"\t-s\t\tShort\n"
38"\t-i\t\tAddresses for the hostname\n"
39"\t-d\t\tDNS domain name\n"
Eric Andersen2285f361999-12-08 04:23:30 +000040"\t-F FILE\t\tUse the contents of FILE to specify the hostname\n";
Eric Andersen485b9551999-12-07 23:14:59 +000041
Eric Andersen485b9551999-12-07 23:14:59 +000042
43void do_sethostname(char *s, int isfile)
44{
45 FILE *f;
46 char buf[255];
47
48 if (!s) return;
49 if (!isfile) {
50 if (sethostname(s, strlen(s)) < 0) {
51 if (errno == EPERM)
52 fprintf(stderr, "hostname: you must be root to change the hostname\n");
53 else
54 perror("sethostname");
55 exit(1);
56 }
57 } else {
58 if ((f = fopen(s, "r")) == NULL) {
59 perror(s);
60 exit(1);
61 } else {
62 fgets(buf, 255, f);
63 fclose(f);
64 if (buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = 0;
65 if (sethostname(buf, strlen(buf)) < 0) {
66 perror("sethostname");
67 exit(1);
68 }
69 }
70 }
71}
72
73int hostname_main(int argc, char **argv)
74{
Eric Andersen485b9551999-12-07 23:14:59 +000075 int opt_short = 0;
76 int opt_domain = 0;
77 int opt_ip = 0;
Eric Andersen485b9551999-12-07 23:14:59 +000078 struct hostent *h;
79 char *filename = NULL;
80 char buf[255];
81 char *s = NULL;
82
83 if (argc < 1) usage(hostname_usage);
84
Eric Andersend29edf31999-12-08 04:13:44 +000085 while (--argc > 0 && **(++argv) == '-') {
86 while (*(++(*argv))) {
87 switch (**argv) {
88 case 's':
89 opt_short = 1;
90 break;
91 case 'i':
92 opt_ip = 1;
93 break;
94 case 'd':
95 opt_domain = 1;
96 break;
97 case 'F':
98 filename = optarg;
99 if (--argc == 0) {
100 usage(hostname_usage);
101 }
102 filename = *(++argv);
103 break;
104 default:
105 usage(hostname_usage);
106 }
107 if (filename!=NULL)
108 break;
109 }
Eric Andersen485b9551999-12-07 23:14:59 +0000110 }
111
Eric Andersend29edf31999-12-08 04:13:44 +0000112 if (argc >= 1) {
113 do_sethostname(*argv, 0);
114 } else if (filename!=NULL) {
Eric Andersen485b9551999-12-07 23:14:59 +0000115 do_sethostname(filename, 1);
116 } else {
117 gethostname(buf, 255);
118 if (opt_short) {
119 s = strchr(buf, '.');
120 if (!s) s = buf; *s = 0;
121 printf("%s\n", buf);
122 } else if (opt_domain) {
123 s = strchr(buf, '.');
124 printf("%s\n", (s ? s+1 : ""));
125 } else if (opt_ip) {
126 h = gethostbyname(buf);
127 if (!h) {
128 printf("Host not found\n");
129 exit(1);
130 }
131 printf("%s\n", inet_ntoa(*(struct in_addr *)(h->h_addr)));
132 } else {
133 printf("%s\n", buf);
134 }
135 }
Eric Andersend29edf31999-12-08 04:13:44 +0000136 exit( 0);
Eric Andersen485b9551999-12-07 23:14:59 +0000137}
138