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.47 2007/01/12 17:38:28 christos Exp $")
64 file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb)
77 * Fstat is cheaper but fails for files you don't have read perms on.
78 * On 4.2BSD and similar systems, use lstat() to identify symlinks.
81 if ((ms->flags & MAGIC_SYMLINK) == 0)
85 ret = stat(fn, sb); /* don't merge into if; see "ret =" above */
88 if (ms->flags & MAGIC_ERROR) {
89 file_error(ms, errno, "cannot stat `%s'", fn);
92 if (file_printf(ms, "cannot open `%s' (%s)",
93 fn, strerror(errno)) == -1)
98 if ((ms->flags & MAGIC_MIME) != 0) {
99 if ((sb->st_mode & S_IFMT) != S_IFREG) {
100 if (file_printf(ms, "application/x-not-regular-file")
108 if (sb->st_mode & S_ISUID)
109 if (file_printf(ms, "setuid ") == -1)
113 if (sb->st_mode & S_ISGID)
114 if (file_printf(ms, "setgid ") == -1)
118 if (sb->st_mode & S_ISVTX)
119 if (file_printf(ms, "sticky ") == -1)
124 switch (sb->st_mode & S_IFMT) {
126 if (file_printf(ms, "directory") == -1)
132 * If -s has been specified, treat character special files
133 * like ordinary files. Otherwise, just report that they
134 * are block special files and go on to the next file.
136 if ((ms->flags & MAGIC_DEVICES) != 0)
140 if (file_printf(ms, "character special (%d/%d/%d)",
141 major(sb->st_rdev), dv_unit(sb->st_rdev),
142 dv_subunit(sb->st_rdev)) == -1)
145 if (file_printf(ms, "character special (%ld/%ld)",
146 (long) major(sb->st_rdev), (long) minor(sb->st_rdev)) == -1)
150 if (file_printf(ms, "character special") == -1)
158 * If -s has been specified, treat block special files
159 * like ordinary files. Otherwise, just report that they
160 * are block special files and go on to the next file.
162 if ((ms->flags & MAGIC_DEVICES) != 0)
166 if (file_printf(ms, "block special (%d/%d/%d)",
167 major(sb->st_rdev), dv_unit(sb->st_rdev),
168 dv_subunit(sb->st_rdev)) == -1)
171 if (file_printf(ms, "block special (%ld/%ld)",
172 (long)major(sb->st_rdev), (long)minor(sb->st_rdev)) == -1)
176 if (file_printf(ms, "block special") == -1)
181 /* TODO add code to handle V7 MUX and Blit MUX files */
184 if((ms->flags & MAGIC_DEVICES) != 0)
186 if (file_printf(ms, "fifo (named pipe)") == -1)
192 if (file_printf(ms, "door") == -1)
198 if ((nch = readlink(fn, buf, BUFSIZ-1)) <= 0) {
199 if (ms->flags & MAGIC_ERROR) {
200 file_error(ms, errno, "unreadable symlink `%s'",
205 "unreadable symlink `%s' (%s)", fn,
206 strerror(errno)) == -1)
210 buf[nch] = '\0'; /* readlink(2) forgets this */
212 /* If broken symlink, say so and quit early. */
214 if (stat(buf, &tstatbuf) < 0) {
215 if (ms->flags & MAGIC_ERROR) {
216 file_error(ms, errno,
217 "broken symbolic link to `%s'", buf);
220 if (file_printf(ms, "broken symbolic link to `%s'",
228 char buf2[BUFSIZ+BUFSIZ+4];
230 if ((tmp = strrchr(fn, '/')) == NULL) {
231 tmp = buf; /* in current directory anyway */
233 if (tmp - fn + 1 > BUFSIZ) {
234 if (ms->flags & MAGIC_ERROR) {
236 "path too long: `%s'", buf);
240 "path too long: `%s'", fn) == -1)
244 (void)strcpy(buf2, fn); /* take dir part */
245 buf2[tmp - fn + 1] = '\0';
246 (void)strcat(buf2, buf); /* plus (rel) link */
249 if (stat(tmp, &tstatbuf) < 0) {
250 if (ms->flags & MAGIC_ERROR) {
251 file_error(ms, errno,
252 "broken symbolic link to `%s'",
257 "broken symbolic link to `%s'", buf) == -1)
263 /* Otherwise, handle it. */
264 if ((ms->flags & MAGIC_SYMLINK) != 0) {
266 ms->flags &= MAGIC_SYMLINK;
267 p = magic_file(ms, buf);
268 ms->flags |= MAGIC_SYMLINK;
269 return p != NULL ? 1 : -1;
270 } else { /* just print what it points to */
271 if (file_printf(ms, "symbolic link to `%s'",
280 if (file_printf(ms, "socket") == -1)
288 file_error(ms, 0, "invalid mode 0%o", sb->st_mode);
294 * regular file, check next possibility
296 * If stat() tells us the file has zero length, report here that
297 * the file is empty, so we can skip all the work of opening and
299 * But if the -s option has been given, we skip this optimization,
300 * since on some systems, stat() reports zero size for raw disk
301 * partitions. (If the block special device really has zero length,
302 * the fact that it is empty will be detected and reported correctly
303 * when we read the file.)
305 if ((ms->flags & MAGIC_DEVICES) == 0 && sb->st_size == 0) {
306 if (file_printf(ms, (ms->flags & MAGIC_MIME) ?
307 "application/x-empty" : "empty") == -1)