file: update vendor branch to v5.04
[dragonfly.git] / contrib / file / src / magic.c
blobd49c29a3d8f0a23f4b1c5ce593bbe4d9a98430c7
1 /*
2 * Copyright (c) Christos Zoulas 2003.
3 * All Rights Reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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
25 * SUCH DAMAGE.
28 #include "file.h"
30 #ifndef lint
31 FILE_RCSID("@(#)$File: magic.c,v 1.65 2009/09/14 17:50:38 christos Exp $")
32 #endif /* lint */
34 #include "magic.h"
36 #include <stdlib.h>
37 #include <unistd.h>
38 #include <string.h>
39 #ifdef QUICK
40 #include <sys/mman.h>
41 #endif
42 #ifdef HAVE_LIMITS_H
43 #include <limits.h> /* for PIPE_BUF */
44 #endif
46 #if defined(HAVE_UTIMES)
47 # include <sys/time.h>
48 #elif defined(HAVE_UTIME)
49 # if defined(HAVE_SYS_UTIME_H)
50 # include <sys/utime.h>
51 # elif defined(HAVE_UTIME_H)
52 # include <utime.h>
53 # endif
54 #endif
56 #ifdef HAVE_UNISTD_H
57 #include <unistd.h> /* for read() */
58 #endif
60 #include "patchlevel.h"
62 #ifndef PIPE_BUF
63 /* Get the PIPE_BUF from pathconf */
64 #ifdef _PC_PIPE_BUF
65 #define PIPE_BUF pathconf(".", _PC_PIPE_BUF)
66 #else
67 #define PIPE_BUF 512
68 #endif
69 #endif
71 private void free_mlist(struct mlist *);
72 private void close_and_restore(const struct magic_set *, const char *, int,
73 const struct stat *);
74 private int unreadable_info(struct magic_set *, mode_t, const char *);
75 private const char* get_default_magic(void);
76 #ifndef COMPILE_ONLY
77 private const char *file_or_fd(struct magic_set *, const char *, int);
78 #endif
80 #ifndef STDIN_FILENO
81 #define STDIN_FILENO 0
82 #endif
84 private const char *
85 get_default_magic(void)
87 static const char hmagic[] = "/.magic";
88 static char default_magic[2 * MAXPATHLEN + 2];
89 char *home;
90 char hmagicpath[MAXPATHLEN + 1];
92 if ((home = getenv("HOME")) == NULL)
93 return MAGIC;
95 (void)snprintf(hmagicpath, sizeof(hmagicpath), "%s%s", home, hmagic);
97 if (access(hmagicpath, R_OK) == -1)
98 return MAGIC;
100 (void)snprintf(default_magic, sizeof(default_magic), "%s:%s",
101 hmagicpath, MAGIC);
103 return default_magic;
106 public const char *
107 magic_getpath(const char *magicfile, int action)
109 if (magicfile != NULL)
110 return magicfile;
112 magicfile = getenv("MAGIC");
113 if (magicfile != NULL)
114 return magicfile;
116 return action == FILE_LOAD ? get_default_magic() : MAGIC;
119 public struct magic_set *
120 magic_open(int flags)
122 struct magic_set *ms;
123 size_t len;
125 if ((ms = CAST(struct magic_set *, calloc((size_t)1,
126 sizeof(struct magic_set)))) == NULL)
127 return NULL;
129 if (magic_setflags(ms, flags) == -1) {
130 errno = EINVAL;
131 goto free;
134 ms->o.buf = ms->o.pbuf = NULL;
135 len = (ms->c.len = 10) * sizeof(*ms->c.li);
137 if ((ms->c.li = CAST(struct level_info *, malloc(len))) == NULL)
138 goto free;
140 ms->event_flags = 0;
141 ms->error = -1;
142 ms->mlist = NULL;
143 ms->file = "unknown";
144 ms->line = 0;
145 return ms;
146 free:
147 free(ms);
148 return NULL;
151 private void
152 free_mlist(struct mlist *mlist)
154 struct mlist *ml;
156 if (mlist == NULL)
157 return;
159 for (ml = mlist->next; ml != mlist;) {
160 struct mlist *next = ml->next;
161 struct magic *mg = ml->magic;
162 file_delmagic(mg, ml->mapped, ml->nmagic);
163 free(ml);
164 ml = next;
166 free(ml);
169 private int
170 unreadable_info(struct magic_set *ms, mode_t md, const char *file)
172 /* We cannot open it, but we were able to stat it. */
173 if (access(file, W_OK) == 0)
174 if (file_printf(ms, "writable, ") == -1)
175 return -1;
176 if (access(file, X_OK) == 0)
177 if (file_printf(ms, "executable, ") == -1)
178 return -1;
179 if (S_ISREG(md))
180 if (file_printf(ms, "regular file, ") == -1)
181 return -1;
182 if (file_printf(ms, "no read permission") == -1)
183 return -1;
184 return 0;
187 public void
188 magic_close(struct magic_set *ms)
190 free_mlist(ms->mlist);
191 free(ms->o.pbuf);
192 free(ms->o.buf);
193 free(ms->c.li);
194 free(ms);
198 * load a magic file
200 public int
201 magic_load(struct magic_set *ms, const char *magicfile)
203 struct mlist *ml = file_apprentice(ms, magicfile, FILE_LOAD);
204 if (ml) {
205 free_mlist(ms->mlist);
206 ms->mlist = ml;
207 return 0;
209 return -1;
212 public int
213 magic_compile(struct magic_set *ms, const char *magicfile)
215 struct mlist *ml = file_apprentice(ms, magicfile, FILE_COMPILE);
216 free_mlist(ml);
217 return ml ? 0 : -1;
220 public int
221 magic_check(struct magic_set *ms, const char *magicfile)
223 struct mlist *ml = file_apprentice(ms, magicfile, FILE_CHECK);
224 free_mlist(ml);
225 return ml ? 0 : -1;
228 private void
229 close_and_restore(const struct magic_set *ms, const char *name, int fd,
230 const struct stat *sb)
232 if (fd == STDIN_FILENO)
233 return;
234 (void) close(fd);
236 if ((ms->flags & MAGIC_PRESERVE_ATIME) != 0) {
238 * Try to restore access, modification times if read it.
239 * This is really *bad* because it will modify the status
240 * time of the file... And of course this will affect
241 * backup programs
243 #ifdef HAVE_UTIMES
244 struct timeval utsbuf[2];
245 (void)memset(utsbuf, 0, sizeof(utsbuf));
246 utsbuf[0].tv_sec = sb->st_atime;
247 utsbuf[1].tv_sec = sb->st_mtime;
249 (void) utimes(name, utsbuf); /* don't care if loses */
250 #elif defined(HAVE_UTIME_H) || defined(HAVE_SYS_UTIME_H)
251 struct utimbuf utbuf;
253 (void)memset(&utbuf, 0, sizeof(utbuf));
254 utbuf.actime = sb->st_atime;
255 utbuf.modtime = sb->st_mtime;
256 (void) utime(name, &utbuf); /* don't care if loses */
257 #endif
261 #ifndef COMPILE_ONLY
264 * find type of descriptor
266 public const char *
267 magic_descriptor(struct magic_set *ms, int fd)
269 return file_or_fd(ms, NULL, fd);
273 * find type of named file
275 public const char *
276 magic_file(struct magic_set *ms, const char *inname)
278 return file_or_fd(ms, inname, STDIN_FILENO);
281 private const char *
282 file_or_fd(struct magic_set *ms, const char *inname, int fd)
284 int rv = -1;
285 unsigned char *buf;
286 struct stat sb;
287 ssize_t nbytes = 0; /* number of bytes read from a datafile */
288 int ispipe = 0;
291 * one extra for terminating '\0', and
292 * some overlapping space for matches near EOF
294 #define SLOP (1 + sizeof(union VALUETYPE))
295 if ((buf = CAST(unsigned char *, malloc(HOWMANY + SLOP))) == NULL)
296 return NULL;
298 if (file_reset(ms) == -1)
299 goto done;
301 switch (file_fsmagic(ms, inname, &sb)) {
302 case -1: /* error */
303 goto done;
304 case 0: /* nothing found */
305 break;
306 default: /* matched it and printed type */
307 rv = 0;
308 goto done;
311 if (inname == NULL) {
312 if (fstat(fd, &sb) == 0 && S_ISFIFO(sb.st_mode))
313 ispipe = 1;
314 } else {
315 int flags = O_RDONLY|O_BINARY;
317 if (stat(inname, &sb) == 0 && S_ISFIFO(sb.st_mode)) {
318 flags |= O_NONBLOCK;
319 ispipe = 1;
322 errno = 0;
323 if ((fd = open(inname, flags)) < 0) {
324 if (unreadable_info(ms, sb.st_mode, inname) == -1)
325 goto done;
326 rv = 0;
327 goto done;
329 #ifdef O_NONBLOCK
330 if ((flags = fcntl(fd, F_GETFL)) != -1) {
331 flags &= ~O_NONBLOCK;
332 (void)fcntl(fd, F_SETFL, flags);
334 #endif
338 * try looking at the first HOWMANY bytes
340 if (ispipe) {
341 ssize_t r = 0;
343 while ((r = sread(fd, (void *)&buf[nbytes],
344 (size_t)(HOWMANY - nbytes), 1)) > 0) {
345 nbytes += r;
346 if (r < PIPE_BUF) break;
349 if (nbytes == 0) {
350 /* We can not read it, but we were able to stat it. */
351 if (unreadable_info(ms, sb.st_mode, inname) == -1)
352 goto done;
353 rv = 0;
354 goto done;
357 } else {
358 if ((nbytes = read(fd, (char *)buf, HOWMANY)) == -1) {
359 file_error(ms, errno, "cannot read `%s'", inname);
360 goto done;
364 (void)memset(buf + nbytes, 0, SLOP); /* NUL terminate */
365 if (file_buffer(ms, fd, inname, buf, (size_t)nbytes) == -1)
366 goto done;
367 rv = 0;
368 done:
369 free(buf);
370 close_and_restore(ms, inname, fd, &sb);
371 return rv == 0 ? file_getbuffer(ms) : NULL;
375 public const char *
376 magic_buffer(struct magic_set *ms, const void *buf, size_t nb)
378 if (file_reset(ms) == -1)
379 return NULL;
381 * The main work is done here!
382 * We have the file name and/or the data buffer to be identified.
384 if (file_buffer(ms, -1, NULL, buf, nb) == -1) {
385 return NULL;
387 return file_getbuffer(ms);
389 #endif
391 public const char *
392 magic_error(struct magic_set *ms)
394 return (ms->event_flags & EVENT_HAD_ERR) ? ms->o.buf : NULL;
397 public int
398 magic_errno(struct magic_set *ms)
400 return (ms->event_flags & EVENT_HAD_ERR) ? ms->error : 0;
403 public int
404 magic_setflags(struct magic_set *ms, int flags)
406 #if !defined(HAVE_UTIME) && !defined(HAVE_UTIMES)
407 if (flags & MAGIC_PRESERVE_ATIME)
408 return -1;
409 #endif
410 ms->flags = flags;
411 return 0;