]> granicus.if.org Git - file/commitdiff
fix reading of ~/.magic
authorChristos Zoulas <christos@zoulas.com>
Wed, 1 Oct 2008 14:30:45 +0000 (14:30 +0000)
committerChristos Zoulas <christos@zoulas.com>
Wed, 1 Oct 2008 14:30:45 +0000 (14:30 +0000)
ChangeLog
src/file.c

index 2c62a725d1b6f46a8a016161dfe7e5a984717c79..b2de6267681c796c8fa85bb6c9eaeace462ae9ff 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2008-10-01 10:30  Christos Zoulas <christos@astron.com>
+
+       * Read ~/.magic in addition to the default magic file not instead
+         of, as documented in the man page.
+
 2008-08-30 12:54  Christos Zoulas <christos@astron.com>
 
        * Don't eat trailing \n in magic enties.
index 2f518da99e571b0dac0240e12a81859f259bc44f..cebdaafa6d40780b4d4767f956739e1d1934bd9a 100644 (file)
@@ -74,7 +74,7 @@ int getopt_long(int argc, char * const *argv, const char *optstring, const struc
 #include "patchlevel.h"
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: file.c,v 1.121 2008/07/03 15:48:18 christos Exp $")
+FILE_RCSID("@(#)$File: file.c,v 1.122 2008/10/01 14:30:45 christos Exp $")
 #endif /* lint */
 
 
@@ -87,7 +87,7 @@ FILE_RCSID("@(#)$File: file.c,v 1.121 2008/07/03 15:48:18 christos Exp $")
 # define USAGE  "Usage: %s [-bcik" SYMLINKFLAG "nNrsvz0] [-e test] [-f namefile] [-F separator] [-m magicfiles] file...\n       %s -C -m magicfiles\n"
 
 #ifndef MAXPATHLEN
-#define        MAXPATHLEN      512
+#define        MAXPATHLEN      1024
 #endif
 
 private int            /* Global command-line options          */
@@ -96,7 +96,7 @@ private int           /* Global command-line options          */
        nobuffer = 0,   /* Do not buffer stdout                 */
        nulsep = 0;     /* Append '\0' to the separator         */
 
-private const char *magicfile = 0;     /* where the magic is   */
+private const char *magicfile;         /* where the magic is   */
 private const char *default_magicfile = MAGIC;
 private const char *separator = ":";   /* Default field separator      */
 
@@ -124,8 +124,8 @@ main(int argc, char *argv[])
        int action = 0, didsomefiles = 0, errflg = 0;
        int flags = 0;
        char *home, *usermagic;
-       struct stat sb;
        static const char hmagic[] = "/.magic";
+       char magicpath[2 * MAXPATHLEN + 2];
 #define OPTSTRING      "bcCde:f:F:hikLm:nNprsvz0"
        int longindex;
        static const struct option long_options[] =
@@ -171,14 +171,12 @@ main(int argc, char *argv[])
                magicfile = usermagic;
        else
                if ((home = getenv("HOME")) != NULL) {
-                       if ((usermagic = malloc(strlen(home)
-                           + sizeof(hmagic))) != NULL) {
-                               (void)strcpy(usermagic, home);
-                               (void)strcat(usermagic, hmagic);
-                               if (stat(usermagic, &sb)<0) 
-                                       free(usermagic);
-                               else
-                                       magicfile = usermagic;
+                       (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;
                        }
                }
 
@@ -293,6 +291,12 @@ main(int argc, char *argv[])
        switch(action) {
        case FILE_CHECK:
        case FILE_COMPILE:
+               /*
+                * 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,