1133359Sobrien/*
2133359Sobrien * Copyright (c) Christos Zoulas 2003.
3133359Sobrien * All Rights Reserved.
4186691Sobrien *
5133359Sobrien * Redistribution and use in source and binary forms, with or without
6133359Sobrien * modification, are permitted provided that the following conditions
7133359Sobrien * are met:
8133359Sobrien * 1. Redistributions of source code must retain the above copyright
9133359Sobrien *    notice immediately at the beginning of the file, without modification,
10133359Sobrien *    this list of conditions, and the following disclaimer.
11133359Sobrien * 2. Redistributions in binary form must reproduce the above copyright
12133359Sobrien *    notice, this list of conditions and the following disclaimer in the
13133359Sobrien *    documentation and/or other materials provided with the distribution.
14186691Sobrien *
15133359Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16133359Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17133359Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18133359Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19133359Sobrien * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20133359Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21133359Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22133359Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23133359Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24133359Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25133359Sobrien * SUCH DAMAGE.
26133359Sobrien */
27133359Sobrien
28234449Sobrien#ifdef WIN32
29234449Sobrien#include <windows.h>
30234449Sobrien#include <shlwapi.h>
31234449Sobrien#endif
32234449Sobrien
33133359Sobrien#include "file.h"
34191771Sobrien
35191771Sobrien#ifndef	lint
36234449SobrienFILE_RCSID("@(#)$File: magic.c,v 1.74 2011/05/26 01:27:59 christos Exp $")
37191771Sobrien#endif	/* lint */
38191771Sobrien
39133359Sobrien#include "magic.h"
40133359Sobrien
41133359Sobrien#include <stdlib.h>
42133359Sobrien#include <unistd.h>
43133359Sobrien#include <string.h>
44133359Sobrien#ifdef QUICK
45133359Sobrien#include <sys/mman.h>
46133359Sobrien#endif
47186691Sobrien#ifdef HAVE_LIMITS_H
48169942Sobrien#include <limits.h>	/* for PIPE_BUF */
49186691Sobrien#endif
50133359Sobrien
51133359Sobrien#if defined(HAVE_UTIMES)
52133359Sobrien# include <sys/time.h>
53133359Sobrien#elif defined(HAVE_UTIME)
54133359Sobrien# if defined(HAVE_SYS_UTIME_H)
55133359Sobrien#  include <sys/utime.h>
56133359Sobrien# elif defined(HAVE_UTIME_H)
57133359Sobrien#  include <utime.h>
58133359Sobrien# endif
59133359Sobrien#endif
60133359Sobrien
61133359Sobrien#ifdef HAVE_UNISTD_H
62133359Sobrien#include <unistd.h>	/* for read() */
63133359Sobrien#endif
64133359Sobrien
65186691Sobrien#ifndef PIPE_BUF
66186691Sobrien/* Get the PIPE_BUF from pathconf */
67186691Sobrien#ifdef _PC_PIPE_BUF
68186691Sobrien#define PIPE_BUF pathconf(".", _PC_PIPE_BUF)
69186691Sobrien#else
70186691Sobrien#define PIPE_BUF 512
71186691Sobrien#endif
72186691Sobrien#endif
73186691Sobrien
74133359Sobrienprivate void free_mlist(struct mlist *);
75210761Srpaulo#ifndef COMPILE_ONLY
76133359Sobrienprivate void close_and_restore(const struct magic_set *, const char *, int,
77133359Sobrien    const struct stat *);
78186691Sobrienprivate int unreadable_info(struct magic_set *, mode_t, const char *);
79234449Sobrienprivate const char* get_default_magic(void);
80175296Sobrienprivate const char *file_or_fd(struct magic_set *, const char *, int);
81175296Sobrien#endif
82133359Sobrien
83159764Sobrien#ifndef	STDIN_FILENO
84159764Sobrien#define	STDIN_FILENO	0
85159764Sobrien#endif
86159764Sobrien
87234449Sobrienprivate const char *
88234449Sobrienget_default_magic(void)
89234449Sobrien{
90234449Sobrien	static const char hmagic[] = "/.magic/magic.mgc";
91234449Sobrien	static char *default_magic;
92234449Sobrien	char *home, *hmagicpath;
93234449Sobrien
94234449Sobrien#ifndef WIN32
95234449Sobrien	struct stat st;
96234449Sobrien
97234449Sobrien	if (default_magic) {
98234449Sobrien		free(default_magic);
99234449Sobrien		default_magic = NULL;
100234449Sobrien	}
101234449Sobrien	if ((home = getenv("HOME")) == NULL)
102234449Sobrien		return MAGIC;
103234449Sobrien
104234449Sobrien	if (asprintf(&hmagicpath, "%s/.magic", home) < 0)
105234449Sobrien		return MAGIC;
106234449Sobrien	if (stat(hmagicpath, &st) == -1)
107234449Sobrien		goto out;
108234449Sobrien	if (S_ISDIR(st.st_mode)) {
109234449Sobrien		free(hmagicpath);
110234449Sobrien		if (asprintf(&hmagicpath, "%s/%s", home, hmagic) < 0)
111234449Sobrien			return MAGIC;
112234449Sobrien		if (access(hmagicpath, R_OK) == -1)
113234449Sobrien			goto out;
114234449Sobrien	}
115234449Sobrien
116234449Sobrien	if (asprintf(&default_magic, "%s:%s", hmagicpath, MAGIC) < 0)
117234449Sobrien		goto out;
118234449Sobrien	free(hmagicpath);
119234449Sobrien	return default_magic;
120234449Sobrienout:
121234449Sobrien	default_magic = NULL;
122234449Sobrien	free(hmagicpath);
123234449Sobrien	return MAGIC;
124234449Sobrien#else
125234449Sobrien	char *hmagicp = hmagicpath;
126234449Sobrien	char *tmppath = NULL;
127234449Sobrien
128234449Sobrien#define APPENDPATH() \
129234449Sobrien	do { \
130234449Sobrien		if (tmppath && access(tmppath, R_OK) != -1) { \
131234449Sobrien			if (hmagicpath == NULL) \
132234449Sobrien				hmagicpath = tmppath; \
133234449Sobrien			else { \
134234449Sobrien				if (asprintf(&hmagicp, "%s%c%s", hmagicpath, \
135234449Sobrien				    PATHSEP, tmppath) >= 0) { \
136234449Sobrien					free(hmagicpath); \
137234449Sobrien					hmagicpath = hmagicp; \
138234449Sobrien				} \
139234449Sobrien				free(tmppath); \
140234449Sobrien			} \
141234449Sobrien			tmppath = NULL; \
142234449Sobrien		} \
143234449Sobrien	} while (/*CONSTCOND*/0)
144234449Sobrien
145234449Sobrien	if (default_magic) {
146234449Sobrien		free(default_magic);
147234449Sobrien		default_magic = NULL;
148234449Sobrien	}
149234449Sobrien
150234449Sobrien	/* First, try to get user-specific magic file */
151234449Sobrien	if ((home = getenv("LOCALAPPDATA")) == NULL) {
152234449Sobrien		if ((home = getenv("USERPROFILE")) != NULL)
153234449Sobrien			if (asprintf(&tmppath,
154234449Sobrien			    "%s/Local Settings/Application Data%s", home,
155234449Sobrien			    hmagic) < 0)
156234449Sobrien				tmppath = NULL;
157234449Sobrien	} else {
158234449Sobrien		if (asprintf(&tmppath, "%s%s", home, hmagic) < 0)
159234449Sobrien			tmppath = NULL;
160234449Sobrien	}
161234449Sobrien
162234449Sobrien	APPENDPATH();
163234449Sobrien
164234449Sobrien	/* Second, try to get a magic file from Common Files */
165234449Sobrien	if ((home = getenv("COMMONPROGRAMFILES")) != NULL) {
166234449Sobrien		if (asprintf(&tmppath, "%s%s", home, hmagic) >= 0)
167234449Sobrien			APPENDPATH();
168234449Sobrien	}
169234449Sobrien
170234449Sobrien	/* Third, try to get magic file relative to dll location */
171234449Sobrien	LPTSTR dllpath = malloc(sizeof(*dllpath) * (MAX_PATH + 1));
172234449Sobrien	dllpath[MAX_PATH] = 0;	/* just in case long path gets truncated and not null terminated */
173234449Sobrien	if (GetModuleFileNameA(NULL, dllpath, MAX_PATH)){
174234449Sobrien		PathRemoveFileSpecA(dllpath);
175234449Sobrien		if (strlen(dllpath) > 3 &&
176234449Sobrien		    stricmp(&dllpath[strlen(dllpath) - 3], "bin") == 0) {
177234449Sobrien			if (asprintf(&tmppath,
178234449Sobrien			    "%s/../share/misc/magic.mgc", dllpath) >= 0)
179234449Sobrien				APPENDPATH();
180234449Sobrien		} else {
181234449Sobrien			if (asprintf(&tmppath,
182234449Sobrien			    "%s/share/misc/magic.mgc", dllpath) >= 0)
183234449Sobrien				APPENDPATH();
184234449Sobrien			else if (asprintf(&tmppath,
185234449Sobrien			    "%s/magic.mgc", dllpath) >= 0)
186234449Sobrien				APPENDPATH();
187234449Sobrien		}
188234449Sobrien	}
189234449Sobrien
190234449Sobrien	/* Don't put MAGIC constant - it likely points to a file within MSys
191234449Sobrien	tree */
192234449Sobrien	default_magic = hmagicpath;
193234449Sobrien	return default_magic;
194234449Sobrien#endif
195234449Sobrien}
196234449Sobrien
197234449Sobrienpublic const char *
198234449Sobrienmagic_getpath(const char *magicfile, int action)
199234449Sobrien{
200234449Sobrien	if (magicfile != NULL)
201234449Sobrien		return magicfile;
202234449Sobrien
203234449Sobrien	magicfile = getenv("MAGIC");
204234449Sobrien	if (magicfile != NULL)
205234449Sobrien		return magicfile;
206234449Sobrien
207234449Sobrien	return action == FILE_LOAD ? get_default_magic() : MAGIC;
208234449Sobrien}
209234449Sobrien
210133359Sobrienpublic struct magic_set *
211133359Sobrienmagic_open(int flags)
212133359Sobrien{
213133359Sobrien	struct magic_set *ms;
214186691Sobrien	size_t len;
215133359Sobrien
216234449Sobrien	if ((ms = CAST(struct magic_set *, calloc((size_t)1,
217186691Sobrien	    sizeof(struct magic_set)))) == NULL)
218133359Sobrien		return NULL;
219133359Sobrien
220133359Sobrien	if (magic_setflags(ms, flags) == -1) {
221133359Sobrien		errno = EINVAL;
222186691Sobrien		goto free;
223133359Sobrien	}
224133359Sobrien
225186691Sobrien	ms->o.buf = ms->o.pbuf = NULL;
226186691Sobrien	len = (ms->c.len = 10) * sizeof(*ms->c.li);
227139368Sobrien
228186691Sobrien	if ((ms->c.li = CAST(struct level_info *, malloc(len))) == NULL)
229186691Sobrien		goto free;
230139368Sobrien
231191771Sobrien	ms->event_flags = 0;
232133359Sobrien	ms->error = -1;
233133359Sobrien	ms->mlist = NULL;
234169962Sobrien	ms->file = "unknown";
235169962Sobrien	ms->line = 0;
236133359Sobrien	return ms;
237186691Sobrienfree:
238139368Sobrien	free(ms);
239139368Sobrien	return NULL;
240133359Sobrien}
241133359Sobrien
242133359Sobrienprivate void
243133359Sobrienfree_mlist(struct mlist *mlist)
244133359Sobrien{
245133359Sobrien	struct mlist *ml;
246133359Sobrien
247133359Sobrien	if (mlist == NULL)
248133359Sobrien		return;
249133359Sobrien
250133359Sobrien	for (ml = mlist->next; ml != mlist;) {
251133359Sobrien		struct mlist *next = ml->next;
252133359Sobrien		struct magic *mg = ml->magic;
253133359Sobrien		file_delmagic(mg, ml->mapped, ml->nmagic);
254133359Sobrien		free(ml);
255133359Sobrien		ml = next;
256133359Sobrien	}
257133359Sobrien	free(ml);
258133359Sobrien}
259133359Sobrien
260210761Srpaulo#ifndef COMPILE_ONLY
261169942Sobrienprivate int
262186691Sobrienunreadable_info(struct magic_set *ms, mode_t md, const char *file)
263169942Sobrien{
264169942Sobrien	/* We cannot open it, but we were able to stat it. */
265186691Sobrien	if (access(file, W_OK) == 0)
266169942Sobrien		if (file_printf(ms, "writable, ") == -1)
267169942Sobrien			return -1;
268186691Sobrien	if (access(file, X_OK) == 0)
269169942Sobrien		if (file_printf(ms, "executable, ") == -1)
270169942Sobrien			return -1;
271169942Sobrien	if (S_ISREG(md))
272169942Sobrien		if (file_printf(ms, "regular file, ") == -1)
273169942Sobrien			return -1;
274169942Sobrien	if (file_printf(ms, "no read permission") == -1)
275169942Sobrien		return -1;
276169942Sobrien	return 0;
277169942Sobrien}
278210761Srpaulo#endif
279169942Sobrien
280133359Sobrienpublic void
281169942Sobrienmagic_close(struct magic_set *ms)
282133359Sobrien{
283133359Sobrien	free_mlist(ms->mlist);
284139368Sobrien	free(ms->o.pbuf);
285133359Sobrien	free(ms->o.buf);
286169962Sobrien	free(ms->c.li);
287133359Sobrien	free(ms);
288133359Sobrien}
289133359Sobrien
290133359Sobrien/*
291133359Sobrien * load a magic file
292133359Sobrien */
293133359Sobrienpublic int
294133359Sobrienmagic_load(struct magic_set *ms, const char *magicfile)
295133359Sobrien{
296133359Sobrien	struct mlist *ml = file_apprentice(ms, magicfile, FILE_LOAD);
297133359Sobrien	if (ml) {
298133359Sobrien		free_mlist(ms->mlist);
299133359Sobrien		ms->mlist = ml;
300133359Sobrien		return 0;
301133359Sobrien	}
302133359Sobrien	return -1;
303133359Sobrien}
304133359Sobrien
305133359Sobrienpublic int
306133359Sobrienmagic_compile(struct magic_set *ms, const char *magicfile)
307133359Sobrien{
308133359Sobrien	struct mlist *ml = file_apprentice(ms, magicfile, FILE_COMPILE);
309133359Sobrien	free_mlist(ml);
310133359Sobrien	return ml ? 0 : -1;
311133359Sobrien}
312133359Sobrien
313133359Sobrienpublic int
314133359Sobrienmagic_check(struct magic_set *ms, const char *magicfile)
315133359Sobrien{
316133359Sobrien	struct mlist *ml = file_apprentice(ms, magicfile, FILE_CHECK);
317133359Sobrien	free_mlist(ml);
318133359Sobrien	return ml ? 0 : -1;
319133359Sobrien}
320133359Sobrien
321234449Sobrienpublic int
322234449Sobrienmagic_list(struct magic_set *ms, const char *magicfile)
323234449Sobrien{
324234449Sobrien	struct mlist *ml = file_apprentice(ms, magicfile, FILE_LIST);
325234449Sobrien	free_mlist(ml);
326234449Sobrien	return ml ? 0 : -1;
327234449Sobrien}
328234449Sobrien
329210761Srpaulo#ifndef COMPILE_ONLY
330133359Sobrienprivate void
331133359Sobrienclose_and_restore(const struct magic_set *ms, const char *name, int fd,
332133359Sobrien    const struct stat *sb)
333133359Sobrien{
334159764Sobrien	if (fd == STDIN_FILENO)
335159764Sobrien		return;
336133359Sobrien	(void) close(fd);
337159764Sobrien
338159764Sobrien	if ((ms->flags & MAGIC_PRESERVE_ATIME) != 0) {
339133359Sobrien		/*
340133359Sobrien		 * Try to restore access, modification times if read it.
341133359Sobrien		 * This is really *bad* because it will modify the status
342133359Sobrien		 * time of the file... And of course this will affect
343133359Sobrien		 * backup programs
344133359Sobrien		 */
345133359Sobrien#ifdef HAVE_UTIMES
346133359Sobrien		struct timeval  utsbuf[2];
347186691Sobrien		(void)memset(utsbuf, 0, sizeof(utsbuf));
348133359Sobrien		utsbuf[0].tv_sec = sb->st_atime;
349133359Sobrien		utsbuf[1].tv_sec = sb->st_mtime;
350133359Sobrien
351133359Sobrien		(void) utimes(name, utsbuf); /* don't care if loses */
352133359Sobrien#elif defined(HAVE_UTIME_H) || defined(HAVE_SYS_UTIME_H)
353133359Sobrien		struct utimbuf  utbuf;
354133359Sobrien
355191771Sobrien		(void)memset(&utbuf, 0, sizeof(utbuf));
356133359Sobrien		utbuf.actime = sb->st_atime;
357133359Sobrien		utbuf.modtime = sb->st_mtime;
358133359Sobrien		(void) utime(name, &utbuf); /* don't care if loses */
359133359Sobrien#endif
360133359Sobrien	}
361133359Sobrien}
362133359Sobrien
363175296Sobrien
364133359Sobrien/*
365175296Sobrien * find type of descriptor
366175296Sobrien */
367175296Sobrienpublic const char *
368175296Sobrienmagic_descriptor(struct magic_set *ms, int fd)
369175296Sobrien{
370175296Sobrien	return file_or_fd(ms, NULL, fd);
371175296Sobrien}
372175296Sobrien
373175296Sobrien/*
374133359Sobrien * find type of named file
375133359Sobrien */
376133359Sobrienpublic const char *
377133359Sobrienmagic_file(struct magic_set *ms, const char *inname)
378133359Sobrien{
379175296Sobrien	return file_or_fd(ms, inname, STDIN_FILENO);
380175296Sobrien}
381175296Sobrien
382175296Sobrienprivate const char *
383175296Sobrienfile_or_fd(struct magic_set *ms, const char *inname, int fd)
384175296Sobrien{
385159764Sobrien	int	rv = -1;
386159764Sobrien	unsigned char *buf;
387133359Sobrien	struct stat	sb;
388133359Sobrien	ssize_t nbytes = 0;	/* number of bytes read from a datafile */
389169942Sobrien	int	ispipe = 0;
390133359Sobrien
391159764Sobrien	/*
392159764Sobrien	 * one extra for terminating '\0', and
393159764Sobrien	 * some overlapping space for matches near EOF
394159764Sobrien	 */
395159764Sobrien#define SLOP (1 + sizeof(union VALUETYPE))
396186691Sobrien	if ((buf = CAST(unsigned char *, malloc(HOWMANY + SLOP))) == NULL)
397133359Sobrien		return NULL;
398133359Sobrien
399159764Sobrien	if (file_reset(ms) == -1)
400159764Sobrien		goto done;
401159764Sobrien
402133359Sobrien	switch (file_fsmagic(ms, inname, &sb)) {
403169942Sobrien	case -1:		/* error */
404159764Sobrien		goto done;
405169942Sobrien	case 0:			/* nothing found */
406133359Sobrien		break;
407169942Sobrien	default:		/* matched it and printed type */
408159764Sobrien		rv = 0;
409159764Sobrien		goto done;
410133359Sobrien	}
411133359Sobrien
412169942Sobrien	if (inname == NULL) {
413169942Sobrien		if (fstat(fd, &sb) == 0 && S_ISFIFO(sb.st_mode))
414169942Sobrien			ispipe = 1;
415169942Sobrien	} else {
416169942Sobrien		int flags = O_RDONLY|O_BINARY;
417169942Sobrien
418169942Sobrien		if (stat(inname, &sb) == 0 && S_ISFIFO(sb.st_mode)) {
419234449Sobrien#ifdef O_NONBLOCK
420169942Sobrien			flags |= O_NONBLOCK;
421234449Sobrien#endif
422169942Sobrien			ispipe = 1;
423169942Sobrien		}
424169942Sobrien
425169942Sobrien		errno = 0;
426169942Sobrien		if ((fd = open(inname, flags)) < 0) {
427192350Sdelphij			if (unreadable_info(ms, sb.st_mode, inname) == -1)
428175296Sobrien				goto done;
429192350Sdelphij			rv = 0;
430192350Sdelphij			goto done;
431169942Sobrien		}
432169942Sobrien#ifdef O_NONBLOCK
433169942Sobrien		if ((flags = fcntl(fd, F_GETFL)) != -1) {
434169942Sobrien			flags &= ~O_NONBLOCK;
435169942Sobrien			(void)fcntl(fd, F_SETFL, flags);
436169942Sobrien		}
437169942Sobrien#endif
438133359Sobrien	}
439133359Sobrien
440133359Sobrien	/*
441133359Sobrien	 * try looking at the first HOWMANY bytes
442133359Sobrien	 */
443169942Sobrien	if (ispipe) {
444169942Sobrien		ssize_t r = 0;
445169942Sobrien
446169942Sobrien		while ((r = sread(fd, (void *)&buf[nbytes],
447169962Sobrien		    (size_t)(HOWMANY - nbytes), 1)) > 0) {
448169942Sobrien			nbytes += r;
449169942Sobrien			if (r < PIPE_BUF) break;
450169942Sobrien		}
451169942Sobrien
452169942Sobrien		if (nbytes == 0) {
453169942Sobrien			/* We can not read it, but we were able to stat it. */
454186691Sobrien			if (unreadable_info(ms, sb.st_mode, inname) == -1)
455169942Sobrien				goto done;
456169942Sobrien			rv = 0;
457169942Sobrien			goto done;
458169942Sobrien		}
459169942Sobrien
460169942Sobrien	} else {
461169942Sobrien		if ((nbytes = read(fd, (char *)buf, HOWMANY)) == -1) {
462169942Sobrien			file_error(ms, errno, "cannot read `%s'", inname);
463169942Sobrien			goto done;
464169942Sobrien		}
465133359Sobrien	}
466133359Sobrien
467175296Sobrien	(void)memset(buf + nbytes, 0, SLOP); /* NUL terminate */
468175296Sobrien	if (file_buffer(ms, fd, inname, buf, (size_t)nbytes) == -1)
469175296Sobrien		goto done;
470159764Sobrien	rv = 0;
471133359Sobriendone:
472159764Sobrien	free(buf);
473133359Sobrien	close_and_restore(ms, inname, fd, &sb);
474159764Sobrien	return rv == 0 ? file_getbuffer(ms) : NULL;
475133359Sobrien}
476133359Sobrien
477133359Sobrien
478133359Sobrienpublic const char *
479133359Sobrienmagic_buffer(struct magic_set *ms, const void *buf, size_t nb)
480133359Sobrien{
481133359Sobrien	if (file_reset(ms) == -1)
482133359Sobrien		return NULL;
483133359Sobrien	/*
484133359Sobrien	 * The main work is done here!
485186691Sobrien	 * We have the file name and/or the data buffer to be identified.
486133359Sobrien	 */
487169962Sobrien	if (file_buffer(ms, -1, NULL, buf, nb) == -1) {
488133359Sobrien		return NULL;
489133359Sobrien	}
490133359Sobrien	return file_getbuffer(ms);
491133359Sobrien}
492210761Srpaulo#endif /* COMPILE_ONLY */
493133359Sobrien
494133359Sobrienpublic const char *
495133359Sobrienmagic_error(struct magic_set *ms)
496133359Sobrien{
497191771Sobrien	return (ms->event_flags & EVENT_HAD_ERR) ? ms->o.buf : NULL;
498133359Sobrien}
499133359Sobrien
500133359Sobrienpublic int
501133359Sobrienmagic_errno(struct magic_set *ms)
502133359Sobrien{
503191771Sobrien	return (ms->event_flags & EVENT_HAD_ERR) ? ms->error : 0;
504133359Sobrien}
505133359Sobrien
506133359Sobrienpublic int
507133359Sobrienmagic_setflags(struct magic_set *ms, int flags)
508133359Sobrien{
509133359Sobrien#if !defined(HAVE_UTIME) && !defined(HAVE_UTIMES)
510133359Sobrien	if (flags & MAGIC_PRESERVE_ATIME)
511133359Sobrien		return -1;
512133359Sobrien#endif
513133359Sobrien	ms->flags = flags;
514133359Sobrien	return 0;
515133359Sobrien}
516