OSDN Git Service

Use getopt() for options parsing.
authorresver@gmail.com <resver@gmail.com@60bc1c72-a15a-11de-b98f-4500b42dc123>
Fri, 29 Mar 2013 06:29:10 +0000 (06:29 +0000)
committerresver@gmail.com <resver@gmail.com@60bc1c72-a15a-11de-b98f-4500b42dc123>
Fri, 29 Mar 2013 06:29:10 +0000 (06:29 +0000)
git-svn-id: http://exfat.googlecode.com/svn/trunk@349 60bc1c72-a15a-11de-b98f-4500b42dc123

dump/main.c
fsck/main.c
fuse/main.c
mkfs/main.c

index b4fad19..8650d51 100644 (file)
@@ -146,7 +146,7 @@ static void usage(const char* prog)
 
 int main(int argc, char* argv[])
 {
-       char** pp;
+       int opt;
        const char* spec = NULL;
        bool sb_only = false;
        bool used_sectors = false;
@@ -154,24 +154,26 @@ int main(int argc, char* argv[])
        printf("dumpexfat %u.%u.%u\n",
                        EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH);
 
-       for (pp = argv + 1; *pp; pp++)
+       while ((opt = getopt(argc, argv, "suV")) != -1)
        {
-               if (strcmp(*pp, "-s") == 0)
+               switch (opt)
+               {
+               case 's':
                        sb_only = true;
-               else if (strcmp(*pp, "-u") == 0)
+                       break;
+               case 'u':
                        used_sectors = true;
-               else if (strcmp(*pp, "-V") == 0)
-               {
+                       break;
+               case 'V':
                        puts("Copyright (C) 2011-2013  Andrew Nayenko");
                        return 0;
-               }
-               else if (spec == NULL)
-                       spec = *pp;
-               else
+               default:
                        usage(argv[0]);
+               }
        }
-       if (spec == NULL)
+       if (argc - optind != 1)
                usage(argv[0]);
+       spec = argv[optind];
 
        if (sb_only)
                return dump_sb(spec);
index 8e305d3..421bd1d 100644 (file)
@@ -23,6 +23,7 @@
 #include <exfat.h>
 #include <exfatfs.h>
 #include <inttypes.h>
+#include <unistd.h>
 
 #define exfat_debug(format, ...)
 
@@ -130,27 +131,28 @@ static void usage(const char* prog)
 
 int main(int argc, char* argv[])
 {
-       char** pp;
+       int opt;
        const char* spec = NULL;
        struct exfat ef;
 
        printf("exfatfsck %u.%u.%u\n",
                        EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH);
 
-       for (pp = argv + 1; *pp; pp++)
+       while ((opt = getopt(argc, argv, "V")) != -1)
        {
-               if (strcmp(*pp, "-V") == 0)
+               switch (opt)
                {
+               case 'V':
                        puts("Copyright (C) 2011-2013  Andrew Nayenko");
                        return 0;
-               }
-               else if (spec == NULL)
-                       spec = *pp;
-               else
+               default:
                        usage(argv[0]);
+                       break;
+               }
        }
-       if (spec == NULL)
+       if (argc - optind != 1)
                usage(argv[0]);
+       spec = argv[optind];
 
        if (exfat_mount(&ef, spec, "ro") != 0)
                return 1;
index 9b59c54..6ce4999 100644 (file)
@@ -398,7 +398,7 @@ int main(int argc, char* argv[])
        int debug = 0;
        struct fuse_chan* fc = NULL;
        struct fuse* fh = NULL;
-       char** pp;
+       int opt;
 
        printf("FUSE exfat %u.%u.%u\n",
                        EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH);
@@ -410,42 +410,37 @@ int main(int argc, char* argv[])
                return 1;
        }
 
