#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"
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},
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, "");
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
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",
* 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,
#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"
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
#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)
{