]> granicus.if.org Git - file/commitdiff
magic file handling re-factoring.
authorChristos Zoulas <christos@zoulas.com>
Mon, 14 Sep 2009 17:50:38 +0000 (17:50 +0000)
committerChristos Zoulas <christos@zoulas.com>
Mon, 14 Sep 2009 17:50:38 +0000 (17:50 +0000)
ChangeLog
src/apprentice.c
src/file.c
src/magic.c
src/magic.h

index 100cf72f0af327f74d99ae7760381a3d21327ea5..c0d5b8073eed5277095fd491168aebf9a3accd79 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2009-09-14  13:49  Christos Zoulas <christos@zoulas.com<
+
+       * Centralize magic path handling routines and remove the
+         special-casing from file.c so that the python module for
+         example comes up with the same magic path (Fixes ~/.magic
+         handling) (from Gab)
+
 2009-09-11  23:38  Reuben Thomas <rrt@sc3d.org>
 
        * When magic argument is a directory, read the files in
index fd8a243d0041f3bc4fd6634417d1d1bdc43f145c..5bcd730eea7ad7d1cb228063ae6597c1a8d83304 100644 (file)
@@ -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));
index 0f2360398ccc25b5c7de56342f87b75cf0746693..672bffaaed8df7d47888b37765115154cb2ab9b0 100644 (file)
@@ -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,
index fef8200ddca4b11888bb13030baa464d82cea088..9b8e48c1237f6905386c3720788b001a185fc8ac 100644 (file)
@@ -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)
 {
index a664e9aa39ed5f93e20f07d451936cc5d748de93..765ff2be3532223e0ffe564a5c6b597061602ca3 100644 (file)
@@ -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);