From 3fec2112f7985602db97f6d156c334b84934ac2d Mon Sep 17 00:00:00 2001 From: jbj Date: Thu, 3 Oct 2002 22:07:30 +0000 Subject: [PATCH] Drill an opaque "fmagic" control structure through the API. Rename all functions to start with prefix "fmagic". CVS patchset: 5750 CVS date: 2002/10/03 22:07:30 --- file/.lclintrc | 3 ++ file/Makefile.am | 1 + file/apprentice.c | 76 +++++++++++++++++++++++---------------------- file/ascmagic.c | 16 +++++----- file/compress.c | 8 ++--- file/file.c | 66 +++++++++++++++++++++------------------ file/file.h | 78 ++++++++++++++++++++++++++--------------------- file/fsmagic.c | 42 ++++++++++++------------- file/print.c | 12 +++++++- file/softmagic.c | 77 ++++++++++++++++++++++++---------------------- 10 files changed, 209 insertions(+), 170 deletions(-) diff --git a/file/.lclintrc b/file/.lclintrc index 7cb34c94c..66644f981 100644 --- a/file/.lclintrc +++ b/file/.lclintrc @@ -14,6 +14,8 @@ +strict # lclint level # --- in progress ++slovak-fcns + -exportheader # 19 -formatconst # 9 -macroredef # 1 tar.h @@ -23,6 +25,7 @@ -redef # 4 -sizeoftype # 29 -uniondef # 6 +-warnmissingglobs # --- +partial artifacts diff --git a/file/Makefile.am b/file/Makefile.am index 1ce7704de..951df4a16 100644 --- a/file/Makefile.am +++ b/file/Makefile.am @@ -17,6 +17,7 @@ libfmagic_la_SOURCES = \ bin_PROGRAMS = file file_SOURCES = file.c +file_LDFLAGS = -all-static file_LDADD = libfmagic.la data_DATA = magic magic.mime magic.mgc magic.mime.mgc diff --git a/file/apprentice.c b/file/apprentice.c index 8e59db4fe..695296ca3 100644 --- a/file/apprentice.c +++ b/file/apprentice.c @@ -31,6 +31,8 @@ FILE_RCSID("@(#)Id: apprentice.c,v 1.49 2002/07/03 19:00:41 christos Exp ") +/*@access fmagic @*/ + #define EATAB {while (isascii((unsigned char) *l) && \ isspace((unsigned char) *l)) ++l;} #define LOWCASE(l) (isupper((unsigned char) (l)) ? \ @@ -63,9 +65,6 @@ FILE_RCSID("@(#)Id: apprentice.c,v 1.49 2002/07/03 19:00:41 christos Exp ") /*@unchecked@*/ static int maxmagic = 0; -/*@unchecked@*/ -struct mlist mlist; - /* * extend the sign bit if the comparison is to be signed */ @@ -780,10 +779,10 @@ mkdbname(const char *fn) * const char *fn: name of magic file */ static int -apprentice_file(/*@out@*/ struct magic **magicp, /*@out@*/ uint32_t *nmagicp, - const char *fn, int action) - /*@globals lineno, maxmagic, fileSystem @*/ - /*@modifies *magicp, *nmagicp, lineno, maxmagic, fileSystem @*/ +apprentice_file(fmagic fm, /*@out@*/ struct magic **magicp, + /*@out@*/ uint32_t *nmagicp, const char *fn, int action) + /*@globals maxmagic, fileSystem @*/ + /*@modifies fm, *magicp, *nmagicp, maxmagic, fileSystem @*/ { /*@observer@*/ static const char hdr[] = @@ -808,7 +807,7 @@ apprentice_file(/*@out@*/ struct magic **magicp, /*@out@*/ uint32_t *nmagicp, if (action == CHECK) /* print silly verbose header for USG compat. */ (void) printf("%s\n", hdr); - for (lineno = 1;fgets(line, BUFSIZ, f) != NULL; lineno++) { + for (fm->lineno = 1; fgets(line, BUFSIZ, f) != NULL; fm->lineno++) { if (line[0]=='#') /* comment, do not parse */ continue; if (strlen(line) <= (unsigned)1) /* null line, garbage, etc */ @@ -831,7 +830,8 @@ apprentice_file(/*@out@*/ struct magic **magicp, /*@out@*/ uint32_t *nmagicp, * handle an mmaped file. */ static int -apprentice_compile(/*@out@*/ struct magic **magicp, /*@out@*/ uint32_t *nmagicp, +apprentice_compile(/*@unused@*/ const fmagic fm, + /*@out@*/ struct magic **magicp, /*@out@*/ uint32_t *nmagicp, const char *fn, /*@unused@*/ int action) /*@globals fileSystem, internalState @*/ /*@modifies fileSystem, internalState @*/ @@ -879,7 +879,8 @@ apprentice_compile(/*@out@*/ struct magic **magicp, /*@out@*/ uint32_t *nmagicp, * handle a compiled file. */ static int -apprentice_map(/*@out@*/ struct magic **magicp, /*@out@*/ uint32_t *nmagicp, +apprentice_map(/*@unused@*/ const fmagic fm, + /*@out@*/ struct magic **magicp, /*@out@*/ uint32_t *nmagicp, const char *fn, /*@unused@*/ int action) /*@globals fileSystem, internalState @*/ /*@modifies *magicp, *nmagicp, fileSystem, internalState @*/ @@ -970,9 +971,9 @@ error: * Handle one file. */ static int -apprentice_1(const char *fn, int action) - /*@globals lineno, mlist, fileSystem, internalState @*/ - /*@modifies lineno, mlist, fileSystem, internalState @*/ +apprentice_1(fmagic fm, const char *fn, int action) + /*@globals fileSystem, internalState @*/ + /*@modifies fm, fileSystem, internalState @*/ { /*@-shadow@*/ struct magic *magic = NULL; @@ -982,19 +983,18 @@ apprentice_1(const char *fn, int action) int rv = -1; if (action == COMPILE) { - rv = apprentice_file(&magic, &nmagic, fn, action); - if (rv == 0) - return apprentice_compile(&magic, &nmagic, fn, action); - else + rv = apprentice_file(fm, &magic, &nmagic, fn, action); + if (rv) return rv; + return apprentice_compile(fm, &magic, &nmagic, fn, action); } #ifndef COMPILE_ONLY - if ((rv = apprentice_map(&magic, &nmagic, fn, action)) != 0) + if ((rv = apprentice_map(fm, &magic, &nmagic, fn, action)) != 0) (void)fprintf(stderr, "%s: Using regular magic file `%s'\n", progname, fn); if (rv != 0) - rv = apprentice_file(&magic, &nmagic, fn, action); + rv = apprentice_file(fm, &magic, &nmagic, fn, action); if (rv != 0) return rv; @@ -1007,42 +1007,46 @@ apprentice_1(const char *fn, int action) ml->magic = magic; ml->nmagic = nmagic; - mlist.prev->next = ml; - ml->prev = mlist.prev; + fm->mlist->prev->next = ml; + ml->prev = fm->mlist->prev; /*@-immediatetrans@*/ - ml->next = &mlist; + ml->next = fm->mlist; /*@=immediatetrans@*/ /*@-kepttrans@*/ - mlist.prev = ml; + fm->mlist->prev = ml; /*@=kepttrans@*/ -/*@-compdef@*/ +/*@-compdef -compmempass @*/ return rv; -/*@=compdef@*/ +/*@=compdef =compmempass @*/ #endif /* COMPILE_ONLY */ } /* const char *fn: list of magic files */ int -apprentice(const char *fn, int action) +fmagicSetup(fmagic fm, const char *fn, int action) { char *p, *mfn; int file_err, errs = -1; + if (fm->mlist == NULL) { + static struct mlist mlist; /*@-immediatetrans@*/ - mlist.next = &mlist; - mlist.prev = &mlist; + mlist.next = &mlist; + mlist.prev = &mlist; + fm->mlist = &mlist; /*@=immediatetrans@*/ + } - mfn = xmalloc(strlen(fn)+1); - fn = strcpy(mfn, fn); + mfn = xstrdup(fn); + fn = mfn; /*@-branchstate@*/ while (fn != NULL) { p = strchr(fn, PATHSEP); if (p != NULL) *p++ = '\0'; - file_err = apprentice_1(fn, action); + file_err = apprentice_1(fm, fn, action); if (file_err > errs) errs = file_err; fn = p; @@ -1061,14 +1065,12 @@ apprentice(const char *fn, int action) } #ifdef COMPILE_ONLY -const char *magicfile; -char *progname; -int lineno; - int main(int argc, char *argv[]) /*@*/ { + static struct fmagic_s myfmagic; + fmagic fm = &myfmagic; int ret; if ((progname = strrchr(argv[0], '/')) != NULL) @@ -1080,8 +1082,8 @@ main(int argc, char *argv[]) (void)fprintf(stderr, "usage: %s file\n", progname); exit(1); } - magicfile = argv[1]; + fm->magicfile = argv[1]; - exit(apprentice(magicfile, COMPILE)); + exit(apprentice(fm, fm->magicfile, COMPILE)); } #endif /* COMPILE_ONLY */ diff --git a/file/ascmagic.c b/file/ascmagic.c index 86fd9af45..0e90c25fd 100644 --- a/file/ascmagic.c +++ b/file/ascmagic.c @@ -41,6 +41,8 @@ FILE_RCSID("@(#)Id: ascmagic.c,v 1.32 2002/07/03 18:26:37 christos Exp ") +/*@access fmagic @*/ + typedef unsigned long unichar; #define MAXLINELEN 300 /* longest sane line length */ @@ -387,7 +389,7 @@ from_ebcdic(const unsigned char *buf, int nbytes, /*@out@*/ unsigned char *otp) } static int -ascmatch(const unsigned char *s, const unichar *us, int ulen) +fmagicAMatch(const unsigned char *s, const unichar *us, int ulen) /*@*/ { size_t i; @@ -405,7 +407,7 @@ ascmatch(const unsigned char *s, const unichar *us, int ulen) /* int nbytes: size actually read */ int -ascmagic(unsigned char *buf, int nbytes) +fmagicA(fmagic fm, unsigned char *buf, int nbytes) { int i; char nbuf[HOWMANY+1]; /* one extra for terminating '\0' */ @@ -436,16 +438,16 @@ ascmagic(unsigned char *buf, int nbytes) */ switch (is_tar(buf, nbytes)) { case 1: - ckfputs((fmagic_flags & FMAGIC_FLAGS_MIME) ? "application/x-tar" : "tar archive", stdout); + ckfputs((fm->flags & FMAGIC_FLAGS_MIME) ? "application/x-tar" : "tar archive", stdout); return 1; case 2: - ckfputs((fmagic_flags & FMAGIC_FLAGS_MIME) ? "application/x-tar, POSIX" + ckfputs((fm->flags & FMAGIC_FLAGS_MIME) ? "application/x-tar, POSIX" : "POSIX tar archive", stdout); return 1; } /* - * Undo the NUL-termination kindly provided by process() + * Undo the NUL-termination kindly provided by fmagicProcess() * but leave at least one byte to look at */ @@ -552,7 +554,7 @@ ascmagic(unsigned char *buf, int nbytes) * compare the word thus isolated against the token list */ for (p = names; p < names + NNAMES; p++) { - if (ascmatch(p->name, ubuf + i, end - i)) { + if (fmagicAMatch(p->name, ubuf + i, end - i)) { subtype = types[p->type].human; subtype_mime = types[p->type].mime; goto subtype_identified; @@ -594,7 +596,7 @@ subtype_identified: } } - if ((fmagic_flags & FMAGIC_FLAGS_MIME)) { + if ((fm->flags & FMAGIC_FLAGS_MIME)) { if (subtype_mime != NULL) ckfputs(subtype_mime, stdout); else diff --git a/file/compress.c b/file/compress.c index f45e89386..7073225e6 100644 --- a/file/compress.c +++ b/file/compress.c @@ -292,11 +292,11 @@ err: /* * compress routines: - * zmagic() - returns 0 if not recognized, uncompresses and prints + * fmagicZ() - returns 0 if not recognized, uncompresses and prints * information if recognized */ int -zmagic(const char *fname, unsigned char *buf, int nbytes) +fmagicZ(fmagic fm, const char *fname, unsigned char *buf, int nbytes) { unsigned char *newbuf; int newsize; @@ -307,10 +307,10 @@ zmagic(const char *fname, unsigned char *buf, int nbytes) continue; if (memcmp(buf, compr[i].magic, compr[i].maglen) == 0 && (newsize = uncompressbuf(i, buf, &newbuf, nbytes)) != 0) { - (void) tryit(fname, newbuf, newsize, 1); + (void) fmagicF(fm, fname, newbuf, newsize, 1); free(newbuf); printf(" ("); - (void) tryit(fname, buf, nbytes, 0); + (void) fmagicF(fm, fname, buf, nbytes, 0); printf(")"); return 1; } diff --git a/file/file.c b/file/file.c index 7eefc8c6b..fd2caaea2 100644 --- a/file/file.c +++ b/file/file.c @@ -32,6 +32,11 @@ FILE_RCSID("@(#)Id: file.c,v 1.66 2002/07/03 19:00:41 christos Exp ") +/*@access fmagic @*/ + +/*@unchecked@*/ +extern fmagic global_fmagic; + #ifdef S_IFLNK # define USAGE "Usage: %s [-bciknsvzL] [-f namefile] [-m magicfiles] file...\n" #else @@ -55,23 +60,19 @@ int os2_apptype (const char *fn, char *buf, int nb); /*@unchecked@*/ static int nobuffer = 0; /* Do not buffer stdout */ -/*@unchecked@*/ /*@null@*/ -const char *magicfile = 0; /* where the magic is */ /*@unchecked@*/ /*@observer@*/ static const char *default_magicfile = MAGIC; /*@unchecked@*/ char *progname; /* used throughout */ -/*@unchecked@*/ -int lineno; /* line number in the magic file */ /* * unwrap -- read a file of filenames, do each one. */ static void -unwrap(char *fn) +unwrap(fmagic fm, char *fn) /*@globals fileSystem, internalState @*/ - /*@modifies fileSystem, internalState @*/ + /*@modifies fm, fileSystem, internalState @*/ { char buf[MAXPATHLEN]; FILE *f; @@ -97,7 +98,7 @@ unwrap(char *fn) while (fgets(buf, MAXPATHLEN, f) != NULL) { buf[strlen(buf)-1] = '\0'; - process(buf, wid); + fmagicProcess(fm, buf, wid); if(nobuffer) (void) fflush(stdout); } @@ -156,16 +157,17 @@ help(void) */ int main(int argc, char **argv) - /*@globals fmagic_flags, nobuffer, - default_magicfile, lineno, magicfile, mlist, optind, progname, + /*@globals global_fmagic, nobuffer, + default_magicfile, optind, progname, fileSystem, internalState @*/ - /*@modifies argv, fmagic_flags, nobuffer, - default_magicfile, lineno, magicfile, mlist, optind, progname, + /*@modifies argv, global_fmagic, nobuffer, + default_magicfile, optind, progname, fileSystem, internalState @*/ { int c; int action = 0, didsomefiles = 0, errflg = 0, ret = 0, app = 0; char *mime, *home, *usermagic; + fmagic fm = global_fmagic; struct stat sb; #define OPTSTRING "bcdf:ikm:nsvzCL" #ifdef HAVE_GETOPT_H @@ -216,9 +218,11 @@ main(int argc, char **argv) progname = argv[0]; /*@=modobserver@*/ - magicfile = default_magicfile; +/*@-assignexpose@*/ + fm->magicfile = default_magicfile; +/*@=assignexpose@*/ if ((usermagic = getenv("MAGIC")) != NULL) - magicfile = usermagic; + fm->magicfile = usermagic; else { if ((home = getenv("HOME")) != NULL) { size_t nb = strlen(home) + 8; @@ -228,7 +232,7 @@ main(int argc, char **argv) if (stat(usermagic, &sb)<0) free(usermagic); else - magicfile = usermagic; + fm->magicfile = usermagic; } } @@ -247,7 +251,7 @@ main(int argc, char **argv) /*@switchbreak@*/ break; #endif case 'b': - fmagic_flags |= FMAGIC_FLAGS_BRIEF; + fm->flags |= FMAGIC_FLAGS_BRIEF; /*@switchbreak@*/ break; case 'c': action = CHECK; @@ -256,49 +260,51 @@ main(int argc, char **argv) action = COMPILE; /*@switchbreak@*/ break; case 'd': - fmagic_flags |= FMAGIC_FLAGS_DEBUG; + fm->flags |= FMAGIC_FLAGS_DEBUG; /*@switchbreak@*/ break; case 'f': if (!app) { - ret = apprentice(magicfile, action); + ret = fmagicSetup(fm, fm->magicfile, action); if (action) exit(ret); app = 1; } - unwrap(optarg); + unwrap(fm, optarg); ++didsomefiles; /*@switchbreak@*/ break; case 'i': - fmagic_flags |= FMAGIC_FLAGS_MIME; - mime = malloc(strlen(magicfile) + sizeof(".mime")); - (void)strcpy(mime, magicfile); + fm->flags |= FMAGIC_FLAGS_MIME; + mime = malloc(strlen(fm->magicfile) + sizeof(".mime")); + (void)strcpy(mime, fm->magicfile); (void)strcat(mime, ".mime"); - magicfile = mime; + fm->magicfile = mime; /*@switchbreak@*/ break; case 'k': - fmagic_flags |= FMAGIC_FLAGS_CONTINUE; + fm->flags |= FMAGIC_FLAGS_CONTINUE; /*@switchbreak@*/ break; case 'm': - magicfile = optarg; +/*@-assignexpose@*/ + fm->magicfile = optarg; +/*@=assignexpose@*/ /*@switchbreak@*/ break; case 'n': ++nobuffer; /*@switchbreak@*/ break; case 's': - fmagic_flags |= FMAGIC_FLAGS_SPECIAL; + fm->flags |= FMAGIC_FLAGS_SPECIAL; /*@switchbreak@*/ break; case 'v': (void) fprintf(stdout, "%s-%d.%d\n", progname, FILE_VERSION_MAJOR, patchlevel); (void) fprintf(stdout, "magic file from %s\n", - magicfile); + fm->magicfile); return 1; case 'z': - fmagic_flags |= FMAGIC_FLAGS_UNCOMPRESS; + fm->flags |= FMAGIC_FLAGS_UNCOMPRESS; /*@switchbreak@*/ break; #ifdef S_IFLNK case 'L': - fmagic_flags |= FMAGIC_FLAGS_FOLLOW; + fm->flags |= FMAGIC_FLAGS_FOLLOW; /*@switchbreak@*/ break; #endif case '?': @@ -312,7 +318,7 @@ main(int argc, char **argv) usage(); if (!app) { - ret = apprentice(magicfile, action); + ret = fmagicSetup(fm, fm->magicfile, action); if (action) exit(ret); app = 1; @@ -329,7 +335,7 @@ main(int argc, char **argv) wid = nw; } for (; optind < argc; optind++) - process(argv[optind], wid); + fmagicProcess(fm, argv[optind], wid); } #if HAVE_MCHECK_H && HAVE_MTRACE diff --git a/file/file.h b/file/file.h index 0a04ad175..ada91bcaa 100644 --- a/file/file.h +++ b/file/file.h @@ -116,16 +116,12 @@ struct mlist { /*@unchecked@*/ /*@observer@*/ /*@null@*/ extern char *progname; /* the program name */ -/*@unchecked@*/ /*@dependent@*/ /*@observer@*/ -extern const char *magicfile; /* name of the magic file */ /*@unchecked@*/ -extern int lineno; /* current line number in magic file */ -/*@unchecked@*/ -extern struct mlist mlist; /* list of arrays of magic entries */ - -enum fmagic_e { +enum fmagicFlags_e { +/*@-enummemuse@*/ FMAGIC_FLAGS_NONE = 0, +/*@=enummemuse@*/ FMAGIC_FLAGS_DEBUG = (1 << 0), FMAGIC_FLAGS_BRIEF = (1 << 1), /*!< brief output format */ FMAGIC_FLAGS_MIME = (1 << 2), /*!< output as mime-types */ @@ -135,49 +131,65 @@ enum fmagic_e { FMAGIC_FLAGS_UNCOMPRESS = (1 << 6) /*!< uncompress files? */ }; -extern int fmagic_flags; +struct fmagic_s { + int flags; /*!< bit(s) to control fmagic behavior. */ +/*@dependent@*/ /*@observer@*/ /*@null@*/ + const char *magicfile; /*!< name of the magic file */ + int lineno; /*!< current line number in magic file */ +/*@null@*/ + struct mlist * mlist; /*!< list of arrays of magic entries */ +}; + +typedef /*@abstract@*/ struct fmagic_s * fmagic; /*@mayexit@*/ -extern int apprentice(const char *fn, int action) - /*@globals lineno, mlist, fileSystem, internalState @*/ - /*@modifies lineno, mlist, fileSystem, internalState @*/; -extern int ascmagic(unsigned char *buf, int nbytes) +extern int fmagicSetup(fmagic fm, const char *fn, int action) + /*@globals fileSystem, internalState @*/ + /*@modifies fm, fileSystem, internalState @*/; +extern void fmagicProcess(fmagic fm, const char *inname, int wid) + /*@globals fileSystem, internalState @*/ + /*@modifies fm, fileSystem, internalState @*/; + +extern int fmagicA(fmagic fm, unsigned char *buf, int nbytes) /*@globals fileSystem @*/ /*@modifies fileSystem @*/; +struct stat; +extern int fmagicD(fmagic fm, const char *fn, /*@out@*/ struct stat *sb) + /*@globals fileSystem, internalState @*/ + /*@modifies fm, *sb, fileSystem, internalState @*/; +extern int fmagicS(fmagic fm, unsigned char *buf, int nbytes) + /*@globals fileSystem @*/ + /*@modifies fm, buf, fileSystem @*/; +extern int fmagicF(fmagic fm, const char *fn, unsigned char *buf, int nb, int zfl) + /*@globals fileSystem, internalState @*/ + /*@modifies fm, buf, fileSystem, internalState @*/; +extern int fmagicZ(fmagic fm, const char *fname, + unsigned char *buf, int nbytes) + /*@globals fileSystem, internalState @*/ + /*@modifies fm, buf, fileSystem, internalState @*/; + /*@exits@*/ -extern void error(const char *f, ...) +extern void error(const char *f, ...) /*@globals fileSystem @*/ /*@modifies fileSystem @*/; -extern void ckfputs(const char *str, FILE *fil) +extern void ckfputs(const char *str, FILE *fil) /*@globals fileSystem @*/ /*@modifies fil, fileSystem @*/; -struct stat; -extern int fsmagic(const char *fn, /*@out@*/ struct stat *sb) - /*@globals fileSystem, internalState @*/ - /*@modifies *sb, fileSystem, internalState @*/; /*@observer@*/ extern char *fmttime(long v, int local) /*@*/; extern int is_tar(unsigned char *buf, int nbytes) /*@*/; -extern void magwarn(const char *f, ...) +extern void magwarn(const char *f, ...) /*@globals fileSystem @*/ /*@modifies fileSystem @*/; -extern void mdump(struct magic *m) +extern void mdump(struct magic *m) /*@globals fileSystem @*/ /*@modifies fileSystem @*/; -extern void process(const char *inname, int wid) - /*@globals fileSystem, internalState @*/ - /*@modifies fileSystem, internalState @*/; -extern void showstr(FILE *fp, const char *s, int len) +extern void showstr(FILE *fp, const char *s, int len) /*@globals fileSystem @*/ /*@modifies fp, fileSystem @*/; -extern int softmagic(unsigned char *buf, int nbytes) - /*@globals fileSystem @*/ - /*@modifies buf, fileSystem @*/; -extern int tryit(const char *fn, unsigned char *buf, int nb, int zfl) - /*@globals fileSystem, internalState @*/ - /*@modifies buf, fileSystem, internalState @*/; + /** */ /*@unused@*/ /*@exits@*/ /*@only@*/ @@ -191,10 +203,8 @@ static inline void * vmefail(/*@unused@*/ size_t nb) return NULL; /*@=nullret@*/ } -extern int zmagic(const char *fname, unsigned char *buf, int nbytes) - /*@globals fileSystem, internalState @*/ - /*@modifies buf, fileSystem, internalState @*/; -extern void ckfprintf(FILE *f, const char *fmt, ...) + +extern void ckfprintf(FILE *f, const char *fmt, ...) /*@globals fileSystem @*/ /*@modifies f, fileSystem @*/; extern uint32_t signextend(struct magic *m, uint32_t v) diff --git a/file/fsmagic.c b/file/fsmagic.c index 60de1f129..e62481f50 100644 --- a/file/fsmagic.c +++ b/file/fsmagic.c @@ -31,10 +31,10 @@ FILE_RCSID("@(#)Id: fsmagic.c,v 1.36 2002/07/03 19:00:41 christos Exp ") -int fmagic_flags = 0; +/*@access fmagic @*/ int -fsmagic(const char *fn, struct stat *sb) +fmagicD(fmagic fm, const char *fn, struct stat *sb) { int ret = 0; @@ -43,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 (!(fmagic_flags & FMAGIC_FLAGS_FOLLOW)) + if (!(fm->flags & FMAGIC_FLAGS_FOLLOW)) ret = lstat(fn, sb); else #endif @@ -57,7 +57,7 @@ fsmagic(const char *fn, struct stat *sb) return 1; } - if ((fmagic_flags & FMAGIC_FLAGS_MIME)) { + if ((fm->flags & FMAGIC_FLAGS_MIME)) { if ((sb->st_mode & S_IFMT) != S_IFREG) { ckfputs("application/x-not-regular-file", stdout); return 1; @@ -86,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 ((fmagic_flags & FMAGIC_FLAGS_SPECIAL)) + if ((fm->flags & FMAGIC_FLAGS_SPECIAL)) break; #ifdef HAVE_STRUCT_STAT_ST_RDEV # ifdef dv_unit @@ -110,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 ((fmagic_flags & FMAGIC_FLAGS_SPECIAL)) + if ((fm->flags & FMAGIC_FLAGS_SPECIAL)) break; #ifdef HAVE_STRUCT_STAT_ST_RDEV # ifdef dv_unit @@ -184,8 +184,8 @@ fsmagic(const char *fn, struct stat *sb) /*@=branchstate@*/ /* Otherwise, handle it. */ - if ((fmagic_flags & FMAGIC_FLAGS_FOLLOW)) { - process(buf, strlen(buf)); + if ((fm->flags & FMAGIC_FLAGS_FOLLOW)) { + fmagicProcess(fm, buf, strlen(buf)); return 1; } else { /* just print what it points to */ ckfputs("symbolic link to ", stdout); @@ -220,15 +220,15 @@ 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 (!(fmagic_flags & FMAGIC_FLAGS_SPECIAL) && sb->st_size == 0) { - ckfputs((fmagic_flags & FMAGIC_FLAGS_MIME) ? "application/x-empty" : "empty", stdout); + if (!(fm->flags & FMAGIC_FLAGS_SPECIAL) && sb->st_size == 0) { + ckfputs((fm->flags & FMAGIC_FLAGS_MIME) ? "application/x-empty" : "empty", stdout); return 1; } return 0; } int -tryit(const char *fn, unsigned char *buf, int nb, int zfl) +fmagicF(fmagic fm, const char *fn, unsigned char *buf, int nb, int zfl) { /* @@ -245,27 +245,27 @@ tryit(const char *fn, unsigned char *buf, int nb, int zfl) return 'o'; #endif /* try compression stuff */ - if (zfl && zmagic(fn, buf, nb)) + if (zfl && fmagicZ(fm, fn, buf, nb)) return 'z'; /* try tests in /etc/magic (or surrogate magic file) */ - if (softmagic(buf, nb)) + if (fmagicS(fm, buf, nb)) return 's'; /* try known keywords, check whether it is ASCII */ - if (ascmagic(buf, nb)) + if (fmagicA(fm, buf, nb)) return 'a'; /* abandon hope, all ye who remain here */ - ckfputs((fmagic_flags & FMAGIC_FLAGS_MIME) ? "application/octet-stream" : "data", stdout); + ckfputs((fm->flags & FMAGIC_FLAGS_MIME) ? "application/octet-stream" : "data", stdout); return '\0'; } /* - * process - process input file + * fmagicProcess - process input file */ void -process(const char *inname, int wid) +fmagicProcess(fmagic fm, const char *inname, int wid) { int fd = 0; static const char stdname[] = "standard input"; @@ -283,7 +283,7 @@ process(const char *inname, int wid) inname = stdname; } - if (wid > 0 && !(fmagic_flags & FMAGIC_FLAGS_BRIEF)) + if (wid > 0 && !(fm->flags & FMAGIC_FLAGS_BRIEF)) (void) printf("%s:%*s ", inname, (int) (wid - strlen(inname)), ""); @@ -291,7 +291,7 @@ process(const char *inname, int wid) /* * first try judging the file based on its filesystem status */ - if (fsmagic(inname, &sb) != 0) { + if (fmagicD(fm, inname, &sb) != 0) { (void) putchar('\n'); return; } @@ -316,10 +316,10 @@ process(const char *inname, int wid) } if (nbytes == 0) - ckfputs((fmagic_flags & FMAGIC_FLAGS_MIME) ? "application/x-empty" : "empty", stdout); + ckfputs((fm->flags & FMAGIC_FLAGS_MIME) ? "application/x-empty" : "empty", stdout); else { buf[nbytes++] = '\0'; /* null-terminate it */ - match = tryit(inname, buf, nbytes, (fmagic_flags & FMAGIC_FLAGS_UNCOMPRESS)); + match = fmagicF(fm, inname, buf, nbytes, (fm->flags & FMAGIC_FLAGS_UNCOMPRESS)); } #ifdef BUILTIN_ELF diff --git a/file/print.c b/file/print.c index f78745a40..0b6aafd04 100644 --- a/file/print.c +++ b/file/print.c @@ -31,6 +31,15 @@ FILE_RCSID("@(#)Id: print.c,v 1.38 2002/07/03 18:37:44 christos Exp ") +/*@access fmagic @*/ + +/*@-compmempass@*/ +/*@unchecked@*/ +static struct fmagic_s myfmagic; +/*@unchecked@*/ +fmagic global_fmagic = &myfmagic; +/*@=compmempass@*/ + #define SZOF(a) (sizeof(a) / sizeof(a[0])) #ifndef COMPILE_ONLY @@ -168,6 +177,7 @@ error(const char *f, ...) void magwarn(const char *f, ...) { + fmagic fm = global_fmagic; va_list va; va_start(va, f); @@ -176,7 +186,7 @@ magwarn(const char *f, ...) if (progname != NULL) (void) fprintf(stderr, "%s: %s, %d: ", - progname, magicfile, lineno); + progname, fm->magicfile, fm->lineno); (void) vfprintf(stderr, f, va); va_end(va); (void) fputc('\n', stderr); diff --git a/file/softmagic.c b/file/softmagic.c index ad1d0b685..6616a0157 100644 --- a/file/softmagic.c +++ b/file/softmagic.c @@ -31,8 +31,10 @@ FILE_RCSID("@(#)Id: softmagic.c,v 1.51 2002/07/03 18:26:38 christos Exp ") +/*@access fmagic @*/ + static int32_t -sm_print(union VALUETYPE *p, struct magic *m) +fmagicSPrint(/*@unused@*/ const fmagic fm, union VALUETYPE *p, struct magic *m) /*@globals fileSystem @*/ /*@modifies p, fileSystem @*/ { @@ -99,7 +101,7 @@ sm_print(union VALUETYPE *p, struct magic *m) break; default: - error("invalid m->type (%d) in sm_print().\n", m->type); + error("invalid m->type (%d) in fmagicSPrint().\n", m->type); /*@notreached@*/ } return(t); @@ -111,7 +113,7 @@ sm_print(union VALUETYPE *p, struct magic *m) * (unless you have a better idea) */ static int -sm_convert(union VALUETYPE *p, struct magic *m) +fmagicSConvert(/*@unused@*/ const fmagic fm, union VALUETYPE *p, struct magic *m) /*@globals fileSystem @*/ /*@modifies p, fileSystem @*/ { @@ -373,7 +375,7 @@ sm_convert(union VALUETYPE *p, struct magic *m) case REGEX: return 1; default: - error("invalid type %d in sm_convert().\n", m->type); + error("invalid type %d in fmagicSConvert().\n", m->type); /*@notreached@*/ return 0; } @@ -381,18 +383,19 @@ sm_convert(union VALUETYPE *p, struct magic *m) static void -sm_debug(int32_t offset, char *str, int len) +fmagicSDebug(int32_t offset, char *str, int len) /*@globals fileSystem @*/ /*@modifies fileSystem @*/ { - (void) fprintf(stderr, "sm_get @%d: ", offset); + (void) fprintf(stderr, "fmagicSGet @%d: ", offset); showstr(stderr, (char *) str, len); (void) fputc('\n', stderr); (void) fputc('\n', stderr); } static int -sm_get(union VALUETYPE *p, unsigned char *s, struct magic *m, int nbytes) +fmagicSGet(const fmagic fm, union VALUETYPE *p, unsigned char *s, + struct magic *m, int nbytes) /*@globals fileSystem @*/ /*@modifies p, s, fileSystem @*/ { @@ -426,8 +429,8 @@ sm_get(union VALUETYPE *p, unsigned char *s, struct magic *m, int nbytes) } /*@=branchstate@*/ - if (fmagic_flags & FMAGIC_FLAGS_DEBUG) { - sm_debug(offset, (char *) p, sizeof(union VALUETYPE)); + if (fm->flags & FMAGIC_FLAGS_DEBUG) { + fmagicSDebug(offset, (char *) p, sizeof(union VALUETYPE)); mdump(m); } @@ -760,18 +763,18 @@ sm_get(union VALUETYPE *p, unsigned char *s, struct magic *m, int nbytes) memcpy(p, s + offset, sizeof(union VALUETYPE)); - if (fmagic_flags & FMAGIC_FLAGS_DEBUG) { - sm_debug(offset, (char *) p, sizeof(union VALUETYPE)); + if (fm->flags & FMAGIC_FLAGS_DEBUG) { + fmagicSDebug(offset, (char *) p, sizeof(union VALUETYPE)); mdump(m); } } - if (!sm_convert(p, m)) + if (!fmagicSConvert(fm, p, m)) return 0; return 1; } static int -sm_check(union VALUETYPE *p, struct magic *m) +fmagicSCheck(const fmagic fm, union VALUETYPE *p, struct magic *m) /*@globals fileSystem @*/ /*@modifies fileSystem @*/ { @@ -872,7 +875,7 @@ sm_check(union VALUETYPE *p, struct magic *m) } /*@notreached@*/ break; default: - error("invalid type %d in sm_check().\n", m->type); + error("invalid type %d in fmagicSCheck().\n", m->type); /*@notreached@*/ return 0; } @@ -882,21 +885,21 @@ sm_check(union VALUETYPE *p, struct magic *m) switch (m->reln) { case 'x': - if (fmagic_flags & FMAGIC_FLAGS_DEBUG) + if (fm->flags & FMAGIC_FLAGS_DEBUG) (void) fprintf(stderr, "%u == *any* = 1\n", v); matched = 1; break; case '!': matched = v != l; - if (fmagic_flags & FMAGIC_FLAGS_DEBUG) + if (fm->flags & FMAGIC_FLAGS_DEBUG) (void) fprintf(stderr, "%u != %u = %d\n", v, l, matched); break; case '=': matched = v == l; - if (fmagic_flags & FMAGIC_FLAGS_DEBUG) + if (fm->flags & FMAGIC_FLAGS_DEBUG) (void) fprintf(stderr, "%u == %u = %d\n", v, l, matched); break; @@ -904,13 +907,13 @@ sm_check(union VALUETYPE *p, struct magic *m) case '>': if (m->flag & UNSIGNED) { matched = v > l; - if (fmagic_flags & FMAGIC_FLAGS_DEBUG) + if (fm->flags & FMAGIC_FLAGS_DEBUG) (void) fprintf(stderr, "%u > %u = %d\n", v, l, matched); } else { matched = (int32_t) v > (int32_t) l; - if (fmagic_flags & FMAGIC_FLAGS_DEBUG) + if (fm->flags & FMAGIC_FLAGS_DEBUG) (void) fprintf(stderr, "%d > %d = %d\n", v, l, matched); } @@ -919,13 +922,13 @@ sm_check(union VALUETYPE *p, struct magic *m) case '<': if (m->flag & UNSIGNED) { matched = v < l; - if (fmagic_flags & FMAGIC_FLAGS_DEBUG) + if (fm->flags & FMAGIC_FLAGS_DEBUG) (void) fprintf(stderr, "%u < %u = %d\n", v, l, matched); } else { matched = (int32_t) v < (int32_t) l; - if (fmagic_flags & FMAGIC_FLAGS_DEBUG) + if (fm->flags & FMAGIC_FLAGS_DEBUG) (void) fprintf(stderr, "%d < %d = %d\n", v, l, matched); } @@ -933,21 +936,21 @@ sm_check(union VALUETYPE *p, struct magic *m) case '&': matched = (v & l) == l; - if (fmagic_flags & FMAGIC_FLAGS_DEBUG) + if (fm->flags & FMAGIC_FLAGS_DEBUG) (void) fprintf(stderr, "((%x & %x) == %x) = %d\n", v, l, l, matched); break; case '^': matched = (v & l) != l; - if (fmagic_flags & FMAGIC_FLAGS_DEBUG) + if (fm->flags & FMAGIC_FLAGS_DEBUG) (void) fprintf(stderr, "((%x & %x) != %x) = %d\n", v, l, l, matched); break; default: matched = 0; - error("sm_check: can't happen: invalid relation %d.\n", m->reln); + error("fmagicSCheck: can't happen: invalid relation %d.\n", m->reln); /*@notreached@*/ break; } @@ -982,7 +985,8 @@ sm_check(union VALUETYPE *p, struct magic *m) * so that higher-level continuations are processed. */ static int -sm_match(struct magic *m, uint32_t nmagic, unsigned char *s, int nbytes) +fmagicSMatch(const fmagic fm, struct magic *m, uint32_t nmagic, + unsigned char *s, int nbytes) /*@globals fileSystem @*/ /*@modifies m, s, fileSystem @*/ { @@ -999,8 +1003,8 @@ sm_match(struct magic *m, uint32_t nmagic, unsigned char *s, int nbytes) for (magindex = 0; magindex < nmagic; magindex++) { /* if main entry matches, print it... */ - if (!sm_get(&p, s, &m[magindex], nbytes) || - !sm_check(&p, &m[magindex])) { + if (!fmagicSGet(fm, &p, s, &m[magindex], nbytes) || + !fmagicSCheck(fm, &p, &m[magindex])) { /* * main entry didn't match, * flush its continuations @@ -1018,7 +1022,7 @@ sm_match(struct magic *m, uint32_t nmagic, unsigned char *s, int nbytes) if ((cont_level+1) >= tmplen) tmpoff = (int32_t *) xrealloc(tmpoff, tmplen += 20); - tmpoff[cont_level] = sm_print(&p, &m[magindex]); + tmpoff[cont_level] = fmagicSPrint(fm, &p, &m[magindex]); cont_level++; /* @@ -1043,8 +1047,8 @@ sm_match(struct magic *m, uint32_t nmagic, unsigned char *s, int nbytes) oldoff = m[magindex].offset; m[magindex].offset += tmpoff[cont_level-1]; } - if (sm_get(&p, s, &m[magindex], nbytes) && - sm_check(&p, &m[magindex])) + if (fmagicSGet(fm, &p, s, &m[magindex], nbytes) && + fmagicSCheck(fm, &p, &m[magindex])) { /* * This continuation matched. @@ -1063,7 +1067,7 @@ sm_match(struct magic *m, uint32_t nmagic, unsigned char *s, int nbytes) } if ((cont_level+1) >= tmplen) tmpoff = xrealloc(tmpoff, tmplen += 20); - tmpoff[cont_level] = sm_print(&p, &m[magindex]); + tmpoff[cont_level] = fmagicSPrint(fm, &p, &m[magindex]); cont_level++; if (m[magindex].desc[0]) need_separator = 1; @@ -1073,24 +1077,25 @@ sm_match(struct magic *m, uint32_t nmagic, unsigned char *s, int nbytes) } firstline = 0; returnval = 1; - if (!(fmagic_flags & FMAGIC_FLAGS_CONTINUE)) /* don't keep searching */ + if (!(fm->flags & FMAGIC_FLAGS_CONTINUE)) /* don't keep searching */ return 1; } return returnval; /* This is hit if -k is set or there is no match */ } /* - * softmagic - lookup one file in database + * fmagicS - lookup one file in database * (already read from MAGIC by apprentice.c). * Passed the name and FILE * of one file to be typed. */ int -softmagic(unsigned char *buf, int nbytes) +fmagicS(fmagic fm, unsigned char *buf, int nbytes) { struct mlist *ml; - for (ml = mlist.next; ml != &mlist; ml = ml->next) { - if (sm_match(ml->magic, ml->nmagic, buf, nbytes)) + if (fm->mlist != NULL) + for (ml = fm->mlist->next; ml != fm->mlist; ml = ml->next) { + if (fmagicSMatch(fm, ml->magic, ml->nmagic, buf, nbytes)) return 1; } -- 2.34.1