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)
24 if (sethostname(s, strlen(s)) < 0) {
26 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
28 bb_perror_msg_and_die("sethostname");
32 while (fgets(bb_common_bufsiz1, sizeof(bb_common_bufsiz1), f) != NULL) {
33 if (bb_common_bufsiz1[0] == '#') {
36 chomp(bb_common_bufsiz1);
37 do_sethostname(bb_common_bufsiz1, 0);
39 if (ENABLE_FEATURE_CLEAN_UP)
44 int hostname_main(int argc, char **argv)
61 getopt32(argc, argv, "dfisF:", &hostname_str);
63 /* Output in desired format */
64 if (option_mask32 & OPT_dfis) {
67 gethostname(buf, sizeof(buf));
68 hp = xgethostbyname(buf);
69 p = strchr(hp->h_name, '.');
70 if (option_mask32 & OPT_f) {
72 } else if (option_mask32 & OPT_s) {
77 } else if (option_mask32 & OPT_d) {
80 } else if (option_mask32 & OPT_i) {
81 while (hp->h_addr_list[0]) {
82 printf("%s ", inet_ntoa(*(struct in_addr *) (*hp->h_addr_list++)));
87 /* Set the hostname */
88 else if (option_mask32 & OPT_F) {
89 do_sethostname(hostname_str, 1);
90 } else if (optind < argc) {
91 do_sethostname(argv[optind], 0);
93 /* Or if all else fails,
94 * just print the current hostname */
96 gethostname(buf, sizeof(buf));