telnetd: getopt_ulflags'isation
[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 #include <getopt.h>
18
19 extern char *optarg; /* in unistd.h */
20 extern int  optind, opterr, optopt; /* in unistd.h */
21
22 static void do_sethostname(char *s, int isfile)
23 {
24         FILE *f;
25         char buf[255];
26
27         if (!s)
28                 return;
29         if (!isfile) {
30                 if (sethostname(s, strlen(s)) < 0) {
31                         if (errno == EPERM)
32                                 bb_error_msg_and_die("you must be root to change the hostname");
33                         else
34                                 bb_perror_msg_and_die("sethostname");
35                 }
36         } else {
37                 f = xfopen(s, "r");
38                 while (fgets(buf, 255, f) != NULL) {
39                         if (buf[0] =='#') {
40                                 continue;
41                         }
42                         chomp(buf);
43                         do_sethostname(buf, 0);
44                 }
45 #ifdef CONFIG_FEATURE_CLEAN_UP
46                 fclose(f);
47 #endif
48         }
49 }
50
51 int hostname_main(int argc, char **argv)
52 {
53         int opt;
54         int type = 0;
55         struct hostent *hp;
56         char *filename = NULL;
57         char buf[255];
58         char *p = NULL;
59
60         if (argc < 1)
61                 bb_show_usage();
62
63         while ((opt = getopt(argc, argv, "dfisF:")) > 0) {
64                 switch (opt) {
65                 case 'd':
66                 case 'f':
67                 case 'i':
68                 case 's':
69                         type = opt;
70                         break;
71                 case 'F':
72                         filename = optarg;
73                         break;
74                 default:
75                         bb_show_usage();
76                 }
77         }
78
79         /* Output in desired format */
80         if (type != 0) {
81                 gethostname(buf, 255);
82                 hp = xgethostbyname(buf);
83                 p = strchr(hp->h_name, '.');
84                 if (type == 'f') {
85                         puts(hp->h_name);
86                 } else if (type == 's') {
87                         if (p != NULL) {
88                                 *p = 0;
89                         }
90                         puts(hp->h_name);
91                 } else if (type == 'd') {
92                         if (p) puts(p + 1);
93                 } else if (type == 'i') {
94                         while (hp->h_addr_list[0]) {
95                                 printf("%s ", inet_ntoa(*(struct in_addr *) (*hp->h_addr_list++)));
96                         }
97                         printf("\n");
98                 }
99         }
100         /* Set the hostname */
101         else if (filename != NULL) {
102                 do_sethostname(filename, 1);
103         } else if (optind < argc) {
104                 do_sethostname(argv[optind], 0);
105         }
106         /* Or if all else fails,
107          * just print the current hostname */
108          else {
109                 gethostname(buf, 255);
110                 puts(buf);
111         }
112         return(0);
113 }