]> granicus.if.org Git - file/commitdiff
Fix bug with long options and explicitly number them to avoid this in
authorChristos Zoulas <christos@zoulas.com>
Thu, 11 Jun 2015 12:52:32 +0000 (12:52 +0000)
committerChristos Zoulas <christos@zoulas.com>
Thu, 11 Jun 2015 12:52:32 +0000 (12:52 +0000)
the future.

src/file.c
src/file_opts.h

index 2b15e21232d1afde6c3f11208f93f4b67ed78390..44f4ccea57351d5e56cc4c1e366d6a7d4195a5c2 100644 (file)
@@ -32,7 +32,7 @@
 #include "file.h"
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: file.c,v 1.164 2015/06/03 18:21:24 christos Exp $")
+FILE_RCSID("@(#)$File: file.c,v 1.165 2015/06/11 12:52:32 christos Exp $")
 #endif /* lint */
 
 #include "magic.h"
@@ -89,10 +89,15 @@ private int                 /* Global command-line options          */
 
 private const char *separator = ":";   /* Default field separator      */
 private const struct option long_options[] = {
+#define OPT_HELP               1
+#define OPT_APPLE              2
+#define OPT_EXTENSIONS         3
+#define OPT_MIME_TYPE          4
+#define OPT_MIME_ENCODING      5
 #define OPT(shortname, longname, opt, doc)      \
     {longname, opt, NULL, shortname},
-#define OPT_LONGONLY(longname, opt, doc)        \
-    {longname, opt, NULL, 0},
+#define OPT_LONGONLY(longname, opt, doc, id)        \
+    {longname, opt, NULL, id},
 #include "file_opts.h"
 #undef OPT
 #undef OPT_LONGONLY
@@ -182,24 +187,20 @@ main(int argc, char *argv[])
        while ((c = getopt_long(argc, argv, OPTSTRING, long_options,
            &longindex)) != -1)
                switch (c) {
-               case 0 :
-                       switch (longindex) {
-                       case 0:
-                               help();
-                               break;
-                       case 10:
-                               flags |= MAGIC_APPLE;
-                               break;
-                       case 11:
-                               flags |= MAGIC_EXTENSION;
-                               break;
-                       case 12:
-                               flags |= MAGIC_MIME_TYPE;
-                               break;
-                       case 13:
-                               flags |= MAGIC_MIME_ENCODING;
-                               break;
-                       }
+               case OPT_HELP:
+                       help();
+                       break;
+               case OPT_APPLE:
+                       flags |= MAGIC_APPLE;
+                       break;
+               case OPT_EXTENSIONS:
+                       flags |= MAGIC_EXTENSION;
+                       break;
+               case OPT_MIME_TYPE:
+                       flags |= MAGIC_MIME_TYPE;
+                       break;
+               case OPT_MIME_ENCODING:
+                       flags |= MAGIC_MIME_ENCODING;
                        break;
                case '0':
                        nulsep = 1;
@@ -595,7 +596,7 @@ help(void)
 #define OPT(shortname, longname, opt, doc)      \
        fprintf(stdout, "  -%c, --" longname, shortname), \
        docprint(doc);
-#define OPT_LONGONLY(longname, opt, doc)        \
+#define OPT_LONGONLY(longname, opt, doc, id)        \
        fprintf(stdout, "      --" longname),   \
        docprint(doc);
 #include "file_opts.h"
index 036505fe1dd5c04da03d37866a1cf304b42ee1bd..2e30d0632041a6400c66fba2671494509dad14c5 100644 (file)
@@ -12,7 +12,7 @@
  * switch statement!
  */
 
-OPT_LONGONLY("help", 0, "                 display this help and exit\n")
+OPT_LONGONLY("help", 0, "                 display this help and exit\n", OPT_HELP)
 OPT('v', "version", 0, "              output version information and exit\n")
 OPT('m', "magic-file", 1, " LIST      use LIST as a colon-separated list of magic\n"
     "                               number files\n")
@@ -29,10 +29,10 @@ OPT('f', "files-from", 1, " FILE      read the filenames to be examined from FIL
 OPT('F', "separator", 1, " STRING     use string as separator instead of `:'\n")
 OPT('i', "mime", 0, "                 output MIME type strings (--mime-type and\n"
     "                               --mime-encoding)\n")
-OPT_LONGONLY("apple", 0, "                output the Apple CREATOR/TYPE\n")
-OPT_LONGONLY("extension", 0, "            output a slash-separated list of extnsions\n")
-OPT_LONGONLY("mime-type", 0, "            output the MIME type\n")
-OPT_LONGONLY("mime-encoding", 0, "        output the MIME encoding\n")
+OPT_LONGONLY("apple", 0, "                output the Apple CREATOR/TYPE\n", OPT_APPLE)
+OPT_LONGONLY("extension", 0, "            output a slash-separated list of extensions\n", OPT_EXTENSIONS)
+OPT_LONGONLY("mime-type", 0, "            output the MIME type\n", OPT_MIME_TYPE)
+OPT_LONGONLY("mime-encoding", 0, "        output the MIME encoding\n", OPT_MIME_ENCODING)
 OPT('k', "keep-going", 0, "           don't stop at the first match\n")
 OPT('l', "list", 0, "                 list magic strength\n")
 #ifdef S_IFLNK