Replace index_in_[sub]str_array with index_in_[sub]strings,
[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 char dumpleases_longopts[] =
28                 "absolute\0"  No_argument       "a"
29                 "remaining\0" No_argument       "r"
30                 "file\0"      Required_argument "f"
31                 ;
32
33         applet_long_options = dumpleases_longopts;
34 #endif
35         opt_complementary = "=0:a--r:r--a";
36         opt = getopt32(argc, argv, "arf:", &file);
37
38         fd = xopen(file, O_RDONLY);
39
40         printf("Mac Address       IP-Address      Expires %s\n", (opt & OPT_a) ? "at" : "in");
41         /*     "00:00:00:00:00:00 255.255.255.255 Wed Jun 30 21:49:08 1993" */
42         while (full_read(fd, &lease, sizeof(lease)) == sizeof(lease)) {
43                 printf(":%02x"+1, lease.chaddr[0]);
44                 for (i = 1; i < 6; i++) {
45                         printf(":%02x", lease.chaddr[i]);
46                 }
47                 addr.s_addr = lease.yiaddr;
48                 printf(" %-15s ", inet_ntoa(addr));
49                 expires = ntohl(lease.expires);
50                 if (!(opt & OPT_a)) { /* no -a */
51                         if (!expires)
52                                 puts("expired");
53                         else {
54                                 unsigned d, h, m;
55                                 d = expires / (24*60*60); expires %= (24*60*60);
56                                 h = expires / (60*60); expires %= (60*60);
57                                 m = expires / 60; expires %= 60;
58                                 if (d) printf("%u days ", d);
59                                 printf("%02u:%02u:%02u\n", h, m, (unsigned)expires);
60                         }
61                 } else /* -a */
62                         fputs(ctime(&expires), stdout);
63         }
64         /* close(fd); */
65
66         return 0;
67 }