]> granicus.if.org Git - file/commitdiff
Added colon separated list of magic files
authorChristos Zoulas <christos@zoulas.com>
Fri, 27 Oct 1995 23:12:01 +0000 (23:12 +0000)
committerChristos Zoulas <christos@zoulas.com>
Fri, 27 Oct 1995 23:12:01 +0000 (23:12 +0000)
doc/file.man
src/apprentice.c
src/file.c

index 2d2aa1a9b41ee75465d49b9e12f9e51fc83b6235..a1d33202ec4b7ec011731f2b125b5e2c529893ad 100644 (file)
@@ -1,5 +1,5 @@
 .TH FILE __CSECTION__ "Copyright but distributable"
-.\" $Id: file.man,v 1.25 1995/04/28 17:29:13 christos Exp $
+.\" $Id: file.man,v 1.26 1995/10/27 23:12:01 christos Exp $
 .SH NAME
 file
 \- determine file type
@@ -13,7 +13,7 @@ file
 namefile ]
 [
 .B \-m 
-magicfile ]
+magicfiles ]
 file ...
 .SH DESCRIPTION
 .I File
@@ -100,8 +100,9 @@ labelled as `ascii text' or `data'.
 .B \-v
 Print the version of the program and exit.
 .TP 8
-.B \-m file
-Specify an alternate file of magic numbers.
+.B \-m list
+Specify an alternate list of files containing magic numbers.
+This can be a single file, or a colon-separated list of files.
 .TP 8
 .B \-z
 Try to look inside compressed files.
@@ -132,7 +133,7 @@ option causes symlinks to be followed, as the like-named option in
 .SH ENVIRONMENT
 The environment variable
 .B MAGIC
-can be used to set the default magic number file.
+can be used to set the default magic number files.
 .SH SEE ALSO
 .IR magic (__FSECTION__)
 \- description of magic file format.
index 08b5055e260725291d1a4003e1a951d07b2879cc..d637399d58e2749e09557ff4ae982d1c60eebb7b 100644 (file)
 #include <stdlib.h>
 #include <string.h>
 #include <ctype.h>
+#include <errno.h>
 #include "file.h"
 
 #ifndef        lint
 static char *moduleid = 
-       "@(#)$Id: apprentice.c,v 1.22 1995/05/20 22:09:21 christos Exp $";
+       "@(#)$Id: apprentice.c,v 1.23 1995/10/27 23:12:01 christos Exp $";
 #endif /* lint */
 
 #define        EATAB {while (isascii((unsigned char) *l) && \
@@ -50,39 +51,70 @@ static void eatsize __P((char **));
 
 static int maxmagic = 0;
 
+static int apprentice_1        __P((char *, int));
 
 int
 apprentice(fn, check)
-char *fn;                      /* name of magic file */
+char *fn;                      /* list of magic files */
 int check;                     /* non-zero? checking-only run. */
 {
-       FILE *f;
-       char line[BUFSIZ+1];
-       int errs = 0;
-
-       f = fopen(fn, "r");
-       if (f==NULL) {
-               (void) fprintf(stderr, "%s: can't read magic file %s\n",
-               progname, fn);
-               if (check)
-                       return -1;
-               else
-                       exit(1);
-       }
+       char *p, *mfn;
+       int file_err, errs = -1;
 
         maxmagic = MAXMAGIS;
-       if ((magic = (struct magic *) calloc(sizeof(struct magic), maxmagic))
-           == NULL) {
+       magic = (struct magic *) calloc(sizeof(struct magic), maxmagic);
+       mfn = malloc(strlen(fn)+1);
+       if (magic == NULL || mfn == NULL) {
                (void) fprintf(stderr, "%s: Out of memory.\n", progname);
                if (check)
                        return -1;
                else
                        exit(1);
        }
+       fn = strcpy(mfn, fn);
   
+       while (fn) {
+               p = strchr(fn, ':');
+               if (p)
+                       *p++ = '\0';
+               file_err = apprentice_1(fn, check);
+               if (file_err > errs)
+                       errs = file_err;
+               fn = p;
+       }
+       if (errs == -1)
+               (void) fprintf(stderr, "%s: couldn't find any magic files!\n",
+                              progname);
+       if (!check && errs)
+               exit(1);
+
+       free(mfn);
+       return errs;
+}
+
+static int
+apprentice_1(fn, check)
+char *fn;                      /* name of magic file */
+int check;                     /* non-zero? checking-only run. */
+{
+       static const char hdr[] =
+               "cont\toffset\ttype\topcode\tmask\tvalue\tdesc";
+       FILE *f;
+       char line[BUFSIZ+1];
+       int errs = 0;
+
+       f = fopen(fn, "r");
+       if (f==NULL) {
+               if (errno != ENOENT)
+                       (void) fprintf(stderr,
+                       "%s: can't read magic file %s (%s)\n", 
+                       progname, fn, strerror(errno));
+               return -1;
+       }
+
        /* parse it */
        if (check)      /* print silly verbose header for USG compat. */
-               (void) printf("cont\toffset\ttype\topcode\tmask\tvalue\tdesc\n");
+               (void) printf("%s\n", hdr);
 
        for (lineno = 1;fgets(line, BUFSIZ, f) != NULL; lineno++) {
                if (line[0]=='#')       /* comment, do not parse */
@@ -91,11 +123,11 @@ int check;                 /* non-zero? checking-only run. */
                        continue;
                line[strlen(line)-1] = '\0'; /* delete newline */
                if (parse(line, &nmagic, check) != 0)
-                       ++errs;
+                       errs = 1;
        }
 
        (void) fclose(f);
-       return errs ? -1 : 0;
+       return errs;
 }
 
 /*
index 8e480f974f07ae1202d0ec47cdf8b11ce4b630dc..0b617e16dd879ab71ab8f93b2f71b60d4b561611 100644 (file)
@@ -26,7 +26,7 @@
  */
 #ifndef        lint
 static char *moduleid = 
-       "@(#)$Id: file.c,v 1.33 1995/05/20 22:09:21 christos Exp $";
+       "@(#)$Id: file.c,v 1.34 1995/10/27 23:12:01 christos Exp $";
 #endif /* lint */
 
 #include <stdio.h>
@@ -51,9 +51,9 @@ static char *moduleid =
 #include "file.h"
 
 #ifdef S_IFLNK
-# define USAGE  "Usage: %s [-vczL] [-f namefile] [-m magicfile] file...\n"
+# define USAGE  "Usage: %s [-vczL] [-f namefile] [-m magicfiles] file...\n"
 #else
-# define USAGE  "Usage: %s [-vcz] [-f namefile] [-m magicfile] file...\n"
+# define USAGE  "Usage: %s [-vcz] [-f namefile] [-m magicfiles] file...\n"
 #endif
 
 #ifndef MAGIC