aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libbb/getopt_ulflags.c7
-rw-r--r--networking/zcip.c61
2 files changed, 31 insertions, 37 deletions
diff --git a/libbb/getopt_ulflags.c b/libbb/getopt_ulflags.c
index 19c96914d..e0dc1371f 100644
--- a/libbb/getopt_ulflags.c
+++ b/libbb/getopt_ulflags.c
@@ -36,6 +36,13 @@ bb_getopt_ulflags (int argc, char **argv, const char *applet_opts, ...)
36 and so on. You can also look at the return value as a bit 36 and so on. You can also look at the return value as a bit
37 field and each option sets one bit. 37 field and each option sets one bit.
38 38
39 On exit, global variable optind is set so that if you
40 will do argc -= optind; argv += optind; then
41 argc will be equal to number of remaining non-option
42 arguments, first one would be in argv[0], next in argv[1] and so on
43 (options and their parameters will be moved into argv[]
44 positions prior to argv[optind]).
45
39 ":" If one of the options requires an argument, then add a ":" 46 ":" If one of the options requires an argument, then add a ":"
40 after the char in applet_opts and provide a pointer to store 47 after the char in applet_opts and provide a pointer to store
41 the argument. For example: 48 the argument. For example:
diff --git a/networking/zcip.c b/networking/zcip.c
index e781a5882..55d28e563 100644
--- a/networking/zcip.c
+++ b/networking/zcip.c
@@ -201,8 +201,6 @@ static unsigned conflicts; // = 0;
201static unsigned nprobes; // = 0; 201static unsigned nprobes; // = 0;
202static unsigned nclaims; // = 0; 202static unsigned nclaims; // = 0;
203static int ready; // = 0; 203static int ready; // = 0;
204static int quit; // = 0;
205static int foreground; // = 0;
206static int verbose; // = 0; 204static int verbose; // = 0;
207static int state = PROBE; 205static int state = PROBE;
208 206
@@ -211,39 +209,30 @@ int zcip_main(int argc, char *argv[])
211 struct ether_addr eth_addr; 209 struct ether_addr eth_addr;
212 char *why; 210 char *why;
213 int fd; 211 int fd;
214 int t;
215 212
216 // parse commandline: prog [options] ifname script 213 // parse commandline: prog [options] ifname script
217 while ((t = getopt(argc, argv, "fqr:v")) != EOF) { 214#define FOREGROUND (opts & 1)
218 switch (t) { 215#define QUIT (opts & 2)
219 case 'f': 216 char *r_opt;
220 foreground = 1; 217 unsigned long opts;
221 continue; 218
222 case 'q': 219 bb_opt_complementally = "vv"; // -v options accumulate
223 quit = 1; 220 opts = bb_getopt_ulflags(argc, argv, "fqr:v", &r_opt, &verbose);
224 continue; 221 if (opts & 4) {
225 case 'r': 222 if (inet_aton(r_opt, &ip) == 0
226 if (inet_aton(optarg, &ip) == 0 223 || (ntohl(ip.s_addr) & IN_CLASSB_NET) != LINKLOCAL_ADDR) {
227 || (ntohl(ip.s_addr) & IN_CLASSB_NET) 224 bb_error_msg_and_die("invalid link address");
228 != LINKLOCAL_ADDR) {
229 bb_error_msg_and_die("invalid link address");
230 }
231 continue;
232 case 'v':
233 verbose++;
234 foreground = 1;
235 continue;
236 default:
237 bb_error_msg_and_die("bad option");
238 } 225 }
239 } 226 }
240 if (optind < argc - 1) { 227 if (verbose) opts |= 1;
241 intf = argv[optind++]; 228 argc -= optind;
242 setenv("interface", intf, 1); 229 argv += optind;
243 script = argv[optind++]; 230 if (argc != 2)
244 }
245 if (optind != argc || !intf)
246 bb_show_usage(); 231 bb_show_usage();
232
233 intf = argv[0];
234 script = argv[1];
235 setenv("interface", intf, 1);
247 openlog(bb_applet_name, 0, LOG_DAEMON); 236 openlog(bb_applet_name, 0, LOG_DAEMON);
248 237
249 // initialize the interface (modprobe, ifup, etc) 238 // initialize the interface (modprobe, ifup, etc)
@@ -257,15 +246,13 @@ int zcip_main(int argc, char *argv[])
257 // open an ARP socket 246 // open an ARP socket
258 fd = xsocket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP)); 247 fd = xsocket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP));
259 // bind to the interface's ARP socket 248 // bind to the interface's ARP socket
260 xbind(fd, &saddr, sizeof (saddr); 249 xbind(fd, &saddr, sizeof (saddr));
261 250
262 // get the interface's ethernet address 251 // get the interface's ethernet address
263 //memset(&ifr, 0, sizeof (ifr)); 252 //memset(&ifr, 0, sizeof (ifr));
264 strncpy(ifr.ifr_name, intf, sizeof (ifr.ifr_name)); 253 strncpy(ifr.ifr_name, intf, sizeof (ifr.ifr_name));
265 if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) { 254 if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
266 foreground = 1; 255 bb_perror_msg_and_die("get ethernet address");
267 why = "get ethernet address";
268 goto bad;
269 } 256 }
270 memcpy(&eth_addr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN); 257 memcpy(&eth_addr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN);
271 258
@@ -283,7 +270,7 @@ int zcip_main(int argc, char *argv[])
283 // - link already has local address... just defend/update 270 // - link already has local address... just defend/update
284 271
285 // daemonize now; don't delay system startup 272 // daemonize now; don't delay system startup
286 if (!foreground) { 273 if (!FOREGROUND) {
287 xdaemon(0, verbose); 274 xdaemon(0, verbose);
288 syslog(LOG_INFO, "start, interface %s", intf); 275 syslog(LOG_INFO, "start, interface %s", intf);
289 } 276 }
@@ -394,7 +381,7 @@ int zcip_main(int argc, char *argv[])
394 381
395 // NOTE: all other exit paths 382 // NOTE: all other exit paths
396 // should deconfig ... 383 // should deconfig ...
397 if (quit) 384 if (QUIT)
398 return EXIT_SUCCESS; 385 return EXIT_SUCCESS;
399 } 386 }
400 break; 387 break;
@@ -555,7 +542,7 @@ int zcip_main(int argc, char *argv[])
555 } // switch poll 542 } // switch poll
556 } 543 }
557bad: 544bad:
558 if (foreground) 545 if (FOREGROUND)
559 perror(why); 546 perror(why);
560 else 547 else
561 syslog(LOG_ERR, "%s %s, %s error: %s", 548 syslog(LOG_ERR, "%s %s, %s error: %s",