From: Christos Zoulas Date: Fri, 27 Oct 1995 23:12:01 +0000 (+0000) Subject: Added colon separated list of magic files X-Git-Tag: FILE3_27~92 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3da1d261db147bf2e6119a6c182dcdf510ce8409;p=file Added colon separated list of magic files --- diff --git a/doc/file.man b/doc/file.man index 2d2aa1a9..a1d33202 100644 --- a/doc/file.man +++ b/doc/file.man @@ -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. diff --git a/src/apprentice.c b/src/apprentice.c index 08b5055e..d637399d 100644 --- a/src/apprentice.c +++ b/src/apprentice.c @@ -29,11 +29,12 @@ #include #include #include +#include #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; } /* diff --git a/src/file.c b/src/file.c index 8e480f97..0b617e16 100644 --- a/src/file.c +++ b/src/file.c @@ -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 @@ -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