Collapse CLI flags into a single int.
authorjbj <devnull@localhost>
Wed, 2 Oct 2002 22:38:00 +0000 (22:38 +0000)
committerjbj <devnull@localhost>
Wed, 2 Oct 2002 22:38:00 +0000 (22:38 +0000)
CVS patchset: 5749
CVS date: 2002/10/02 22:38:00

file/.cvsignore
file/ascmagic.c
file/file.c
file/file.h
file/fsmagic.c
file/softmagic.c

index 7f278be..e8267a6 100644 (file)
@@ -1,3 +1,5 @@
+*.lo
+*.la
 .deps
 .depend
 .depend-done
index 5eff3a9..86fd9af 100644 (file)
@@ -436,10 +436,10 @@ ascmagic(unsigned char *buf, int nbytes)
         */
        switch (is_tar(buf, nbytes)) {
        case 1:
-               ckfputs(iflag ? "application/x-tar" : "tar archive", stdout);
+               ckfputs((fmagic_flags & FMAGIC_FLAGS_MIME) ? "application/x-tar" : "tar archive", stdout);
                return 1;
        case 2:
-               ckfputs(iflag ? "application/x-tar, POSIX"
+               ckfputs((fmagic_flags & FMAGIC_FLAGS_MIME) ? "application/x-tar, POSIX"
                                : "POSIX tar archive", stdout);
                return 1;
        }
@@ -594,7 +594,7 @@ subtype_identified:
                }
        }
 
