stty: simplify linewrapping code a bit
[oweals/busybox.git] / networking / udhcp / dumpleases.c
index 2b19d976829353a1c95714d5c692794ca84f7f60..60e6b7edf969f0d0b83124dcda4fb407df37bef0 100644 (file)
@@ -1,81 +1,63 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
+ */
 #include <fcntl.h>
 #include <string.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <sys/wait.h>
-#include <sys/stat.h>
 #include <arpa/inet.h>
 #include <netdb.h>
 #include <netinet/in.h>
 #include <stdio.h>
-#include <sys/types.h>
 #include <sys/socket.h>
 #include <unistd.h>
-#include <syslog.h>
-#include <signal.h>
-#include <errno.h>
 #include <getopt.h>
 #include <time.h>
 
+#include "dhcpd.h"
+#include "leases.h"
 #include "libbb_udhcp.h"
 
 #define REMAINING 0
 #define ABSOLUTE 1
 
-struct lease_t {
-       unsigned char chaddr[16];
-       u_int32_t yiaddr;
-       u_int32_t expires;
-};
-
-#ifdef BB_VER
 int dumpleases_main(int argc, char *argv[])
-#else
-int main(int argc, char *argv[])
-#endif
 {
        FILE *fp;
        int i, c, mode = REMAINING;
        long expires;
-       char file[255] = "/var/lib/misc/udhcpd.leases";
-       struct lease_t lease;
+       const char *file = LEASES_FILE;
+       struct dhcpOfferedAddr lease;
        struct in_addr addr;
-       
-       static struct option options[] = {
+
+       static const struct option options[] = {
                {"absolute", 0, 0, 'a'},
                {"remaining", 0, 0, 'r'},
                {"file", 1, 0, 'f'},
-               {"help", 0, 0, 'h'},
                {0, 0, 0, 0}
        };
 
        while (1) {
                int option_index = 0;
-               c = getopt_long(argc, argv, "arf:h", options, &option_index);
+               c = getopt_long(argc, argv, "arf:", options, &option_index);
                if (c == -1) break;
-               
+
                switch (c) {
                case 'a': mode = ABSOLUTE; break;
                case 'r': mode = REMAINING; break;
                case 'f':
-                       strncpy(file, optarg, 255);
-                       file[254] = '\0';
-                       break;
-               case 'h':
-                       printf("Usage: dumpleases -f <file> -[r|a]\n\n");
-                       printf("  -f, --file=FILENAME             Leases file to load\n");
-                       printf("  -r, --remaining                 Interepret lease times as time remaing\n");
-                       printf("  -a, --absolute                  Interepret lease times as expire time\n");
+                       file = optarg;
                        break;
+               default:
+                       bb_show_usage();
                }
        }
-                       
-       if (!(fp = fopen(file, "r"))) {
-               perror("could not open input file");
-               return 0;
-       }
 
-       printf("Mac Address       IP-Address      Expires %s\n", mode == REMAINING ? "in" : "at");  
+       fp = xfopen(file, "r");
+
+       printf("Mac Address       IP-Address      Expires %s\n", mode == REMAINING ? "in" : "at");
        /*     "00:00:00:00:00:00 255.255.255.255 Wed Jun 30 21:49:08 1993" */
        while (fread(&lease, sizeof(lease), 1, fp)) {
 
@@ -107,6 +89,6 @@ int main(int argc, char *argv[])
                } else printf("%s", ctime(&expires));
        }
        fclose(fp);
-       
+
        return 0;
 }