2 * Copyright (c) Ian F. Darwin 1986-1995.
3 * Software written by Ian F. Darwin and others;
4 * maintained 1995-present by Christos Zoulas and others.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice immediately at the beginning of the file, without modification,
11 * this list of conditions, and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * fsmagic - magic based on filesystem info - directory, special files, etc.
40 /* Since major is a function on SVR4, we cannot use `ifndef major'. */
42 # include <sys/mkdev.h>
45 #ifdef MAJOR_IN_SYSMACROS
46 # include <sys/sysmacros.h>
49 #ifdef major /* Might be defined in sys/types.h. */
54 # define major(dev) (((dev) >> 8) & 0xff)
55 # define minor(dev) ((dev) & 0xff)
60 FILE_RCSID("@(#)$File: fsmagic.c,v 1.48 2007/10/17 19:33:31 christos Exp $")
64 file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb)
67 int mime = ms->flags & MAGIC_MIME;
78 * Fstat is cheaper but fails for files you don't have read perms on.
79 * On 4.2BSD and similar systems, use lstat() to identify symlinks.
82 if ((ms->flags & MAGIC_SYMLINK) == 0)
86 ret = stat(fn, sb); /* don't merge into if; see "ret =" above */
89 if (ms->flags & MAGIC_ERROR) {
90 file_error(ms, errno, "cannot stat `%s'", fn);
93 if (file_printf(ms, "cannot open `%s' (%s)",
94 fn, strerror(errno)) == -1)
100 if ((sb->st_mode & S_IFMT) != S_IFREG) {
101 if ((mime & MAGIC_MIME_TYPE) &&
102 file_printf(ms, "application/x-not-regular-file")
110 if (sb->st_mode & S_ISUID)
111 if (file_printf(ms, "setuid ") == -1)
115 if (sb->st_mode & S_ISGID)
116 if (file_printf(ms, "setgid ") == -1)
120 if (sb->st_mode & S_ISVTX)
121 if (file_printf(ms, "sticky ") == -1)
126 switch (sb->st_mode & S_IFMT) {
128 if (file_printf(ms, "directory") == -1)
134 * If -s has been specified, treat character special files
135 * like ordinary files. Otherwise, just report that they
136 * are block special files and go on to the next file.
138 if ((ms->flags & MAGIC_DEVICES) != 0)
142 if (file_printf(ms, "character special (%d/%d/%d)",
143 major(sb->st_rdev), dv_unit(sb->st_rdev),
144 dv_subunit(sb->st_rdev)) == -1)
147 if (file_printf(ms, "character special (%ld/%ld)",
148 (long) major(sb->st_rdev), (long) minor(sb->st_rdev)) == -1)
152 if (file_printf(ms, "character special") == -1)
160 * If -s has been specified, treat block special files
161 * like ordinary files. Otherwise, just report that they
162 * are block special files and go on to the next file.
164 if ((ms->flags & MAGIC_DEVICES) != 0)
168 if (file_printf(ms, "block special (%d/%d/%d)",
169 major(sb->st_rdev), dv_unit(sb->st_rdev),
170 dv_subunit(sb->st_rdev)) == -1)
173 if (file_printf(ms, "block special (%ld/%ld)",
174 (long)major(sb->st_rdev), (long)minor(sb->st_rdev)) == -1)
178 if (file_printf(ms, "block special") == -1)
183 /* TODO add code to handle V7 MUX and Blit MUX files */
186 if((ms->flags & MAGIC_DEVICES) != 0)
188 if (file_printf(ms, "fifo (named pipe)") == -1)
194 if (file_printf(ms, "door") == -1)
200 if ((nch = readlink(fn, buf, BUFSIZ-1)) <= 0) {
201 if (ms->flags & MAGIC_ERROR) {
202 file_error(ms, errno, "unreadable symlink `%s'",
207 "unreadable symlink `%s' (%s)", fn,
208 strerror(errno)) == -1)
212 buf[nch] = '\0'; /* readlink(2) forgets this */
214 /* If broken symlink, say so and quit early. */
216 if (stat(buf, &tstatbuf) < 0) {
217 if (ms->flags & MAGIC_ERROR) {
218 file_error(ms, errno,
219 "broken symbolic link to `%s'", buf);
222 if (file_printf(ms, "broken symbolic link to `%s'",
230 char buf2[BUFSIZ+BUFSIZ+4];
232 if ((tmp = strrchr(fn, '/')) == NULL) {
233 tmp = buf; /* in current directory anyway */
235 if (tmp - fn + 1 > BUFSIZ) {
236 if (ms->flags & MAGIC_ERROR) {
238 "path too long: `%s'", buf);
242 "path too long: `%s'", fn) == -1)
246 (void)strcpy(buf2, fn); /* take dir part */
247 buf2[tmp - fn + 1] = '\0';
248 (void)strcat(buf2, buf); /* plus (rel) link */
251 if (stat(tmp, &tstatbuf) < 0) {
252 if (ms->flags & MAGIC_ERROR) {
253 file_error(ms, errno,
254 "broken symbolic link to `%s'",
259 "broken symbolic link to `%s'", buf) == -1)
265 /* Otherwise, handle it. */
266 if ((ms->flags & MAGIC_SYMLINK) != 0) {
268 ms->flags &= MAGIC_SYMLINK;
269 p = magic_file(ms, buf);
270 ms->flags |= MAGIC_SYMLINK;
271 return p != NULL ? 1 : -1;
272 } else { /* just print what it points to */
273 if (file_printf(ms, "symbolic link to `%s'",
282 if (file_printf(ms, "socket") == -1)
290 file_error(ms, 0, "invalid mode 0%o", sb->st_mode);
296 * regular file, check next possibility
298 * If stat() tells us the file has zero length, report here that
299 * the file is empty, so we can skip all the work of opening and
301 * But if the -s option has been given, we skip this optimization,
302 * since on some systems, stat() reports zero size for raw disk
303 * partitions. (If the block special device really has zero length,
304 * the fact that it is empty will be detected and reported correctly
305 * when we read the file.)
307 if ((ms->flags & MAGIC_DEVICES) == 0 && sb->st_size == 0) {
308 if ((!mime || (mime & MAGIC_MIME_TYPE)) &&
309 file_printf(ms, mime ? "application/x-empty" :