-       if (iflag) {
+       if ((fmagic_flags & FMAGIC_FLAGS_MIME)) {
                if (subtype_mime != NULL)
                        ckfputs(subtype_mime, stdout);
                else
index f82c0e2..7eefc8c 100644 (file)
@@ -53,21 +53,6 @@ int os2_apptype (const char *fn, char *buf, int nb);
 
                        /* Global command-line options          */
 /*@unchecked@*/
-       int     debug = 0;      /* debugging                            */
-/*@unchecked@*/
-       int     bflag = 0;      /* brief output format                  */
-/*@unchecked@*/
-       int     iflag = 0;
-/*@unchecked@*/
-       int     kflag = 0;      /* Keep going after the first match     */
-/*@unchecked@*/
-       int     lflag = 0;      /* follow Symlinks (BSD only)           */
-/*@unchecked@*/
-       int     sflag = 0;      /* read block special files             */
-/*@unchecked@*/
-       int     zflag = 0;      /* follow (uncompress) compressed files */
-
-/*@unchecked@*/
 static int     nobuffer = 0;   /* Do not buffer stdout */
 
 /*@unchecked@*/ /*@null@*/
@@ -171,10 +156,10 @@ help(void)
  */
 int
 main(int argc, char **argv)
-       /*@globals debug, bflag, zflag, sflag, iflag, nobuffer, kflag,
+       /*@globals fmagic_flags, nobuffer,
                default_magicfile, lineno, magicfile, mlist, optind, progname,
                fileSystem, internalState @*/
-       /*@modifies argv, debug, bflag, zflag, sflag, iflag, nobuffer, kflag,
+       /*@modifies argv, fmagic_flags, nobuffer,
                default_magicfile, lineno, magicfile, mlist, optind, progname,
                fileSystem, internalState @*/
 {
@@ -262,7 +247,7 @@ main(int argc, char **argv)
                        /*@switchbreak@*/ break;
 #endif
                case 'b':
-                       ++bflag;
+                       fmagic_flags |= FMAGIC_FLAGS_BRIEF;
                        /*@switchbreak@*/ break;
                case 'c':
                        action = CHECK;
@@ -271,7 +256,7 @@ main(int argc, char **argv)
                        action = COMPILE;
                        /*@switchbreak@*/ break;
                case 'd':
-                       ++debug;
+                       fmagic_flags |= FMAGIC_FLAGS_DEBUG;
                        /*@switchbreak@*/ break;
                case 'f':
                        if (!app) {
@@ -284,14 +269,14 @@ main(int argc, char **argv)
                        ++didsomefiles;
                        /*@switchbreak@*/ break;
                case 'i':
-                       iflag++;
+                       fmagic_flags |= FMAGIC_FLAGS_MIME;
                        mime = malloc(strlen(magicfile) + sizeof(".mime"));
                        (void)strcpy(mime, magicfile);
                        (void)strcat(mime, ".mime");
                        magicfile = mime;
                        /*@switchbreak@*/ break;
                case 'k':
-                       kflag = 1;
+                       fmagic_flags |= FMAGIC_FLAGS_CONTINUE;
                        /*@switchbreak@*/ break;
                case 'm':
                        magicfile = optarg;
@@ -300,7 +285,7 @@ main(int argc, char **argv)
                        ++nobuffer;
                        /*@switchbreak@*/ break;
                case 's':
-                       sflag++;
+                       fmagic_flags |= FMAGIC_FLAGS_SPECIAL;
                        /*@switchbreak@*/ break;
                case 'v':
                        (void) fprintf(stdout, "%s-%d.%d\n", progname,
@@ -309,11 +294,11 @@ main(int argc, char **argv)
                                       magicfile);
                        return 1;
                case 'z':
-                       zflag++;
+                       fmagic_flags |= FMAGIC_FLAGS_UNCOMPRESS;
                        /*@switchbreak@*/ break;
 #ifdef S_IFLNK
                case 'L':
-                       ++lflag;
+                       fmagic_flags |= FMAGIC_FLAGS_FOLLOW;
                        /*@switchbreak@*/ break;
 #endif
                case '?':
@@ -323,9 +308,8 @@ main(int argc, char **argv)
                }
        }
 
-       if (errflg) {
+       if (errflg)
                usage();
-       }
 
        if (!app) {
                ret = apprentice(magicfile, action);
@@ -335,11 +319,9 @@ main(int argc, char **argv)
        }
 
        if (optind == argc) {
-               if (!didsomefiles) {
+               if (!didsomefiles)
                        usage();
-               }
-       }
-       else {
+       } else {
                int i, wid, nw;
                for (wid = 0, i = optind; i < argc; i++) {
                        nw = strlen(argv[i]);
@@ -355,5 +337,6 @@ main(int argc, char **argv)
        muntrace(); /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */
        /*@=noeffect@*/
 #endif
+
        return 0;
 }
index 2c03883..0a04ad1 100644 (file)
@@ -124,22 +124,18 @@ extern int lineno;                /* current line number in magic file    */
 /*@unchecked@*/
 extern struct mlist mlist;     /* list of arrays of magic entries      */
 
-/*@-exportlocal@*/
-/*@unchecked@*/
-extern int debug;              /* enable debugging?                    */
-/*@unchecked@*/
-extern int bflag;              /* brief output format                  */
-/*@unchecked@*/
-extern int iflag;              /* Output types as mime-types           */
-/*@unchecked@*/
-extern int kflag;              /* Keep going after the first match     */
-/*@unchecked@*/
-extern int lflag;              /* follow symbolic links?               */
-/*@unchecked@*/
-extern int sflag;              /* read/analyze block special files?    */
-/*@unchecked@*/
-extern int zflag;              /* process compressed files?            */
-/*@=exportlocal@*/
+enum fmagic_e {
+    FMAGIC_FLAGS_NONE          = 0,
+    FMAGIC_FLAGS_DEBUG         = (1 << 0),
+    FMAGIC_FLAGS_BRIEF         = (1 << 1),     /*!< brief output format */
+    FMAGIC_FLAGS_MIME          = (1 << 2),     /*!< output as mime-types */
+    FMAGIC_FLAGS_CONTINUE      = (1 << 3),     /*!< continue after 1st match */
+    FMAGIC_FLAGS_FOLLOW                = (1 << 4),     /*!< follow symlinks? */
+    FMAGIC_FLAGS_SPECIAL       = (1 << 5),     /*!< analyze block devices? */
+    FMAGIC_FLAGS_UNCOMPRESS    = (1 << 6)      /*!< uncompress files? */
+};
+
+extern int fmagic_flags;
 
 /*@mayexit@*/
 extern int   apprentice(const char *fn, int action)
index 7b8780d..60de1f1 100644 (file)
@@ -31,6 +31,8 @@
 
 FILE_RCSID("@(#)Id: fsmagic.c,v 1.36 2002/07/03 19:00:41 christos Exp ")
 
+int fmagic_flags = 0;
+
 int
 fsmagic(const char *fn, struct stat *sb)
 {
@@ -41,7 +43,7 @@ fsmagic(const char *fn, struct stat *sb)
         * On 4.2BSD and similar systems, use lstat() to identify symlinks.
         */
 #if defined(S_IFLNK) || defined(__LCLINT__)
-       if (!lflag)
+       if (!(fmagic_flags & FMAGIC_FLAGS_FOLLOW))
                ret = lstat(fn, sb);
        else
 #endif
@@ -55,7 +57,7 @@ fsmagic(const char *fn, struct stat *sb)
                return 1;
        }
 
-       if (iflag) {
+       if ((fmagic_flags & FMAGIC_FLAGS_MIME)) {
                if ((sb->st_mode & S_IFMT) != S_IFREG) {
                        ckfputs("application/x-not-regular-file", stdout);
                        return 1;
@@ -84,7 +86,7 @@ fsmagic(const char *fn, struct stat *sb)
                 * like ordinary files.  Otherwise, just report that they
                 * are block special files and go on to the next file.
                 */
-               if (sflag)
+               if ((fmagic_flags & FMAGIC_FLAGS_SPECIAL))
                        break;
 #ifdef HAVE_STRUCT_STAT_ST_RDEV
 # ifdef dv_unit
@@ -108,7 +110,7 @@ fsmagic(const char *fn, struct stat *sb)
                 * like ordinary files.  Otherwise, just report that they
                 * are block special files and go on to the next file.
                 */
-               if (sflag)
+               if ((fmagic_flags & FMAGIC_FLAGS_SPECIAL))
                        break;
 #ifdef HAVE_STRUCT_STAT_ST_RDEV
 # ifdef dv_unit
@@ -182,7 +184,7 @@ fsmagic(const char *fn, struct stat *sb)
 /*@=branchstate@*/
 
                        /* Otherwise, handle it. */
-                       if (lflag) {
+                       if ((fmagic_flags & FMAGIC_FLAGS_FOLLOW)) {
                                process(buf, strlen(buf));
                                return 1;
                        } else { /* just print what it points to */
@@ -218,8 +220,8 @@ fsmagic(const char *fn, struct stat *sb)
         * the fact that it is empty will be detected and reported correctly
         * when we read the file.)
         */
-       if (!sflag && sb->st_size == 0) {
-               ckfputs(iflag ? "application/x-empty" : "empty", stdout);
+       if (!(fmagic_flags & FMAGIC_FLAGS_SPECIAL) && sb->st_size == 0) {
+               ckfputs((fmagic_flags & FMAGIC_FLAGS_MIME) ? "application/x-empty" : "empty", stdout);
                return 1;
        }
        return 0;
@@ -255,7 +257,7 @@ tryit(const char *fn, unsigned char *buf, int nb, int zfl)
                return 'a';
 
        /* abandon hope, all ye who remain here */
-       ckfputs(iflag ? "application/octet-stream" : "data", stdout);
+       ckfputs((fmagic_flags & FMAGIC_FLAGS_MIME) ? "application/octet-stream" : "data", stdout);
                return '\0';
 }
 
@@ -281,7 +283,7 @@ process(const char *inname, int wid)
                inname = stdname;
        }
 
-       if (wid > 0 && !bflag)
+       if (wid > 0 && !(fmagic_flags & FMAGIC_FLAGS_BRIEF))
             (void) printf("%s:%*s ", inname, 
                           (int) (wid - strlen(inname)), "");
 
@@ -314,10 +316,10 @@ process(const char *inname, int wid)
        }
 
        if (nbytes == 0)
-               ckfputs(iflag ? "application/x-empty" : "empty", stdout);
+               ckfputs((fmagic_flags & FMAGIC_FLAGS_MIME) ? "application/x-empty" : "empty", stdout);
        else {
                buf[nbytes++] = '\0';   /* null-terminate it */
-               match = tryit(inname, buf, nbytes, zflag);
+               match = tryit(inname, buf, nbytes, (fmagic_flags & FMAGIC_FLAGS_UNCOMPRESS));
        }
 
 #ifdef BUILTIN_ELF
index 4f0e7a3..ad1d0b6 100644 (file)
@@ -426,7 +426,7 @@ sm_get(union VALUETYPE *p, unsigned char *s, struct magic *m, int nbytes)
        }
 /*@=branchstate@*/
 
-       if (debug) {
+       if (fmagic_flags & FMAGIC_FLAGS_DEBUG) {
                sm_debug(offset, (char *) p, sizeof(union VALUETYPE));
                mdump(m);
        }
@@ -760,7 +760,7 @@ sm_get(union VALUETYPE *p, unsigned char *s, struct magic *m, int nbytes)
 
                memcpy(p, s + offset, sizeof(union VALUETYPE));
 
-               if (debug) {
+               if (fmagic_flags & FMAGIC_FLAGS_DEBUG) {
                        sm_debug(offset, (char *) p, sizeof(union VALUETYPE));
                        mdump(m);
                }
@@ -882,21 +882,21 @@ sm_check(union VALUETYPE *p, struct magic *m)
 
        switch (m->reln) {
        case 'x':
-               if (debug)
+               if (fmagic_flags & FMAGIC_FLAGS_DEBUG)
                        (void) fprintf(stderr, "%u == *any* = 1\n", v);
                matched = 1;
                break;
 
        case '!':
                matched = v != l;
-               if (debug)
+               if (fmagic_flags & FMAGIC_FLAGS_DEBUG)
                        (void) fprintf(stderr, "%u != %u = %d\n",
                                       v, l, matched);
                break;
 
        case '=':
                matched = v == l;
-               if (debug)
+               if (fmagic_flags & FMAGIC_FLAGS_DEBUG)
                        (void) fprintf(stderr, "%u == %u = %d\n",
                                       v, l, matched);
                break;
@@ -904,13 +904,13 @@ sm_check(union VALUETYPE *p, struct magic *m)
        case '>':
                if (m->flag & UNSIGNED) {
                        matched = v > l;
-                       if (debug)
+                       if (fmagic_flags & FMAGIC_FLAGS_DEBUG)
                                (void) fprintf(stderr, "%u > %u = %d\n",
                                               v, l, matched);
                }
                else {
                        matched = (int32_t) v > (int32_t) l;
-                       if (debug)
+                       if (fmagic_flags & FMAGIC_FLAGS_DEBUG)
                                (void) fprintf(stderr, "%d > %d = %d\n",
                                               v, l, matched);
                }
@@ -919,13 +919,13 @@ sm_check(union VALUETYPE *p, struct magic *m)
        case '<':
                if (m->flag & UNSIGNED) {
                        matched = v < l;
-                       if (debug)
+                       if (fmagic_flags & FMAGIC_FLAGS_DEBUG)
                                (void) fprintf(stderr, "%u < %u = %d\n",
                                               v, l, matched);
                }
                else {
                        matched = (int32_t) v < (int32_t) l;
-                       if (debug)
+                       if (fmagic_flags & FMAGIC_FLAGS_DEBUG)
                                (void) fprintf(stderr, "%d < %d = %d\n",
                                               v, l, matched);
                }
@@ -933,14 +933,14 @@ sm_check(union VALUETYPE *p, struct magic *m)
 
        case '&':
                matched = (v & l) == l;
-               if (debug)
+               if (fmagic_flags & FMAGIC_FLAGS_DEBUG)
                        (void) fprintf(stderr, "((%x & %x) == %x) = %d\n",
                                       v, l, l, matched);
                break;
 
        case '^':
                matched = (v & l) != l;
-               if (debug)
+               if (fmagic_flags & FMAGIC_FLAGS_DEBUG)
                        (void) fprintf(stderr, "((%x & %x) != %x) = %d\n",
                                       v, l, l, matched);
                break;
@@ -1073,7 +1073,7 @@ sm_match(struct magic *m, uint32_t nmagic, unsigned char *s, int nbytes)
                }
                firstline = 0;
                returnval = 1;
-               if (!kflag)     /* don't keep searching */
+               if (!(fmagic_flags & FMAGIC_FLAGS_CONTINUE))    /* don't keep searching */
                        return 1;
        }
        return returnval;  /* This is hit if -k is set or there is no match */