]> www.infradead.org Git - mtd-utils.git/commitdiff
flash_{un,}lock: switch to getopt library
authorBrian Norris <computersforpeace@gmail.com>
Thu, 27 Aug 2015 18:00:31 +0000 (11:00 -0700)
committerBrian Norris <computersforpeace@gmail.com>
Wed, 11 Nov 2015 22:05:36 +0000 (14:05 -0800)
We will be adding some more flags, so the getopt library can help.

Signed-off-by: Brian Norris <computersforpeace@gmail.com>
flash_unlock.c

index ee8ac890f61c278e3f2b7951ee93dfd773bbf03b..777e4375a44db7273a3ab9b4ee56caf124d954d7 100644 (file)
@@ -13,6 +13,7 @@
 #define FLASH_UNLOCK 0
 #endif
 
+#include <getopt.h>
 #include <unistd.h>
 #include <stdlib.h>
 #include <stdio.h>
@@ -35,6 +36,12 @@ static void usage(int status)
        exit(status);
 }
 
+static const char short_opts[] = "h";
+static const struct option long_opts[] = {
+       { "help",       no_argument,    0, 'h' },
+       { NULL,         0,              0, 0 },
+};
+
 int main(int argc, char *argv[])
 {
        int fd, request;
@@ -43,11 +50,26 @@ int main(int argc, char *argv[])
        int count;
        const char *dev;
 
+       for (;;) {
+               int c;
+
+               c = getopt_long(argc, argv, short_opts, long_opts, NULL);
+               if (c == EOF)
+                       break;
+
+               switch (c) {
+               case 'h':
+                       usage(0);
+                       break;
+               default:
+                       usage(1);
+                       break;
+               }
+       }
+
        /* Parse command line options */
        if (argc < 2 || argc > 4)
                usage(1);
-       if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))
-               usage(0);
 
        dev = argv[1];