lpr: add more accurate comments
[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
6 #include "common.h"
7 #include "dhcpd.h"
8
9 int dumpleases_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
10 int dumpleases_main(int argc ATTRIBUTE_UNUSED, char **argv)
11 {
12         int fd;
13         int i;
14         unsigned opt;
15         time_t expires;
16         const char *file = LEASES_FILE;
17         struct dhcpOfferedAddr lease;
18         struct in_addr addr;
19
20         enum {
21                 OPT_a   = 0x1,  // -a
22                 OPT_r   = 0x2,  // -r
23                 OPT_f   = 0x4,  // -f
24         };
25 #if ENABLE_GETOPT_LONG
26         static const char dumpleases_longopts[] ALIGN1 =
27                 "absolute\0"  No_argument       "a"
28                 "remaining\0" No_argument       "r"
29                 "file\0"      Required_argument "f"
30                 ;
31
32         applet_long_options = dumpleases_longopts;
33 #endif
34         opt_complementary = "=0:a--r:r--a";
35         opt = getopt32(argv, "arf:", &file);
36
37         fd = xopen(file, O_RDONLY);
38
39         printf("Mac Address       IP-Address      Expires %s\n", (opt & OPT_a) ? "at" : "in");
40         /*     "00:00:00:00:00:00 255.255.255.255 Wed Jun 30 21:49:08 1993" */
41         while (full_read(fd, &lease, sizeof(lease)) == sizeof(lease)) {
42                 printf(":%02x"+1, lease.chaddr[0]);
43                 for (i = 1; i < 6; i++) {
44                         printf(":%02x", lease.chaddr[i]);
45                 }
46                 addr.s_addr = lease.yiaddr;
47                 printf(" %-15s ", inet_ntoa(addr));
48                 expires = ntohl(lease.expires);
49                 if (!(opt & OPT_a)) { /* no -a */
50                         if (!expires)
51                                 puts("expired");
52                         else {
53                                 unsigned d, h, m;
54                                 d = expires / (24*60*60); expires %= (24*60*60);
55                                 h = expires / (60*60); expires %= (60*60);
56                                 m = expires / 60; expires %= 60;
57                                 if (d) printf("%u days ", d);
58                                 printf("%02u:%02u:%02u\n", h, m, (unsigned)expires);
59                         }
60                 } else /* -a */
61                         fputs(ctime(&expires), stdout);
62         }
63         /* close(fd); */
64
65         return 0;
66 }