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.73 2011/05/10 17:08:14 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 *);
75 private void close_and_restore(const struct magic_set *, const char *, int,
77 private int unreadable_info(struct magic_set *, mode_t, const char *);
78 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 BOOL WINAPI DllMain(HINSTANCE hinstDLL,
89 DWORD fdwReason __attribute__((__unused__)),
90 LPVOID lpvReserved __attribute__((__unused__)));
92 CHAR dllpath[MAX_PATH + 1] = { 0 };
94 BOOL WINAPI DllMain(HINSTANCE hinstDLL,
95 DWORD fdwReason __attribute__((__unused__)),
96 LPVOID lpvReserved __attribute__((__unused__)))
98 if (dllpath[0] == 0 &&
99 GetModuleFileNameA(hinstDLL, dllpath, MAX_PATH) != 0)
100 PathRemoveFileSpecA(dllpath);
106 get_default_magic(void)
108 static const char hmagic[] = "/.magic/magic.mgc";
109 static char *default_magic;
110 char *home, *hmagicpath;
117 default_magic = NULL;
119 if ((home = getenv("HOME")) == NULL)
122 if (asprintf(&hmagicpath, "%s/.magic", home) < 0)
124 if (stat(hmagicpath, &st) == -1)
126 if (S_ISDIR(st.st_mode)) {
128 if (asprintf(&hmagicpath, "%s/%s", home, hmagic) < 0)
130 if (access(hmagicpath, R_OK) == -1)
134 if (asprintf(&default_magic, "%s:%s", hmagicpath, MAGIC) < 0)
137 return default_magic;
139 default_magic = NULL;
143 char *hmagicp = hmagicpath;
144 char *tmppath = NULL;
146 #define APPENDPATH() \
148 if (tmppath && access(tmppath, R_OK) != -1) { \
149 if (hmagicpath == NULL) { \
150 hmagicpath = tmppath; \
154 if (asprintf(&hmagicp, "%s%c%s", hmagicpath, \
155 PATHSEP, tmppath) >= 0) { \
157 hmagicpath = hmagicp; \
160 } while (/*CONSTCOND*/0)
164 default_magic = NULL;
167 /* First, try to get user-specific magic file */
168 if ((home = getenv("LOCALAPPDATA")) == NULL) {
169 if ((home = getenv("USERPROFILE")) != NULL)
170 if (asprintf(&tmppath,
171 "%s/Local Settings/Application Data%s", home,
175 if (asprintf(&tmppath, "%s%s", home, hmagic) < 0)
181 /* Second, try to get a magic file from Common Files */
182 if ((home = getenv("COMMONPROGRAMFILES")) != NULL) {
183 if (asprintf(&tmppath, "%s%s", home, hmagic) >= 0)
188 /* Third, try to get magic file relative to dll location */
189 if (dllpath[0] != 0) {
190 if (strlen(dllpath) > 3 &&
191 stricmp(&dllpath[strlen(dllpath) - 3], "bin") == 0) {
192 if (asprintf(&tmppath,
193 "%s/../share/misc/magic.mgc", dllpath) >= 0)
196 if (asprintf(&tmppath,
197 "%s/share/misc/magic.mgc", dllpath) >= 0)
199 else if (asprintf(&tmppath,
200 "%s/magic.mgc", dllpath) >= 0)
205 /* Don't put MAGIC constant - it likely points to a file within MSys
207 default_magic = hmagicpath;
208 return default_magic;
213 magic_getpath(const char *magicfile, int action)
215 if (magicfile != NULL)
218 magicfile = getenv("MAGIC");
219 if (magicfile != NULL)
222 return action == FILE_LOAD ? get_default_magic() : MAGIC;
225 public struct magic_set *
226 magic_open(int flags)
228 struct magic_set *ms;
231 if ((ms = CAST(struct magic_set *, calloc((size_t)1,
232 sizeof(struct magic_set)))) == NULL)
235 if (magic_setflags(ms, flags) == -1) {
240 ms->o.buf = ms->o.pbuf = NULL;
241 len = (ms->c.len = 10) * sizeof(*ms->c.li);
243 if ((ms->c.li = CAST(struct level_info *, malloc(len))) == NULL)
249 ms->file = "unknown";
258 free_mlist(struct mlist *mlist)
265 for (ml = mlist->next; ml != mlist;) {
266 struct mlist *next = ml->next;
267 struct magic *mg = ml->magic;
268 file_delmagic(mg, ml->mapped, ml->nmagic);
276 unreadable_info(struct magic_set *ms, mode_t md, const char *file)
278 /* We cannot open it, but we were able to stat it. */
279 if (access(file, W_OK) == 0)
280 if (file_printf(ms, "writable, ") == -1)
282 if (access(file, X_OK) == 0)
283 if (file_printf(ms, "executable, ") == -1)
286 if (file_printf(ms, "regular file, ") == -1)
288 if (file_printf(ms, "no read permission") == -1)
294 magic_close(struct magic_set *ms)
296 free_mlist(ms->mlist);
307 magic_load(struct magic_set *ms, const char *magicfile)
309 struct mlist *ml = file_apprentice(ms, magicfile, FILE_LOAD);
311 free_mlist(ms->mlist);
319 magic_compile(struct magic_set *ms, const char *magicfile)
321 struct mlist *ml = file_apprentice(ms, magicfile, FILE_COMPILE);
327 magic_check(struct magic_set *ms, const char *magicfile)
329 struct mlist *ml = file_apprentice(ms, magicfile, FILE_CHECK);
335 magic_list(struct magic_set *ms, const char *magicfile)
337 struct mlist *ml = file_apprentice(ms, magicfile, FILE_LIST);
343 close_and_restore(const struct magic_set *ms, const char *name, int fd,
344 const struct stat *sb)
346 if (fd == STDIN_FILENO)
350 if ((ms->flags & MAGIC_PRESERVE_ATIME) != 0) {
352 * Try to restore access, modification times if read it.
353 * This is really *bad* because it will modify the status
354 * time of the file... And of course this will affect
358 struct timeval utsbuf[2];
359 (void)memset(utsbuf, 0, sizeof(utsbuf));
360 utsbuf[0].tv_sec = sb->st_atime;
361 utsbuf[1].tv_sec = sb->st_mtime;
363 (void) utimes(name, utsbuf); /* don't care if loses */
364 #elif defined(HAVE_UTIME_H) || defined(HAVE_SYS_UTIME_H)
365 struct utimbuf utbuf;
367 (void)memset(&utbuf, 0, sizeof(utbuf));
368 utbuf.actime = sb->st_atime;
369 utbuf.modtime = sb->st_mtime;
370 (void) utime(name, &utbuf); /* don't care if loses */
378 * find type of descriptor
381 magic_descriptor(struct magic_set *ms, int fd)
383 return file_or_fd(ms, NULL, fd);
387 * find type of named file
390 magic_file(struct magic_set *ms, const char *inname)
392 return file_or_fd(ms, inname, STDIN_FILENO);
396 file_or_fd(struct magic_set *ms, const char *inname, int fd)
401 ssize_t nbytes = 0; /* number of bytes read from a datafile */
405 * one extra for terminating '\0', and
406 * some overlapping space for matches near EOF
408 #define SLOP (1 + sizeof(union VALUETYPE))
409 if ((buf = CAST(unsigned char *, malloc(HOWMANY + SLOP))) == NULL)
412 if (file_reset(ms) == -1)
415 switch (file_fsmagic(ms, inname, &sb)) {
418 case 0: /* nothing found */
420 default: /* matched it and printed type */
425 if (inname == NULL) {
426 if (fstat(fd, &sb) == 0 && S_ISFIFO(sb.st_mode))
429 int flags = O_RDONLY|O_BINARY;
431 if (stat(inname, &sb) == 0 && S_ISFIFO(sb.st_mode)) {
439 if ((fd = open(inname, flags)) < 0) {
440 if (unreadable_info(ms, sb.st_mode, inname) == -1)
446 if ((flags = fcntl(fd, F_GETFL)) != -1) {
447 flags &= ~O_NONBLOCK;
448 (void)fcntl(fd, F_SETFL, flags);
454 * try looking at the first HOWMANY bytes
459 while ((r = sread(fd, (void *)&buf[nbytes],
460 (size_t)(HOWMANY - nbytes), 1)) > 0) {
462 if (r < PIPE_BUF) break;
466 /* We can not read it, but we were able to stat it. */
467 if (unreadable_info(ms, sb.st_mode, inname) == -1)
474 if ((nbytes = read(fd, (char *)buf, HOWMANY)) == -1) {
475 file_error(ms, errno, "cannot read `%s'", inname);
480 (void)memset(buf + nbytes, 0, SLOP); /* NUL terminate */
481 if (file_buffer(ms, fd, inname, buf, (size_t)nbytes) == -1)
486 close_and_restore(ms, inname, fd, &sb);
487 return rv == 0 ? file_getbuffer(ms) : NULL;
492 magic_buffer(struct magic_set *ms, const void *buf, size_t nb)
494 if (file_reset(ms) == -1)
497 * The main work is done here!
498 * We have the file name and/or the data buffer to be identified.
500 if (file_buffer(ms, -1, NULL, buf, nb) == -1) {
503 return file_getbuffer(ms);
508 magic_error(struct magic_set *ms)
510 return (ms->event_flags & EVENT_HAD_ERR) ? ms->o.buf : NULL;
514 magic_errno(struct magic_set *ms)
516 return (ms->event_flags & EVENT_HAD_ERR) ? ms->error : 0;
520 magic_setflags(struct magic_set *ms, int flags)
522 #if !defined(HAVE_UTIME) && !defined(HAVE_UTIMES)
523 if (flags & MAGIC_PRESERVE_ATIME)