aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2006-09-22 08:53:14 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2006-09-22 08:53:14 +0000
commit8514fc5681a640b305a79bb9d42fcec4d3b113d5 (patch)
tree1a8ea8336f1a6ac52c124441da50919f5b8b91db
parent0e87d347e8298c76d689eae1aabcee5aba313072 (diff)
downloadbusybox-w32-8514fc5681a640b305a79bb9d42fcec4d3b113d5.tar.gz
busybox-w32-8514fc5681a640b305a79bb9d42fcec4d3b113d5.tar.bz2
busybox-w32-8514fc5681a640b305a79bb9d42fcec4d3b113d5.zip
hostname: getopt_ulflags'isation
-rw-r--r--networking/hostname.c67
1 files changed, 28 insertions, 39 deletions
diff --git a/networking/hostname.c b/networking/hostname.c
index 03fd88edb..2850bd5bd 100644
--- a/networking/hostname.c
+++ b/networking/hostname.c
@@ -14,15 +14,11 @@
14 */ 14 */
15 15
16#include "busybox.h" 16#include "busybox.h"
17#include <getopt.h>
18
19extern char *optarg; /* in unistd.h */
20extern int optind, opterr, optopt; /* in unistd.h */
21 17
22static void do_sethostname(char *s, int isfile) 18static void do_sethostname(char *s, int isfile)
23{ 19{
24 FILE *f; 20 FILE *f;
25 char buf[255]; 21 char buf[256];
26 22
27 if (!s) 23 if (!s)
28 return; 24 return;
@@ -35,7 +31,7 @@ static void do_sethostname(char *s, int isfile)
35 } 31 }
36 } else { 32 } else {
37 f = xfopen(s, "r"); 33 f = xfopen(s, "r");
38 while (fgets(buf, 255, f) != NULL) { 34 while (fgets(buf, sizeof(buf), f) != NULL) {
39 if (buf[0] =='#') { 35 if (buf[0] =='#') {
40 continue; 36 continue;
41 } 37 }
@@ -50,64 +46,57 @@ static void do_sethostname(char *s, int isfile)
50 46
51int hostname_main(int argc, char **argv) 47int hostname_main(int argc, char **argv)
52{ 48{
53 int opt; 49 enum {
54 int type = 0; 50 OPT_d = 0x1,
55 struct hostent *hp; 51 OPT_f = 0x2,
56 char *filename = NULL; 52 OPT_i = 0x4,
57 char buf[255]; 53 OPT_s = 0x8,
58 char *p = NULL; 54 OPT_dfis = 0xf,
55 };
56
57 char buf[256];
58 unsigned long opt;
59 char *hostname_str = NULL;
59 60
60 if (argc < 1) 61 if (argc < 1)
61 bb_show_usage(); 62 bb_show_usage();
62 63
63 while ((opt = getopt(argc, argv, "dfisF:")) > 0) { 64 opt = bb_getopt_ulflags(argc, argv, "dfisF:", &hostname_str);
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 65
79 /* Output in desired format */ 66 /* Output in desired format */
80 if (type != 0) { 67 if (opt & OPT_dfis) {
81 gethostname(buf, 255); 68 struct hostent *hp;
69 char *p;
70 gethostname(buf, sizeof(buf));
82 hp = xgethostbyname(buf); 71 hp = xgethostbyname(buf);
83 p = strchr(hp->h_name, '.'); 72 p = strchr(hp->h_name, '.');
84 if (type == 'f') { 73 if (opt & OPT_f) {
85 puts(hp->h_name); 74 puts(hp->h_name);
86 } else if (type == 's') { 75 } else if (opt & OPT_s) {
87 if (p != NULL) { 76 if (p != NULL) {
88 *p = 0; 77 *p = 0;
89 } 78 }
90 puts(hp->h_name); 79 puts(hp->h_name);
91 } else if (type == 'd') { 80 } else if (opt & OPT_d) {
92 if (p) puts(p + 1); 81 if (p) puts(p + 1);
93 } else if (type == 'i') { 82 } else if (opt & OPT_i) {
94 while (hp->h_addr_list[0]) { 83 while (hp->h_addr_list[0]) {
95 printf("%s ", inet_ntoa(*(struct in_addr *) (*hp->h_addr_list++))); 84 printf("%s ", inet_ntoa(*(struct in_addr *) (*hp->h_addr_list++)));
96 } 85 }
97 printf("\n"); 86 puts("");
98 } 87 }
99 } 88 }
100 /* Set the hostname */ 89 /* Set the hostname */
101 else if (filename != NULL) { 90 else if (hostname_str != NULL) {
102 do_sethostname(filename, 1); 91 do_sethostname(hostname_str, 1);
103 } else if (optind < argc) { 92 } else if (optind < argc) {
104 do_sethostname(argv[optind], 0); 93 do_sethostname(argv[optind], 0);
105 } 94 }
106 /* Or if all else fails, 95 /* Or if all else fails,
107 * just print the current hostname */ 96 * just print the current hostname */
108 else { 97 else {
109 gethostname(buf, 255); 98 gethostname(buf, sizeof(buf));
110 puts(buf); 99 puts(buf);
111 } 100 }
112 return(0); 101 return 0;
113} 102}