1 /* vi: set sw=4 ts=4: */
3 * $Id: hostname.c,v 1.30 2001/06/26 02:06:08 bug1 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
27 #include <arpa/inet.h>
35 static void do_sethostname(char *s, int isfile)
43 if (sethostname(s, strlen(s)) < 0) {
45 error_msg_and_die("you must be root to change the hostname");
47 perror_msg_and_die("sethostname");
52 #ifdef BB_FEATURE_CLEAN_UP
56 do_sethostname(buf, 0);
60 int hostname_main(int argc, char **argv)
66 char *filename = NULL;
73 while (--argc > 0 && **(++argv) == '-') {
74 while (*(++(*argv))) {
92 if (strcmp(++(*argv), "file") || --argc ==0 ) {
100 if (filename != NULL)
106 do_sethostname(*argv, 0);
107 } else if (filename != NULL) {
108 do_sethostname(filename, 1);
110 gethostname(buf, 255);
112 s = strchr(buf, '.');
117 } else if (opt_domain) {
118 s = strchr(buf, '.');
119 puts(s ? s + 1 : "");
121 h = xgethostbyname(buf);
122 puts(inet_ntoa(*(struct in_addr *) (h->h_addr)));