Initial public busybox upstream commit
[busybox4maemo] / networking / hostname.c
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  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
11  *
12  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
13  */
14
15 #include "libbb.h"
16
17 static void do_sethostname(char *s, int isfile)
18 {
19         FILE *f;
20
21         if (!s)
22                 return;
23         if (!isfile) {
24                 if (sethostname(s, strlen(s)) < 0) {
25                         if (errno == EPERM)
26                                 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
27                         bb_perror_msg_and_die("sethostname");
28                 }
29         } else {
30                 f = xfopen(s, "r");
31 #define strbuf bb_common_bufsiz1
32                 while (fgets(strbuf, sizeof(strbuf), f) != NULL) {
33                         if (strbuf[0] == '#') {
34                                 continue;
35                         }
36                         chomp(strbuf);
37                         do_sethostname(strbuf, 0);
38                 }
39                 if (ENABLE_FEATURE_CLEAN_UP)
40                         fclose(f);
41         }
42 }
43
44 int hostname_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
45 int hostname_main(int argc, char **argv)
46 {
47         enum {
48                 OPT_d = 0x1,
49                 OPT_f = 0x2,
50                 OPT_i = 0x4,
51                 OPT_s = 0x8,
52                 OPT_F = 0x10,
53                 OPT_dfis = 0xf,
54         };
55
56         char *buf;
57         char *hostname_str;
58
59         if (argc < 1)
60                 bb_show_usage();
61
62         getopt32(argv, "dfisF:", &hostname_str);
63         argv += optind;
64         buf = safe_gethostname();
65
66         /* Output in desired format */
67         if (option_mask32 & OPT_dfis) {
68                 struct hostent *hp;
69                 char *p;
70                 hp = xgethostbyname(buf);
71                 p = strchr(hp->h_name, '.');
72                 if (option_mask32 & OPT_f) {
73                         puts(hp->h_name);
74                 } else if (option_mask32 & OPT_s) {
75                         if (p)
76                                 *p = '\0';
77                         puts(hp->h_name);
78                 } else if (option_mask32 & OPT_d) {
79                         if (p)
80                                 puts(p + 1);
81                 } else if (option_mask32 & OPT_i) {
82                         while (hp->h_addr_list[0]) {
83                                 printf("%s ", inet_ntoa(*(struct in_addr *) (*hp->h_addr_list++)));
84                         }
85                         bb_putchar('\n');
86                 }
87         }
88         /* Set the hostname */
89         else if (option_mask32 & OPT_F) {
90                 do_sethostname(hostname_str, 1);
91         } else if (argv[0]) {
92                 do_sethostname(argv[0], 0);
93         }
94         /* Or if all else fails,
95          * just print the current hostname */
96         else {
97                 puts(buf);
98         }
99         if (ENABLE_FEATURE_CLEAN_UP)
100                 free(buf);
101         return 0;
102 }