-       for (pp = argv + 1; *pp; pp++)
+       while ((opt = getopt(argc, argv, "dno:V")) != -1)
        {
-               if (strcmp(*pp, "-o") == 0)
+               switch (opt)
                {
-                       pp++;
-                       if (*pp == NULL)
-                               usage(argv[0]);
-                       mount_options = add_option(mount_options, *pp, NULL);
+               case 'd':
+                       debug = 1;
+                       break;
+               case 'n':
+                       break;
+               case 'o':
+                       mount_options = add_option(mount_options, optarg, NULL);
                        if (mount_options == NULL)
                                return 1;
-               }
-               else if (strcmp(*pp, "-d") == 0)
-                       debug = 1;
-               else if (strcmp(*pp, "-V") == 0)
-               {
+                       break;
+               case 'V':
                        free(mount_options);
                        puts("Copyright (C) 2010-2013  Andrew Nayenko");
                        return 0;
-               }
-               else if (strcmp(*pp, "-n") == 0)
-                       /* ignore */ ;
-               else if (spec == NULL)
-                       spec = *pp;
-               else if (mount_point == NULL)
-                       mount_point = *pp;
-               else
-               {
+               default:
                        free(mount_options);
                        usage(argv[0]);
+                       break;
                }
        }
-       if (spec == NULL || mount_point == NULL)
+       if (argc - optind != 2)
        {
                free(mount_options);
                usage(argv[0]);
        }
+       spec = argv[optind];
+       mount_point = argv[optind + 1];
 
        if (exfat_mount(&ef, spec, mount_options) != 0)
        {
index 247cc49..28b3ec1 100644 (file)
@@ -195,7 +195,7 @@ static void usage(const char* prog)
 int main(int argc, char* argv[])
 {
        const char* spec = NULL;
-       char** pp;
+       int opt;
        int spc_bits = -1;
        const char* volume_label = NULL;
        uint32_t volume_serial = 0;
@@ -205,53 +205,38 @@ int main(int argc, char* argv[])
        printf("mkexfatfs %u.%u.%u\n",
                        EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH);
 
-       for (pp = argv + 1; *pp; pp++)
+       while ((opt = getopt(argc, argv, "i:n:p:s:V")) != -1)
        {
-               if (strcmp(*pp, "-s") == 0)
+               switch (opt)
                {
-                       pp++;
-                       if (*pp == NULL)
-                               usage(argv[0]);
-                       spc_bits = logarithm2(atoi(*pp));
+               case 'i':
+                       volume_serial = strtol(optarg, NULL, 16);
+                       break;
+               case 'n':
+                       volume_label = optarg;
+                       break;
+               case 'p':
+                       first_sector = strtoll(optarg, NULL, 10);
+                       break;
+               case 's':
+                       spc_bits = logarithm2(atoi(optarg));
                        if (spc_bits < 0)
                        {
-                               exfat_error("invalid option value: `%s'", *pp);
+                               exfat_error("invalid option value: `%s'", optarg);
                                return 1;
                        }
-               }
-               else if (strcmp(*pp, "-n") == 0)
-               {
-                       pp++;
-                       if (*pp == NULL)
-                               usage(argv[0]);
-                       volume_label = *pp;
-               }
-               else if (strcmp(*pp, "-i") == 0)
-               {
-                       pp++;
-                       if (*pp == NULL)
-                               usage(argv[0]);
-                       volume_serial = strtol(*pp, NULL, 16);
-               }
-               else if (strcmp(*pp, "-p") == 0)
-               {
-                       pp++;
-                       if (*pp == NULL)
-                               usage(argv[0]);
-                       first_sector = strtoll(*pp, NULL, 10);
-               }
-               else if (strcmp(*pp, "-V") == 0)
-               {
+                       break;
+               case 'V':
                        puts("Copyright (C) 2011-2013  Andrew Nayenko");
                        return 0;
-               }
-               else if (spec == NULL)
-                       spec = *pp;
-               else
+               default:
                        usage(argv[0]);
+                       break;
+               }
        }
-       if (spec == NULL)
+       if (argc - optind != 1)
                usage(argv[0]);
+       spec = argv[optind];
 
        dev = exfat_open(spec, EXFAT_MODE_RW);
        if (dev == NULL)