libbb: nuke BB_GETOPT_ERROR, always die if there are mutually exclusive options
[oweals/busybox.git] / networking / udhcp / dumpleases.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
4  */
5 #include <getopt.h>
6
7 #include "common.h"
8 #include "dhcpd.h"
9
10 int dumpleases_main(int argc, char **argv);
11 int dumpleases_main(int argc, char **argv)
12 {
13         int fd;
14         int i;
15         unsigned opt;
16         time_t expires;
17         const char *file = LEASES_FILE;
18         struct dhcpOfferedAddr lease;
19         struct in_addr addr;
20
21         enum {
22                 OPT_a   = 0x1,  // -a
23                 OPT_r   = 0x2,  // -r
24                 OPT_f   = 0x4,  // -f
25         };
26 #if ENABLE_GETOPT_LONG
27         static const struct option options[] = {
28                 { "absolute", no_argument, 0, 'a' },
29                 { "remaining", no_argument, 0, 'r' },
30                 { "file", required_argument, 0, 'f' },
31                 { NULL, 0, 0, 0 }
32         };
33
34         applet_long_options = options;
35 #endif
36         opt_complementary = "=0:a--r:r--a";
37         opt = getopt32(argc, argv, "arf:", &file);
38
39         fd = xopen(file, O_RDONLY);
40
41         printf("Mac Address       IP-Address      Expires %s\n", (opt & OPT_a) ? "at" : "in");
42         /*     "00:00:00:00:00:00 255.255.255.255 Wed Jun 30 21:49:08 1993" */
43         while (full_read(fd, &lease, sizeof(lease)) == sizeof(lease)) {
44                 printf(":%02x"+1, lease.chaddr[0]);
45                 for (i = 1; i < 6; i++) {
46                         printf(":%02x", lease.chaddr[i]);
47                 }
48                 addr.s_addr = lease.yiaddr;
49                 printf(" %-15s ", inet_ntoa(addr));
50                 expires = ntohl(lease.expires);
51                 if (!(opt & OPT_a)) { /* no -a */
52                         if (!expires)
53                                 puts("expired");
54                         else {
55                                 unsigned d, h, m;
56                                 d = expires / (24*60*60); expires %= (24*60*60);
57                                 h = expires / (60*60); expires %= (60*60);
58                                 m = expires / 60; expires %= 60;
59                                 if (d) printf("%u days ", d);
60                                 printf("%02u:%02u:%02u\n", h, m, (unsigned)expires);
61                         }
62                 } else /* -a */
63                         fputs(ctime(&expires), stdout);
64         }
65         /* close(fd); */
66
67         return 0;
68 }