A bunch of defined(__GLIBC__) added. static-linking warning expanded
[oweals/busybox.git] / networking / hostname.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * $Id: hostname.c,v 1.36 2003/07/14 21:21:01 andersen Exp $
4  * Mini hostname implementation for busybox
5  *
6  * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
7  *
8  * adjusted by Erik Andersen <andersen@codepoet.org> to remove
9  * use of long options and GNU getopt.  Improved the usage info.
10  *
11  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
12  *
13  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
14  */
15
16 #include "busybox.h"
17
18 static void do_sethostname(char *s, int isfile)
19 {
20         FILE *f;
21         char buf[256];
22
23         if (!s)
24                 return;
25         if (!isfile) {
26                 if (sethostname(s, strlen(s)) < 0) {
27                         if (errno == EPERM)
28                                 bb_error_msg_and_die("you must be root to change the hostname");
29                         else
30                                 bb_perror_msg_and_die("sethostname");
31                 }
32         } else {
33                 f = xfopen(s, "r");
34                 while (fgets(buf, sizeof(buf), f) != NULL) {
35                         if (buf[0] =='#') {
36                                 continue;
37                         }
38                         chomp(buf);
39                         do_sethostname(buf, 0);
40                 }
41 #ifdef CONFIG_FEATURE_CLEAN_UP
42                 fclose(f);
43 #endif
44         }
45 }
46
47 int hostname_main(int argc, char **argv)
48 {
49         enum {
50                 OPT_d = 0x1,
51                 OPT_f = 0x2,
52                 OPT_i = 0x4,
53                 OPT_s = 0x8,
54                 OPT_dfis = 0xf,
55         };
56
57         char buf[256];
58         unsigned opt;
59         char *hostname_str = NULL;
60
61         if (argc < 1)
62                 bb_show_usage();
63
64         opt = getopt32(argc, argv, "dfisF:", &hostname_str);
65
66         /* Output in desired format */
67         if (opt & OPT_dfis) {
68                 struct hostent *hp;
69                 char *p;
70                 gethostname(buf, sizeof(buf));
71                 hp = xgethostbyname(buf);
72                 p = strchr(hp->h_name, '.');
73                 if (opt & OPT_f) {
74                         puts(hp->h_name);
75                 } else if (opt & OPT_s) {
76                         if (p != NULL) {
77                                 *p = 0;
78                         }
79                         puts(hp->h_name);
80                 } else if (opt & OPT_d) {
81                         if (p) puts(p + 1);
82                 } else if (opt & OPT_i) {
83                         while (hp->h_addr_list[0]) {
84                                 printf("%s ", inet_ntoa(*(struct in_addr *) (*hp->h_addr_list++)));
85                         }
86                         puts("");
87                 }
88         }
89         /* Set the hostname */
90         else if (hostname_str != NULL) {
91                 do_sethostname(hostname_str, 1);
92         } else if (optind < argc) {
93                 do_sethostname(argv[optind], 0);
94         }
95         /* Or if all else fails,
96          * just print the current hostname */
97         else {
98                 gethostname(buf, sizeof(buf));
99                 puts(buf);
100         }
101         return 0;
102 }