dumpleases: new option -d to show time in seconds
authorIsaac Dunham <ibid.ag@gmail.com>
Sat, 24 Oct 2015 18:28:04 +0000 (20:28 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Sat, 24 Oct 2015 18:28:04 +0000 (20:28 +0200)
function                                             old     new   delta
dumpleases_main                                      493     534     +41
static.dumpleases_longopts                            31      41     +10
packed_usage                                       30777   30752     -25
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/1 up/down: 51/-25)             Total: 26 bytes

Signed-off-by: Isaac Dunham <ibid.ag@gmail.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
networking/udhcp/dumpleases.c

index 64cd73ec774638cdfbde3995ac4a2a7ea867960e..f68e499e7345950a53a84ade4da0c797ef073787 100644 (file)
@@ -4,18 +4,20 @@
  */
 
 //usage:#define dumpleases_trivial_usage
-//usage:       "[-r|-a] [-f LEASEFILE]"
+//usage:       "[-r|-a] [-d] [-f LEASEFILE]"
 //usage:#define dumpleases_full_usage "\n\n"
 //usage:       "Display DHCP leases granted by udhcpd\n"
 //usage:       IF_LONG_OPTS(
 //usage:     "\n       -f,--file=FILE  Lease file"
 //usage:     "\n       -r,--remaining  Show remaining time"
 //usage:     "\n       -a,--absolute   Show expiration time"
+//usage:     "\n       -d,--decimal    Show time in seconds"
 //usage:       )
 //usage:       IF_NOT_LONG_OPTS(
 //usage:     "\n       -f FILE Lease file"
 //usage:     "\n       -r      Show remaining time"
 //usage:     "\n       -a      Show expiration time"
+//usage:     "\n       -d      Show time in seconds"
 //usage:       )
 
 #include "common.h"
@@ -28,21 +30,22 @@ int dumpleases_main(int argc UNUSED_PARAM, char **argv)
        int fd;
        int i;
        unsigned opt;
-       int64_t written_at, curr, expires_abs;
+       int64_t written_at, curr;
        const char *file = LEASES_FILE;
        struct dyn_lease lease;
-       struct in_addr addr;
 
        enum {
                OPT_a = 0x1, // -a
                OPT_r = 0x2, // -r
                OPT_f = 0x4, // -f
+               OPT_d = 0x8, // -d
        };
 #if ENABLE_LONG_OPTS
        static const char dumpleases_longopts[] ALIGN1 =
                "absolute\0"  No_argument       "a"
                "remaining\0" No_argument       "r"
                "file\0"      Required_argument "f"
+               "decimal\0"   No_argument       "d"
                ;
 
        applet_long_options = dumpleases_longopts;
@@ -50,11 +53,12 @@ int dumpleases_main(int argc UNUSED_PARAM, char **argv)
        init_unicode();
 
        opt_complementary = "=0:a--r:r--a";
-       opt = getopt32(argv, "arf:", &file);
+       opt = getopt32(argv, "arf:d", &file);
 
        fd = xopen(file, O_RDONLY);
 
-       printf("Mac Address       IP Address      Host Name           Expires %s\n", (opt & OPT_a) ? "at" : "in");
+       printf("Mac Address       IP Address      Host Name           Expires %s\n",
+               (opt & OPT_a) ? "at" : "in");
        /*     "00:00:00:00:00:00 255.255.255.255 ABCDEFGHIJKLMNOPQRS Wed Jun 30 21:49:08 1993" */
        /*     "123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 */
 
@@ -65,6 +69,9 @@ int dumpleases_main(int argc UNUSED_PARAM, char **argv)
                written_at = curr; /* lease file from future! :) */
 
        while (full_read(fd, &lease, sizeof(lease)) == sizeof(lease)) {
+               struct in_addr addr;
+               int64_t expires_abs;
+
                const char *fmt = ":%02x" + 1;
                for (i = 0; i < 6; i++) {
                        printf(fmt, lease.lease_mac[i]);
@@ -87,6 +94,13 @@ int dumpleases_main(int argc UNUSED_PARAM, char **argv)
                        puts("expired");
                        continue;
                }
+               if (opt & OPT_d) {
+                       /* -d: decimal time */
+                       if (!(opt & OPT_a))
+                               expires_abs -= curr;
+                       printf("%llu\n", (unsigned long long) expires_abs);
+                       continue;
+               }
                if (!(opt & OPT_a)) { /* no -a */
                        unsigned d, h, m;
                        unsigned expires = expires_abs - curr;