4 |
* |
* |
5 |
* Copyright (C) 1999 by Randolph Chung <tausq@debian.org> |
* Copyright (C) 1999 by Randolph Chung <tausq@debian.org> |
6 |
* |
* |
7 |
* adjusted by Erik Andersen <andersen@codepoet.org> to remove |
* Adjusted by Erik Andersen <andersen@codepoet.org> to remove |
8 |
* use of long options and GNU getopt. Improved the usage info. |
* use of long options and GNU getopt. Improved the usage info. |
9 |
* |
* |
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
|
* |
|
10 |
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
11 |
*/ |
*/ |
|
|
|
12 |
#include "libbb.h" |
#include "libbb.h" |
13 |
|
|
14 |
static void do_sethostname(char *s, int isfile) |
static void do_sethostname(char *s, int isfile) |
15 |
{ |
{ |
16 |
if (!s) |
// if (!s) |
17 |
return; |
// return; |
18 |
if (isfile) { |
if (isfile) { |
19 |
parser_t *parser = config_open2(s, xfopen_for_read); |
parser_t *parser = config_open2(s, xfopen_for_read); |
20 |
while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL & ~PARSE_GREEDY)) { |
while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL & ~PARSE_GREEDY)) { |
22 |
} |
} |
23 |
if (ENABLE_FEATURE_CLEAN_UP) |
if (ENABLE_FEATURE_CLEAN_UP) |
24 |
config_close(parser); |
config_close(parser); |
25 |
} else if (sethostname(s, strlen(s)) < 0) { |
} else if (sethostname(s, strlen(s))) { |
26 |
if (errno == EPERM) |
// if (errno == EPERM) |
27 |
bb_error_msg_and_die(bb_msg_perm_denied_are_you_root); |
// bb_error_msg_and_die(bb_msg_perm_denied_are_you_root); |
28 |
bb_perror_msg_and_die("sethostname"); |
bb_perror_msg_and_die("sethostname"); |
29 |
} |
} |
30 |
} |
} |
31 |
|
|
32 |
|
/* Manpage circa 2009: |
33 |
|
* |
34 |
|
* hostname [-v] [-a] [--alias] [-d] [--domain] [-f] [--fqdn] [--long] |
35 |
|
* [-i] [--ip-address] [-s] [--short] [-y] [--yp] [--nis] |
36 |
|
* |
37 |
|
* hostname [-v] [-F filename] [--file filename] / [hostname] |
38 |
|
* |
39 |
|
* domainname [-v] [-F filename] [--file filename] / [name] |
40 |
|
* { bbox: not supported } |
41 |
|
* |
42 |
|
* nodename [-v] [-F filename] [--file filename] / [name] |
43 |
|
* { bbox: not supported } |
44 |
|
* |
45 |
|
* dnsdomainname [-v] |
46 |
|
* { bbox: supported: Linux kernel build needs this } |
47 |
|
* nisdomainname [-v] |
48 |
|
* { bbox: not supported } |
49 |
|
* ypdomainname [-v] |
50 |
|
* { bbox: not supported } |
51 |
|
* |
52 |
|
* -a, --alias |
53 |
|
* Display the alias name of the host (if used). |
54 |
|
* { bbox: not supported } |
55 |
|
* -d, --domain |
56 |
|
* Display the name of the DNS domain. Don't use the command |
57 |
|
* domainname to get the DNS domain name because it will show the |
58 |
|
* NIS domain name and not the DNS domain name. Use dnsdomainname |
59 |
|
* instead. |
60 |
|
* -f, --fqdn, --long |
61 |
|
* Display the FQDN (Fully Qualified Domain Name). A FQDN consists |
62 |
|
* of a short host name and the DNS domain name. Unless you are |
63 |
|
* using bind or NIS for host lookups you can change the FQDN and |
64 |
|
* the DNS domain name (which is part of the FQDN) in the |
65 |
|
* /etc/hosts file. |
66 |
|
* -i, --ip-address |
67 |
|
* Display the IP address(es) of the host. |
68 |
|
* -s, --short |
69 |
|
* Display the short host name. This is the host name cut at the |
70 |
|
* first dot. |
71 |
|
* -v, --verbose |
72 |
|
* Be verbose and tell what's going on. |
73 |
|
* { bbox: supported but ignored } |
74 |
|
* -y, --yp, --nis |
75 |
|
* Display the NIS domain name. If a parameter is given (or --file |
76 |
|
* name ) then root can also set a new NIS domain. |
77 |
|
* { bbox: not supported } |
78 |
|
* -F, --file filename |
79 |
|
* Read the host name from the specified file. Comments (lines |
80 |
|
* starting with a `#') are ignored. |
81 |
|
*/ |
82 |
int hostname_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
int hostname_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
83 |
int hostname_main(int argc, char **argv) |
int hostname_main(int argc UNUSED_PARAM, char **argv) |
84 |
{ |
{ |
85 |
enum { |
enum { |
86 |
OPT_d = 0x1, |
OPT_d = 0x1, |
91 |
OPT_dfis = 0xf, |
OPT_dfis = 0xf, |
92 |
}; |
}; |
93 |
|
|
94 |
|
unsigned opts; |
95 |
char *buf; |
char *buf; |
96 |
char *hostname_str; |
char *hostname_str; |
97 |
|
|
98 |
if (argc < 1) |
#if ENABLE_LONG_OPTS |
99 |
bb_show_usage(); |
applet_long_options = |
100 |
|
"domain\0" No_argument "d" |
101 |
getopt32(argv, "dfisF:", &hostname_str); |
"fqdn\0" No_argument "f" |
102 |
|
//Enable if seen in active use in some distro: |
103 |
|
// "long\0" No_argument "f" |
104 |
|
// "ip-address\0" No_argument "i" |
105 |
|
// "short\0" No_argument "s" |
106 |
|
// "verbose\0" No_argument "v" |
107 |
|
"file\0" No_argument "F" |
108 |
|
; |
109 |
|
|
110 |
|
#endif |
111 |
|
/* dnsdomainname from net-tools 1.60, hostname 1.100 (2001-04-14), |
112 |
|
* supports hostname's options too (not just -v as manpage says) */ |
113 |
|
opts = getopt32(argv, "dfisF:v", &hostname_str); |
114 |
argv += optind; |
argv += optind; |
115 |
buf = safe_gethostname(); |
buf = safe_gethostname(); |
116 |
|
if (applet_name[0] == 'd') /* dnsdomainname? */ |
117 |
|
opts = OPT_d; |
118 |
|
|
119 |
/* Output in desired format */ |
if (opts & OPT_dfis) { |
120 |
if (option_mask32 & OPT_dfis) { |
/* Cases when we need full hostname (or its part) */ |
121 |
struct hostent *hp; |
struct hostent *hp; |
122 |
char *p; |
char *p; |
123 |
|
|
124 |
hp = xgethostbyname(buf); |
hp = xgethostbyname(buf); |
125 |
p = strchr(hp->h_name, '.'); |
p = strchrnul(hp->h_name, '.'); |
126 |
if (option_mask32 & OPT_f) { |
if (opts & OPT_f) { |
127 |
puts(hp->h_name); |
puts(hp->h_name); |
128 |
} else if (option_mask32 & OPT_s) { |
} else if (opts & OPT_s) { |
129 |
if (p) |
*p = '\0'; |
|
*p = '\0'; |
|
130 |
puts(hp->h_name); |
puts(hp->h_name); |
131 |
} else if (option_mask32 & OPT_d) { |
} else if (opts & OPT_d) { |
132 |
if (p) |
if (*p) |
133 |
puts(p + 1); |
puts(p + 1); |
134 |
} else if (option_mask32 & OPT_i) { |
} else /*if (opts & OPT_i)*/ { |
135 |
while (hp->h_addr_list[0]) { |
while (hp->h_addr_list[0]) { |
136 |
printf("%s ", inet_ntoa(*(struct in_addr *) (*hp->h_addr_list++))); |
printf("%s ", inet_ntoa(*(struct in_addr *) (*hp->h_addr_list++))); |
137 |
} |
} |
138 |
bb_putchar('\n'); |
bb_putchar('\n'); |
139 |
} |
} |
140 |
} |
} else if (opts & OPT_F) { |
141 |
/* Set the hostname */ |
/* Set the hostname */ |
|
else if (option_mask32 & OPT_F) { |
|
142 |
do_sethostname(hostname_str, 1); |
do_sethostname(hostname_str, 1); |
143 |
} else if (argv[0]) { |
} else if (argv[0]) { |
144 |
|
/* Set the hostname */ |
145 |
do_sethostname(argv[0], 0); |
do_sethostname(argv[0], 0); |
146 |
} |
} else { |
147 |
/* Or if all else fails, |
/* Just print the current hostname */ |
|
* just print the current hostname */ |
|
|
else { |
|
148 |
puts(buf); |
puts(buf); |
149 |
} |
} |
150 |
|
|
151 |
if (ENABLE_FEATURE_CLEAN_UP) |
if (ENABLE_FEATURE_CLEAN_UP) |
152 |
free(buf); |
free(buf); |
153 |
return EXIT_SUCCESS; |
return EXIT_SUCCESS; |