1 /* vi: set sw=4 ts=4: */
3 * $Id: hostname.c,v 1.10 2000/06/19 17:25:39 andersen Exp $
4 * Mini hostname implementation for busybox
6 * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
8 * adjusted by Erik Andersen <andersee@debian.org> to remove
9 * use of long options and GNU getopt. Improved the usage info.
11 * 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.
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.
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
28 #include <arpa/inet.h>
33 static const char *hostname_usage =
34 "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"
37 "(or a file with the -F parameter), the host name will be set.\n\n"
41 "\t-i\t\tAddresses for the hostname\n"
42 "\t-d\t\tDNS domain name\n"
43 "\t-F FILE\t\tUse the contents of FILE to specify the hostname\n"
48 void do_sethostname(char *s, int isfile)
56 if (sethostname(s, strlen(s)) < 0) {
59 "hostname: you must be root to change the hostname\n");
61 perror("sethostname");
65 if ((f = fopen(s, "r")) == NULL) {
71 if (buf[strlen(buf) - 1] == '\n')
72 buf[strlen(buf) - 1] = 0;
73 if (sethostname(buf, strlen(buf)) < 0) {
74 perror("sethostname");
81 int hostname_main(int argc, char **argv)
87 char *filename = NULL;
92 usage(hostname_usage);
94 while (--argc > 0 && **(++argv) == '-') {
95 while (*(++(*argv))) {
108 usage(hostname_usage);
110 filename = *(++argv);
113 usage(hostname_usage);
115 if (filename != NULL)
121 do_sethostname(*argv, 0);
122 } else if (filename != NULL) {
123 do_sethostname(filename, 1);
125 gethostname(buf, 255);
127 s = strchr(buf, '.');
132 } else if (opt_domain) {
133 s = strchr(buf, '.');
134 printf("%s\n", (s ? s + 1 : ""));
136 h = gethostbyname(buf);
138 printf("Host not found\n");
141 printf("%s\n", inet_ntoa(*(struct in_addr *) (h->h_addr)));