source: MondoRescue/branches/3.3/mindi-busybox/networking/hostname.c@ 3734

Last change on this file since 3734 was 3621, checked in by Bruno Cornec, 10 years ago

New 3?3 banch for incorporation of latest busybox 1.25. Changing minor version to handle potential incompatibilities.

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