dumpleases: new option -d to show time in seconds

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>
This commit is contained in:
Isaac Dunham 2015-10-24 20:28:04 +02:00 committed by Denys Vlasenko
parent 334e12ac6a
commit d320a1e7a5

View File

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