2 * $Id: hostname.c,v 1.4 1999/12/08 23:19:36 andersen Exp $
3 * Mini hostname implementation for busybox
5 * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
7 * adjusted by Erik Andersen <andersee@debian.org> to remove
8 * use of long options and GNU getopt. Improved the usage info.
10 * 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.
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.
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
27 #include <arpa/inet.h>
32 static 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"
38 "\t-i\t\tAddresses for the hostname\n"
39 "\t-d\t\tDNS domain name\n"
40 "\t-F FILE\t\tUse the contents of FILE to specify the hostname\n";
43 void do_sethostname(char *s, int isfile)
50 if (sethostname(s, strlen(s)) < 0) {
52 fprintf(stderr, "hostname: you must be root to change the hostname\n");
54 perror("sethostname");
58 if ((f = fopen(s, "r")) == NULL) {
64 if (buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = 0;
65 if (sethostname(buf, strlen(buf)) < 0) {
66 perror("sethostname");
73 int hostname_main(int argc, char **argv)
79 char *filename = NULL;
83 if (argc < 1) usage(hostname_usage);
85 while (--argc > 0 && **(++argv) == '-') {
86 while (*(++(*argv))) {
100 usage(hostname_usage);
102 filename = *(++argv);
105 usage(hostname_usage);
113 do_sethostname(*argv, 0);
114 } else if (filename!=NULL) {
115 do_sethostname(filename, 1);
117 gethostname(buf, 255);
119 s = strchr(buf, '.');
120 if (!s) s = buf; *s = 0;
122 } else if (opt_domain) {
123 s = strchr(buf, '.');
124 printf("%s\n", (s ? s+1 : ""));
126 h = gethostbyname(buf);
128 printf("Host not found\n");
131 printf("%s\n", inet_ntoa(*(struct in_addr *)(h->h_addr)));