1 /* vi: set sw=4 ts=4: */
3 * Mini hostname implementation for busybox
5 * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
7 * adjusted by Erik Andersen <andersen@codepoet.org> to remove
8 * use of long options and GNU getopt. Improved the usage info.
10 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
12 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
17 static void do_sethostname(char *s, int isfile)
22 parser_t *parser = config_open2(s, xfopen_for_read);
23 while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL & ~PARSE_GREEDY)) {
26 if (ENABLE_FEATURE_CLEAN_UP)
28 } else if (sethostname(s, strlen(s)) < 0) {
30 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
31 bb_perror_msg_and_die("sethostname");
35 int hostname_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
36 int hostname_main(int argc, char **argv)
53 getopt32(argv, "dfisF:", &hostname_str);
55 buf = safe_gethostname();
57 /* Output in desired format */
58 if (option_mask32 & OPT_dfis) {
61 hp = xgethostbyname(buf);
62 p = strchr(hp->h_name, '.');
63 if (option_mask32 & OPT_f) {
65 } else if (option_mask32 & OPT_s) {
69 } else if (option_mask32 & OPT_d) {
72 } else if (option_mask32 & OPT_i) {
73 while (hp->h_addr_list[0]) {
74 printf("%s ", inet_ntoa(*(struct in_addr *) (*hp->h_addr_list++)));
79 /* Set the hostname */
80 else if (option_mask32 & OPT_F) {
81 do_sethostname(hostname_str, 1);
83 do_sethostname(argv[0], 0);
85 /* Or if all else fails,
86 * just print the current hostname */
90 if (ENABLE_FEATURE_CLEAN_UP)