2 * Copyright (c) Christos Zoulas 2003.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice immediately at the beginning of the file, without modification,
10 * this list of conditions, and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 FILE_RCSID("@(#)$File: magic.c,v 1.74 2011/05/26 01:27:59 christos Exp $")
48 #include <limits.h> /* for PIPE_BUF */
51 #if defined(HAVE_UTIMES)
52 # include <sys/time.h>
53 #elif defined(HAVE_UTIME)
54 # if defined(HAVE_SYS_UTIME_H)
55 # include <sys/utime.h>
56 # elif defined(HAVE_UTIME_H)
62 #include <unistd.h> /* for read() */
66 /* Get the PIPE_BUF from pathconf */
68 #define PIPE_BUF pathconf(".", _PC_PIPE_BUF)
74 private void free_mlist(struct mlist *);
76 private void close_and_restore(const struct magic_set *, const char *, int,
78 private int unreadable_info(struct magic_set *, mode_t, const char *);
79 private const char* get_default_magic(void);
80 private const char *file_or_fd(struct magic_set *, const char *, int);
84 #define STDIN_FILENO 0
88 get_default_magic(void)
90 static const char hmagic[] = "/.magic/magic.mgc";
91 static char *default_magic;
92 char *home, *hmagicpath;
101 if ((home = getenv("HOME")) == NULL)
104 if (asprintf(&hmagicpath, "%s/.magic", home) < 0)
106 if (stat(hmagicpath, &st) == -1)
108 if (S_ISDIR(st.st_mode)) {
110 if (asprintf(&hmagicpath, "%s/%s", home, hmagic) < 0)
112 if (access(hmagicpath, R_OK) == -1)
116 if (asprintf(&default_magic, "%s:%s", hmagicpath, MAGIC) < 0)
119 return default_magic;
121 default_magic = NULL;
125 char *hmagicp = hmagicpath;
126 char *tmppath = NULL;
128 #define APPENDPATH() \
130 if (tmppath && access(tmppath, R_OK) != -1) { \
131 if (hmagicpath == NULL) \
132 hmagicpath = tmppath; \
134 if (asprintf(&hmagicp, "%s%c%s", hmagicpath, \
135 PATHSEP, tmppath) >= 0) { \
137 hmagicpath = hmagicp; \
143 } while (/*CONSTCOND*/0)
147 default_magic = NULL;
150 /* First, try to get user-specific magic file */
151 if ((home = getenv("LOCALAPPDATA")) == NULL) {
152 if ((home = getenv("USERPROFILE")) != NULL)
153 if (asprintf(&tmppath,
154 "%s/Local Settings/Application Data%s", home,
158 if (asprintf(&tmppath, "%s%s", home, hmagic) < 0)
164 /* Second, try to get a magic file from Common Files */
165 if ((home = getenv("COMMONPROGRAMFILES")) != NULL) {
166 if (asprintf(&tmppath, "%s%s", home, hmagic) >= 0)
170 /* Third, try to get magic file relative to dll location */
171 LPTSTR dllpath = malloc(sizeof(*dllpath) * (MAX_PATH + 1));
172 dllpath[MAX_PATH] = 0; /* just in case long path gets truncated and not null terminated */
173 if (GetModuleFileNameA(NULL, dllpath, MAX_PATH)){
174 PathRemoveFileSpecA(dllpath);
175 if (strlen(dllpath) > 3 &&
176 stricmp(&dllpath[strlen(dllpath) - 3], "bin") == 0) {
177 if (asprintf(&tmppath,
178 "%s/../share/misc/magic.mgc", dllpath) >= 0)
181 if (asprintf(&tmppath,
182 "%s/share/misc/magic.mgc", dllpath) >= 0)
184 else if (asprintf(&tmppath,
185 "%s/magic.mgc", dllpath) >= 0)
190 /* Don't put MAGIC constant - it likely points to a file within MSys
192 default_magic = hmagicpath;
193 return default_magic;
198 magic_getpath(const char *magicfile, int action)
200 if (magicfile != NULL)
203 magicfile = getenv("MAGIC");
204 if (magicfile != NULL)
207 return action == FILE_LOAD ? get_default_magic() : MAGIC;
210 public struct magic_set *
211 magic_open(int flags)
213 struct magic_set *ms;
216 if ((ms = CAST(struct magic_set *, calloc((size_t)1,
217 sizeof(struct magic_set)))) == NULL)
220 if (magic_setflags(ms, flags) == -1) {
225 ms->o.buf = ms->o.pbuf = NULL;
226 len = (ms->c.len = 10) * sizeof(*ms->c.li);
228 if ((ms->c.li = CAST(struct level_info *, malloc(len))) == NULL)
234 ms->file = "unknown";
243 free_mlist(struct mlist *mlist)
250 for (ml = mlist->next; ml != mlist;) {
251 struct mlist *next = ml->next;
252 struct magic *mg = ml->magic;
253 file_delmagic(mg, ml->mapped, ml->nmagic);
262 unreadable_info(struct magic_set *ms, mode_t md, const char *file)
264 /* We cannot open it, but we were able to stat it. */
265 if (access(file, W_OK) == 0)
266 if (file_printf(ms, "writable, ") == -1)
268 if (access(file, X_OK) == 0)
269 if (file_printf(ms, "executable, ") == -1)
272 if (file_printf(ms, "regular file, ") == -1)
274 if (file_printf(ms, "no read permission") == -1)
281 magic_close(struct magic_set *ms)
283 free_mlist(ms->mlist);
294 magic_load(struct magic_set *ms, const char *magicfile)
296 struct mlist *ml = file_apprentice(ms, magicfile, FILE_LOAD);
298 free_mlist(ms->mlist);
306 magic_compile(struct magic_set *ms, const char *magicfile)
308 struct mlist *ml = file_apprentice(ms, magicfile, FILE_COMPILE);
314 magic_check(struct magic_set *ms, const char *magicfile)
316 struct mlist *ml = file_apprentice(ms, magicfile, FILE_CHECK);
322 magic_list(struct magic_set *ms, const char *magicfile)
324 struct mlist *ml = file_apprentice(ms, magicfile, FILE_LIST);
331 close_and_restore(const struct magic_set *ms, const char *name, int fd,
332 const struct stat *sb)
334 if (fd == STDIN_FILENO)
338 if ((ms->flags & MAGIC_PRESERVE_ATIME) != 0) {
340 * Try to restore access, modification times if read it.
341 * This is really *bad* because it will modify the status
342 * time of the file... And of course this will affect
346 struct timeval utsbuf[2];
347 (void)memset(utsbuf, 0, sizeof(utsbuf));
348 utsbuf[0].tv_sec = sb->st_atime;
349 utsbuf[1].tv_sec = sb->st_mtime;
351 (void) utimes(name, utsbuf); /* don't care if loses */
352 #elif defined(HAVE_UTIME_H) || defined(HAVE_SYS_UTIME_H)
353 struct utimbuf utbuf;
355 (void)memset(&utbuf, 0, sizeof(utbuf));
356 utbuf.actime = sb->st_atime;
357 utbuf.modtime = sb->st_mtime;
358 (void) utime(name, &utbuf); /* don't care if loses */
365 * find type of descriptor
368 magic_descriptor(struct magic_set *ms, int fd)
370 return file_or_fd(ms, NULL, fd);
374 * find type of named file
377 magic_file(struct magic_set *ms, const char *inname)
379 return file_or_fd(ms, inname, STDIN_FILENO);
383 file_or_fd(struct magic_set *ms, const char *inname, int fd)
388 ssize_t nbytes = 0; /* number of bytes read from a datafile */
392 * one extra for terminating '\0', and
393 * some overlapping space for matches near EOF
395 #define SLOP (1 + sizeof(union VALUETYPE))
396 if ((buf = CAST(unsigned char *, malloc(HOWMANY + SLOP))) == NULL)
399 if (file_reset(ms) == -1)
402 switch (file_fsmagic(ms, inname, &sb)) {
405 case 0: /* nothing found */
407 default: /* matched it and printed type */
412 if (inname == NULL) {
413 if (fstat(fd, &sb) == 0 && S_ISFIFO(sb.st_mode))
416 int flags = O_RDONLY|O_BINARY;
418 if (stat(inname, &sb) == 0 && S_ISFIFO(sb.st_mode)) {
426 if ((fd = open(inname, flags)) < 0) {
427 if (unreadable_info(ms, sb.st_mode, inname) == -1)
433 if ((flags = fcntl(fd, F_GETFL)) != -1) {
434 flags &= ~O_NONBLOCK;
435 (void)fcntl(fd, F_SETFL, flags);
441 * try looking at the first HOWMANY bytes
446 while ((r = sread(fd, (void *)&buf[nbytes],
447 (size_t)(HOWMANY - nbytes), 1)) > 0) {
449 if (r < PIPE_BUF) break;
453 /* We can not read it, but we were able to stat it. */
454 if (unreadable_info(ms, sb.st_mode, inname) == -1)
461 if ((nbytes = read(fd, (char *)buf, HOWMANY)) == -1) {
462 file_error(ms, errno, "cannot read `%s'", inname);
467 (void)memset(buf + nbytes, 0, SLOP); /* NUL terminate */
468 if (file_buffer(ms, fd, inname, buf, (size_t)nbytes) == -1)
473 close_and_restore(ms, inname, fd, &sb);
474 return rv == 0 ? file_getbuffer(ms) : NULL;
479 magic_buffer(struct magic_set *ms, const void *buf, size_t nb)
481 if (file_reset(ms) == -1)
484 * The main work is done here!
485 * We have the file name and/or the data buffer to be identified.
487 if (file_buffer(ms, -1, NULL, buf, nb) == -1) {
490 return file_getbuffer(ms);
492 #endif /* COMPILE_ONLY */
495 magic_error(struct magic_set *ms)
497 return (ms->event_flags & EVENT_HAD_ERR) ? ms->o.buf : NULL;
501 magic_errno(struct magic_set *ms)
503 return (ms->event_flags & EVENT_HAD_ERR) ? ms->error : 0;
507 magic_setflags(struct magic_set *ms, int flags)
509 #if !defined(HAVE_UTIME) && !defined(HAVE_UTIMES)
510 if (flags & MAGIC_PRESERVE_ATIME)