6d3b8ebb86c0c5c84d232dad3f79ab78f48ab372
[oweals/busybox.git] / 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 "busybox.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                         else
28                                 bb_perror_msg_and_die("sethostname");
29                 }
30         } else {
31                 f = xfopen(s, "r");
32                 while (fgets(bb_common_bufsiz1, sizeof(bb_common_bufsiz1), f) != NULL) {
33                         if (bb_common_bufsiz1[0] == '#') {
34                                 continue;
35                         }
36                         chomp(bb_common_bufsiz1);
37                         do_sethostname(bb_common_bufsiz1, 0);
38                 }
39                 if (ENABLE_FEATURE_CLEAN_UP)
40                         fclose(f);
41         }
42 }
43
44 int hostname_main(int argc, char **argv)
45 {
46         enum {
47                 OPT_d = 0x1,
48                 OPT_f = 0x2,
49                 OPT_i = 0x4,
50                 OPT_s = 0x8,
51                 OPT_dfis = 0xf,
52         };
53
54         char buf[256];
55         char *hostname_str = NULL;
56
57         if (argc < 1)
58                 bb_show_usage();
59
60         getopt32(argc, argv, "dfisF:", &hostname_str);
61
62         /* Output in desired format */
63         if (option_mask32 & OPT_dfis) {
64                 struct hostent *hp;
65                 char *p;
66                 gethostname(buf, sizeof(buf));
67                 hp = xgethostbyname(buf);
68                 p = strchr(hp->h_name, '.');
69                 if (option_mask32 & OPT_f) {
70                         puts(hp->h_name);
71                 } else if (option_mask32 & OPT_s) {
72                         if (p != NULL) {
73                                 *p = 0;
74                         }
75                         puts(hp->h_name);
76                 } else if (option_mask32 & OPT_d) {
77                         if (p)
78                                 puts(p + 1);
79                 } else if (option_mask32 & OPT_i) {
80                         while (hp->h_addr_list[0]) {
81                                 printf("%s ", inet_ntoa(*(struct in_addr *) (*hp->h_addr_list++)));
82                         }
83                         puts("");
84                 }
85         }
86         /* Set the hostname */
87         else if (hostname_str != NULL) {
88                 do_sethostname(hostname_str, 1);
89         } else if (optind < argc) {
90                 do_sethostname(argv[optind], 0);
91         }
92         /* Or if all else fails,
93          * just print the current hostname */
94         else {
95                 gethostname(buf, sizeof(buf));
96                 puts(buf);
97         }
98         return 0;
99 }