1 | /* vi: set sw=4 ts=4: */
|
---|
2 | /*
|
---|
3 | * $Id: hostname.c,v 1.36 2003/07/14 21:21:01 andersen Exp $
|
---|
4 | * Mini hostname implementation for busybox
|
---|
5 | *
|
---|
6 | * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
|
---|
7 | *
|
---|
8 | * adjusted by Erik Andersen <andersen@codepoet.org> to remove
|
---|
9 | * use of long options and GNU getopt. Improved the usage info.
|
---|
10 | *
|
---|
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.
|
---|
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 <errno.h>
|
---|
27 | #include <arpa/inet.h>
|
---|
28 | #include <netdb.h>
|
---|
29 | #include <unistd.h>
|
---|
30 | #include <string.h>
|
---|
31 | #include <stdio.h>
|
---|
32 | #include <stdlib.h>
|
---|
33 | #include <getopt.h>
|
---|
34 | #include "busybox.h"
|
---|
35 |
|
---|
36 | extern char *optarg; /* in unistd.h */
|
---|
37 | extern int optind, opterr, optopt; /* in unistd.h */
|
---|
38 |
|
---|
39 | static void do_sethostname(char *s, int isfile)
|
---|
40 | {
|
---|
41 | FILE *f;
|
---|
42 | char buf[255];
|
---|
43 |
|
---|
44 | if (!s)
|
---|
45 | return;
|
---|
46 | if (!isfile) {
|
---|
47 | if (sethostname(s, strlen(s)) < 0) {
|
---|
48 | if (errno == EPERM)
|
---|
49 | bb_error_msg_and_die("you must be root to change the hostname");
|
---|
50 | else
|
---|
51 | bb_perror_msg_and_die("sethostname");
|
---|
52 | }
|
---|
53 | } else {
|
---|
54 | f = bb_xfopen(s, "r");
|
---|
55 | while (fgets(buf, 255, f) != NULL) {
|
---|
56 | if (buf[0] =='#') {
|
---|
57 | continue;
|
---|
58 | }
|
---|
59 | chomp(buf);
|
---|
60 | do_sethostname(buf, 0);
|
---|
61 | }
|
---|
62 | #ifdef CONFIG_FEATURE_CLEAN_UP
|
---|
63 | fclose(f);
|
---|
64 | #endif
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | int hostname_main(int argc, char **argv)
|
---|
69 | {
|
---|
70 | int opt;
|
---|
71 | int type = 0;
|
---|
72 | struct hostent *hp;
|
---|
73 | char *filename = NULL;
|
---|
74 | char buf[255];
|
---|
75 | char *p = NULL;
|
---|
76 |
|
---|
77 | if (argc < 1)
|
---|
78 | bb_show_usage();
|
---|
79 |
|
---|
80 | while ((opt = getopt(argc, argv, "dfisF:")) > 0) {
|
---|
81 | switch (opt) {
|
---|
82 | case 'd':
|
---|
83 | case 'f':
|
---|
84 | case 'i':
|
---|
85 | case 's':
|
---|
86 | type = opt;
|
---|
87 | break;
|
---|
88 | case 'F':
|
---|
89 | filename = optarg;
|
---|
90 | break;
|
---|
91 | default:
|
---|
92 | bb_show_usage();
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | /* Output in desired format */
|
---|
97 | if (type != 0) {
|
---|
98 | gethostname(buf, 255);
|
---|
99 | hp = xgethostbyname(buf);
|
---|
100 | p = strchr(hp->h_name, '.');
|
---|
101 | if (type == 'f') {
|
---|
102 | puts(hp->h_name);
|
---|
103 | } else if (type == 's') {
|
---|
104 | if (p != NULL) {
|
---|
105 | *p = 0;
|
---|
106 | }
|
---|
107 | puts(hp->h_name);
|
---|
108 | } else if (type == 'd') {
|
---|
109 | if (p) puts(p + 1);
|
---|
110 | } else if (type == 'i') {
|
---|
111 | while (hp->h_addr_list[0]) {
|
---|
112 | printf("%s ", inet_ntoa(*(struct in_addr *) (*hp->h_addr_list++)));
|
---|
113 | }
|
---|
114 | printf("\n");
|
---|
115 | }
|
---|
116 | }
|
---|
117 | /* Set the hostname */
|
---|
118 | else if (filename != NULL) {
|
---|
119 | do_sethostname(filename, 1);
|
---|
120 | } else if (optind < argc) {
|
---|
121 | do_sethostname(argv[optind], 0);
|
---|
122 | }
|
---|
123 | /* Or if all else fails,
|
---|
124 | * just print the current hostname */
|
---|
125 | else {
|
---|
126 | gethostname(buf, 255);
|
---|
127 | puts(buf);
|
---|
128 | }
|
---|
129 | return(0);
|
---|
130 | }
|
---|