diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-03-29 19:29:32 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-03-29 19:29:32 +0000 |
commit | 5e43d8591b50905e8bef8b30984b7633bcdf07ad (patch) | |
tree | 95ca9860d7660bded43d3ea69bbc661178cae584 /networking | |
parent | a87ed2c716d883d7665904d3716a63e21337c6cf (diff) | |
download | busybox-w32-5e43d8591b50905e8bef8b30984b7633bcdf07ad.tar.gz busybox-w32-5e43d8591b50905e8bef8b30984b7633bcdf07ad.tar.bz2 busybox-w32-5e43d8591b50905e8bef8b30984b7633bcdf07ad.zip |
dumpleases: getopt32()-ization
(from Mats Erik Andersson <mats.andersson64@comhem.se>)
Diffstat (limited to 'networking')
-rw-r--r-- | networking/udhcp/dumpleases.c | 61 |
1 files changed, 27 insertions, 34 deletions
diff --git a/networking/udhcp/dumpleases.c b/networking/udhcp/dumpleases.c index ce73c474c..80cf5b8f5 100644 --- a/networking/udhcp/dumpleases.c +++ b/networking/udhcp/dumpleases.c | |||
@@ -7,48 +7,40 @@ | |||
7 | #include "common.h" | 7 | #include "common.h" |
8 | #include "dhcpd.h" | 8 | #include "dhcpd.h" |
9 | 9 | ||
10 | |||
11 | #define REMAINING 0 | ||
12 | #define ABSOLUTE 1 | ||
13 | |||
14 | int dumpleases_main(int argc, char *argv[]); | 10 | int dumpleases_main(int argc, char *argv[]); |
15 | int dumpleases_main(int argc, char *argv[]) | 11 | int dumpleases_main(int argc, char *argv[]) |
16 | { | 12 | { |
17 | int fp; | 13 | int fd; |
18 | int i, c, mode = REMAINING; | 14 | int i; |
19 | unsigned long expires; | 15 | unsigned opt; |
16 | time_t expires; | ||
20 | const char *file = LEASES_FILE; | 17 | const char *file = LEASES_FILE; |
21 | struct dhcpOfferedAddr lease; | 18 | struct dhcpOfferedAddr lease; |
22 | struct in_addr addr; | 19 | struct in_addr addr; |
23 | 20 | ||
21 | enum { | ||
22 | OPT_a = 0x1, // -a | ||
23 | OPT_r = 0x2, // -r | ||
24 | OPT_f = 0x4, // -f | ||
25 | }; | ||
26 | #if ENABLE_GETOPT_LONG | ||
24 | static const struct option options[] = { | 27 | static const struct option options[] = { |
25 | {"absolute", 0, 0, 'a'}, | 28 | { "absolute", no_argument, 0, 'a' }, |
26 | {"remaining", 0, 0, 'r'}, | 29 | { "remaining", no_argument, 0, 'r' }, |
27 | {"file", 1, 0, 'f'}, | 30 | { "file", required_argument, 0, 'f' }, |
28 | {0, 0, 0, 0} | 31 | { NULL, 0, 0, 0 } |
29 | }; | 32 | }; |
33 | |||
34 | applet_long_options = options; | ||
35 | #endif | ||
36 | opt_complementary = "=0:?:a--r:r--a"; | ||
37 | opt = getopt32(argc, argv, "arf:", &file); | ||
30 | 38 | ||
31 | while (1) { | 39 | fd = xopen(file, O_RDONLY); |
32 | int option_index = 0; | ||
33 | c = getopt_long(argc, argv, "arf:", options, &option_index); | ||
34 | if (c == -1) break; | ||
35 | |||
36 | switch (c) { | ||
37 | case 'a': mode = ABSOLUTE; break; | ||
38 | case 'r': mode = REMAINING; break; | ||
39 | case 'f': | ||
40 | file = optarg; | ||
41 | break; | ||
42 | default: | ||
43 | bb_show_usage(); | ||
44 | } | ||
45 | } | ||
46 | |||
47 | fp = xopen(file, O_RDONLY); | ||
48 | 40 | ||
49 | printf("Mac Address IP-Address Expires %s\n", mode == REMAINING ? "in" : "at"); | 41 | printf("Mac Address IP-Address Expires %s\n", (opt & OPT_a) ? "at" : "in"); |
50 | /* "00:00:00:00:00:00 255.255.255.255 Wed Jun 30 21:49:08 1993" */ | 42 | /* "00:00:00:00:00:00 255.255.255.255 Wed Jun 30 21:49:08 1993" */ |
51 | while (full_read(fp, &lease, sizeof(lease)) == sizeof(lease)) { | 43 | while (full_read(fd, &lease, sizeof(lease)) == sizeof(lease)) { |
52 | printf(":%02x"+1, lease.chaddr[0]); | 44 | printf(":%02x"+1, lease.chaddr[0]); |
53 | for (i = 1; i < 6; i++) { | 45 | for (i = 1; i < 6; i++) { |
54 | printf(":%02x", lease.chaddr[i]); | 46 | printf(":%02x", lease.chaddr[i]); |
@@ -56,9 +48,9 @@ int dumpleases_main(int argc, char *argv[]) | |||
56 | addr.s_addr = lease.yiaddr; | 48 | addr.s_addr = lease.yiaddr; |
57 | printf(" %-15s ", inet_ntoa(addr)); | 49 | printf(" %-15s ", inet_ntoa(addr)); |
58 | expires = ntohl(lease.expires); | 50 | expires = ntohl(lease.expires); |
59 | if (mode == REMAINING) { | 51 | if (!(opt & OPT_a)) { /* no -a */ |
60 | if (!expires) | 52 | if (!expires) |
61 | printf("expired\n"); | 53 | puts("expired"); |
62 | else { | 54 | else { |
63 | unsigned d, h, m; | 55 | unsigned d, h, m; |
64 | d = expires / (24*60*60); expires %= (24*60*60); | 56 | d = expires / (24*60*60); expires %= (24*60*60); |
@@ -67,9 +59,10 @@ int dumpleases_main(int argc, char *argv[]) | |||
67 | if (d) printf("%u days ", d); | 59 | if (d) printf("%u days ", d); |
68 | printf("%02u:%02u:%02u\n", h, m, (unsigned)expires); | 60 | printf("%02u:%02u:%02u\n", h, m, (unsigned)expires); |
69 | } | 61 | } |
70 | } else fputs(ctime(&expires), stdout); | 62 | } else /* -a */ |
63 | fputs(ctime(&expires), stdout); | ||
71 | } | 64 | } |
72 | /* close(fp); */ | 65 | /* close(fd); */ |
73 | 66 | ||
74 | return 0; | 67 | return 0; |
75 | } | 68 | } |