From: Christos Zoulas Date: Mon, 14 Sep 2009 17:50:38 +0000 (+0000) Subject: magic file handling re-factoring. X-Git-Tag: FILE5_04~35 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5d6b8d40a9e30b341c9b625f9c7839ad2fd7e057;p=file magic file handling re-factoring. --- diff --git a/ChangeLog b/ChangeLog index 100cf72f..c0d5b807 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2009-09-14 13:49 Christos Zoulas * When magic argument is a directory, read the files in diff --git a/src/apprentice.c b/src/apprentice.c index fd8a243d..5bcd730e 100644 --- a/src/apprentice.c +++ b/src/apprentice.c @@ -32,7 +32,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: apprentice.c,v 1.155 2009/09/11 22:40:50 rrt Exp $") +FILE_RCSID("@(#)$File: apprentice.c,v 1.156 2009/09/11 23:39:25 christos Exp $") #endif /* lint */ #include "magic.h" @@ -353,12 +353,10 @@ file_apprentice(struct magic_set *ms, const char *fn, int action) int file_err, errs = -1; struct mlist *mlist; - init_file_tables(); + if ((fn = magic_getpath(fn, action)) == NULL) + return NULL; - if (fn == NULL) - fn = getenv("MAGIC"); - if (fn == NULL) - fn = MAGIC; + init_file_tables(); if ((mfn = strdup(fn)) == NULL) { file_oomem(ms, strlen(fn)); diff --git a/src/file.c b/src/file.c index 0f236039..672bffaa 100644 --- a/src/file.c +++ b/src/file.c @@ -32,7 +32,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: file.c,v 1.131 2009/02/13 18:48:05 christos Exp $") +FILE_RCSID("@(#)$File: file.c,v 1.132 2009/07/15 14:08:23 christos Exp $") #endif /* lint */ #include "magic.h" @@ -90,9 +90,7 @@ private int /* Global command-line options */ nobuffer = 0, /* Do not buffer stdout */ nulsep = 0; /* Append '\0' to the separator */ -private const char *default_magicfile = MAGIC; private const char *separator = ":"; /* Default field separator */ -private const char hmagic[] = "/.magic"; private const struct option long_options[] = { #define OPT(shortname, longname, opt, doc) \ {longname, opt, NULL, shortname}, @@ -141,11 +139,9 @@ main(int argc, char *argv[]) size_t i; int action = 0, didsomefiles = 0, errflg = 0; int flags = 0, e = 0; - char *home, *usermagic; struct magic_set *magic = NULL; - char magicpath[2 * MAXPATHLEN + 2]; int longindex; - const char *magicfile; /* where the magic is */ + const char *magicfile = NULL; /* where the magic is */ /* makes islower etc work for other langs */ (void)setlocale(LC_CTYPE, ""); @@ -160,20 +156,6 @@ main(int argc, char *argv[]) else progname = argv[0]; - magicfile = default_magicfile; - if ((usermagic = getenv("MAGIC")) != NULL) - magicfile = usermagic; - else - if ((home = getenv("HOME")) != NULL) { - (void)snprintf(magicpath, sizeof(magicpath), "%s%s", - home, hmagic); - if (access(magicpath, R_OK) == 0) { - (void)snprintf(magicpath, sizeof(magicpath), - "%s%s:%s", home, hmagic, magicfile); - magicfile = magicpath; - } - } - #ifdef S_IFLNK flags |= getenv("POSIXLY_CORRECT") ? MAGIC_SYMLINK : 0; #endif @@ -261,6 +243,8 @@ main(int argc, char *argv[]) flags |= MAGIC_DEVICES; break; case 'v': + if (magicfile == NULL) + magicfile = magic_getpath(magicfile, action); (void)fprintf(stderr, "%s-%d.%.2d\n", progname, FILE_VERSION_MAJOR, patchlevel); (void)fprintf(stderr, "magic file from %s\n", @@ -296,8 +280,6 @@ main(int argc, char *argv[]) * Don't try to check/compile ~/.magic unless we explicitly * ask for it. */ - if (magicfile == magicpath) - magicfile = default_magicfile; magic = magic_open(flags|MAGIC_CHECK); if (magic == NULL) { (void)fprintf(stderr, "%s: %s\n", progname, diff --git a/src/magic.c b/src/magic.c index fef8200d..9b8e48c1 100644 --- a/src/magic.c +++ b/src/magic.c @@ -28,7 +28,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: magic.c,v 1.63 2009/05/08 17:41:59 christos Exp $") +FILE_RCSID("@(#)$File: magic.c,v 1.64 2009/07/15 14:08:43 christos Exp $") #endif /* lint */ #include "magic.h" @@ -72,6 +72,7 @@ private void free_mlist(struct mlist *); private void close_and_restore(const struct magic_set *, const char *, int, const struct stat *); private int unreadable_info(struct magic_set *, mode_t, const char *); +private const char* get_default_magic(void); #ifndef COMPILE_ONLY private const char *file_or_fd(struct magic_set *, const char *, int); #endif @@ -80,6 +81,41 @@ private const char *file_or_fd(struct magic_set *, const char *, int); #define STDIN_FILENO 0 #endif +private const char * +get_default_magic(void) +{ + static const char hmagic[] = "/.magic"; + static char default_magic[2 * MAXPATHLEN + 2]; + char *home; + char hmagicpath[MAXPATHLEN + 1]; + + if ((home = getenv("HOME")) == NULL) + return MAGIC; + + (void)snprintf(hmagicpath, sizeof(hmagicpath), "%s%s", home, hmagic); + + if (access(hmagicpath, R_OK) == -1) + return MAGIC; + + (void)snprintf(default_magic, sizeof(default_magic), "%s:%s", + hmagicpath, MAGIC); + + return default_magic; +} + +public const char * +magic_getpath(const char *magicfile, int action) +{ + if (magicfile != NULL) + return magicfile; + + magicfile = getenv("MAGIC"); + if (magicfile != NULL) + return magicfile; + + return action == FILE_LOAD ? get_default_magic() : MAGIC; +} + public struct magic_set * magic_open(int flags) { diff --git a/src/magic.h b/src/magic.h index a664e9aa..765ff2be 100644 --- a/src/magic.h +++ b/src/magic.h @@ -69,6 +69,7 @@ typedef struct magic_set *magic_t; magic_t magic_open(int); void magic_close(magic_t); +const char *magic_getpath(const char *, int); const char *magic_file(magic_t, const char *); const char *magic_descriptor(magic_t, int); const char *magic_buffer(magic_t, const void *, size_t);