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("@(#)$Id: fsmagic.c,v 1.45 2004/11/13 10:19:48 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 (file_printf(ms
, "fifo (named pipe)") == -1)
190 if (file_printf(ms
, "door") == -1)
196 if ((nch
= readlink(fn
, buf
, BUFSIZ
-1)) <= 0) {
197 if (ms
->flags
& MAGIC_ERROR
) {
198 file_error(ms
, errno
, "unreadable symlink `%s'",
203 "unreadable symlink `%s' (%s)", fn
,
204 strerror(errno
)) == -1)
208 buf
[nch
] = '\0'; /* readlink(2) forgets this */
210 /* If broken symlink, say so and quit early. */
212 if (stat(buf
, &tstatbuf
) < 0) {
213 if (ms
->flags
& MAGIC_ERROR
) {
214 file_error(ms
, errno
,
215 "broken symbolic link to `%s'", buf
);
218 if (file_printf(ms
, "broken symbolic link to `%s'",
226 char buf2
[BUFSIZ
+BUFSIZ
+4];
228 if ((tmp
= strrchr(fn
, '/')) == NULL
) {
229 tmp
= buf
; /* in current directory anyway */
231 if (tmp
- fn
+ 1 > BUFSIZ
) {
232 if (ms
->flags
& MAGIC_ERROR
) {
234 "path too long: `%s'", buf
);
238 "path too long: `%s'", fn
) == -1)
242 (void)strcpy(buf2
, fn
); /* take dir part */
243 buf2
[tmp
- fn
+ 1] = '\0';
244 (void)strcat(buf2
, buf
); /* plus (rel) link */
247 if (stat(tmp
, &tstatbuf
) < 0) {
248 if (ms
->flags
& MAGIC_ERROR
) {
249 file_error(ms
, errno
,
250 "broken symbolic link to `%s'",
255 "broken symbolic link to `%s'", buf
) == -1)
261 /* Otherwise, handle it. */
262 if ((ms
->flags
& MAGIC_SYMLINK
) != 0) {
264 ms
->flags
&= MAGIC_SYMLINK
;
265 p
= magic_file(ms
, buf
);
266 ms
->flags
|= MAGIC_SYMLINK
;
267 return p
!= NULL
? 1 : -1;
268 } else { /* just print what it points to */
269 if (file_printf(ms
, "symbolic link to `%s'",
278 if (file_printf(ms
, "socket") == -1)
286 file_error(ms
, 0, "invalid mode 0%o", sb
->st_mode
);
292 * regular file, check next possibility
294 * If stat() tells us the file has zero length, report here that
295 * the file is empty, so we can skip all the work of opening and
297 * But if the -s option has been given, we skip this optimization,
298 * since on some systems, stat() reports zero size for raw disk
299 * partitions. (If the block special device really has zero length,
300 * the fact that it is empty will be detected and reported correctly
301 * when we read the file.)
303 if ((ms
->flags
& MAGIC_DEVICES
) == 0 && sb
->st_size
== 0) {
304 if (file_printf(ms
, (ms
->flags
& MAGIC_MIME
) ?
305 "application/x-empty" : "empty") == -1)