xref: /netbsd-src/external/bsd/file/dist/src/fsmagic.c (revision ddb176824c39fb0db5ceef3e9e40dcaa273aec38)
1*ddb17682Schristos /*	$NetBSD: fsmagic.c,v 1.18 2023/08/18 19:00:11 christos Exp $	*/
2fa9ee498Schristos 
31b108b8bSchristos /*
41b108b8bSchristos  * Copyright (c) Ian F. Darwin 1986-1995.
51b108b8bSchristos  * Software written by Ian F. Darwin and others;
61b108b8bSchristos  * maintained 1995-present by Christos Zoulas and others.
71b108b8bSchristos  *
81b108b8bSchristos  * Redistribution and use in source and binary forms, with or without
91b108b8bSchristos  * modification, are permitted provided that the following conditions
101b108b8bSchristos  * are met:
111b108b8bSchristos  * 1. Redistributions of source code must retain the above copyright
121b108b8bSchristos  *    notice immediately at the beginning of the file, without modification,
131b108b8bSchristos  *    this list of conditions, and the following disclaimer.
141b108b8bSchristos  * 2. Redistributions in binary form must reproduce the above copyright
151b108b8bSchristos  *    notice, this list of conditions and the following disclaimer in the
161b108b8bSchristos  *    documentation and/or other materials provided with the distribution.
171b108b8bSchristos  *
181b108b8bSchristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
191b108b8bSchristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
201b108b8bSchristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
211b108b8bSchristos  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
221b108b8bSchristos  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
231b108b8bSchristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
241b108b8bSchristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
251b108b8bSchristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
261b108b8bSchristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
271b108b8bSchristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
281b108b8bSchristos  * SUCH DAMAGE.
291b108b8bSchristos  */
301b108b8bSchristos /*
311b108b8bSchristos  * fsmagic - magic based on filesystem info - directory, special files, etc.
321b108b8bSchristos  */
331b108b8bSchristos 
341b108b8bSchristos #include "file.h"
351b108b8bSchristos 
361b108b8bSchristos #ifndef	lint
371b108b8bSchristos #if 0
38*ddb17682Schristos FILE_RCSID("@(#)$File: fsmagic.c,v 1.85 2022/12/26 17:31:14 christos Exp $")
391b108b8bSchristos #else
40*ddb17682Schristos __RCSID("$NetBSD: fsmagic.c,v 1.18 2023/08/18 19:00:11 christos Exp $");
411b108b8bSchristos #endif
421b108b8bSchristos #endif	/* lint */
431b108b8bSchristos 
441b108b8bSchristos #include "magic.h"
451b108b8bSchristos #include <string.h>
461b108b8bSchristos #ifdef HAVE_UNISTD_H
471b108b8bSchristos #include <unistd.h>
481b108b8bSchristos #endif
491b108b8bSchristos #include <stdlib.h>
501b108b8bSchristos /* Since major is a function on SVR4, we cannot use `ifndef major'.  */
511b108b8bSchristos #ifdef MAJOR_IN_MKDEV
521b108b8bSchristos # include <sys/mkdev.h>
531b108b8bSchristos # define HAVE_MAJOR
541b108b8bSchristos #endif
55d0c65b7bSchristos #ifdef HAVE_SYS_SYSMACROS_H
561b108b8bSchristos # include <sys/sysmacros.h>
57d0c65b7bSchristos #endif
58d0c65b7bSchristos #ifdef MAJOR_IN_SYSMACROS
591b108b8bSchristos # define HAVE_MAJOR
601b108b8bSchristos #endif
61d0c65b7bSchristos #if defined(major) && !defined(HAVE_MAJOR)
62d0c65b7bSchristos /* Might be defined in sys/types.h.  */
631b108b8bSchristos # define HAVE_MAJOR
641b108b8bSchristos #endif
65819e6405Schristos #ifdef WIN32
66819e6405Schristos # define WIN32_LEAN_AND_MEAN
67819e6405Schristos # include <windows.h>
68819e6405Schristos #endif
691b108b8bSchristos 
701b108b8bSchristos #ifndef HAVE_MAJOR
711b108b8bSchristos # define major(dev)  (((dev) >> 8) & 0xff)
721b108b8bSchristos # define minor(dev)  ((dev) & 0xff)
731b108b8bSchristos #endif
741b108b8bSchristos #undef HAVE_MAJOR
752344ff98Schristos #ifdef	S_IFLNK
76*ddb17682Schristos file_private int
bad_link(struct magic_set * ms,int err,char * buf)771b108b8bSchristos bad_link(struct magic_set *ms, int err, char *buf)
781b108b8bSchristos {
791b108b8bSchristos 	int mime = ms->flags & MAGIC_MIME;
801b108b8bSchristos 	if ((mime & MAGIC_MIME_TYPE) &&
8179bb278aSchristos 	    file_printf(ms, "inode/symlink")
821b108b8bSchristos 	    == -1)
831b108b8bSchristos 		return -1;
841b108b8bSchristos 	else if (!mime) {
851b108b8bSchristos 		if (ms->flags & MAGIC_ERROR) {
86dbb4627dSchristos 			file_error(ms, err,
8767990773Schristos 				   "broken symbolic link to %s", buf);
881b108b8bSchristos 			return -1;
891b108b8bSchristos 		}
9067990773Schristos 		if (file_printf(ms, "broken symbolic link to %s", buf) == -1)
911b108b8bSchristos 			return -1;
921b108b8bSchristos 	}
931b108b8bSchristos 	return 1;
941b108b8bSchristos }
952344ff98Schristos #endif
96*ddb17682Schristos file_private int
handle_mime(struct magic_set * ms,int mime,const char * str)971b108b8bSchristos handle_mime(struct magic_set *ms, int mime, const char *str)
981b108b8bSchristos {
991b108b8bSchristos 	if ((mime & MAGIC_MIME_TYPE)) {
10079bb278aSchristos 		if (file_printf(ms, "inode/%s", str) == -1)
1011b108b8bSchristos 			return -1;
1021b108b8bSchristos 		if ((mime & MAGIC_MIME_ENCODING) && file_printf(ms,
1031b108b8bSchristos 		    "; charset=") == -1)
1041b108b8bSchristos 			return -1;
1051b108b8bSchristos 	}
1061b108b8bSchristos 	if ((mime & MAGIC_MIME_ENCODING) && file_printf(ms, "binary") == -1)
1071b108b8bSchristos 		return -1;
1081b108b8bSchristos 	return 0;
1091b108b8bSchristos }
1101b108b8bSchristos 
111*ddb17682Schristos file_protected int
file_fsmagic(struct magic_set * ms,const char * fn,struct stat * sb)1121b108b8bSchristos file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb)
1131b108b8bSchristos {
114c2e19894Schristos 	int ret, did = 0;
1151b108b8bSchristos 	int mime = ms->flags & MAGIC_MIME;
116051bdd9aSchristos 	int silent = ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION);
1171b108b8bSchristos #ifdef	S_IFLNK
1181b108b8bSchristos 	char buf[BUFSIZ+4];
1197c25ef23Schristos 	ssize_t nch;
1201b108b8bSchristos 	struct stat tstatbuf;
1211b108b8bSchristos #endif
1221b108b8bSchristos 
1231b108b8bSchristos 	if (fn == NULL)
1241b108b8bSchristos 		return 0;
1251b108b8bSchristos 
12620d96732Schristos #define COMMA	(did++ ? ", " : "")
1271b108b8bSchristos 	/*
1281b108b8bSchristos 	 * Fstat is cheaper but fails for files you don't have read perms on.
1291b108b8bSchristos 	 * On 4.2BSD and similar systems, use lstat() to identify symlinks.
1301b108b8bSchristos 	 */
1311b108b8bSchristos #ifdef	S_IFLNK
1321b108b8bSchristos 	if ((ms->flags & MAGIC_SYMLINK) == 0)
1331b108b8bSchristos 		ret = lstat(fn, sb);
1341b108b8bSchristos 	else
1351b108b8bSchristos #endif
1361b108b8bSchristos 	ret = stat(fn, sb);	/* don't merge into if; see "ret =" above */
1371b108b8bSchristos 
138819e6405Schristos #ifdef WIN32
139819e6405Schristos 	{
140fa9ee498Schristos 		HANDLE hFile = CreateFile((LPCSTR)fn, 0, FILE_SHARE_DELETE |
141819e6405Schristos 		    FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0,
142819e6405Schristos 		    NULL);
143819e6405Schristos 		if (hFile != INVALID_HANDLE_VALUE) {
144819e6405Schristos 			/*
145819e6405Schristos 			 * Stat failed, but we can still open it - assume it's
146819e6405Schristos 			 * a block device, if nothing else.
147819e6405Schristos 			 */
148819e6405Schristos 			if (ret) {
149819e6405Schristos 				sb->st_mode = S_IFBLK;
150819e6405Schristos 				ret = 0;
151819e6405Schristos 			}
152819e6405Schristos 			switch (GetFileType(hFile)) {
153819e6405Schristos 			case FILE_TYPE_CHAR:
154819e6405Schristos 				sb->st_mode |= S_IFCHR;
155819e6405Schristos 				sb->st_mode &= ~S_IFREG;
156819e6405Schristos 				break;
157819e6405Schristos 			case FILE_TYPE_PIPE:
158819e6405Schristos 				sb->st_mode |= S_IFIFO;
159819e6405Schristos 				sb->st_mode &= ~S_IFREG;
160819e6405Schristos 				break;
161819e6405Schristos 			}
162819e6405Schristos 			CloseHandle(hFile);
163819e6405Schristos 		}
164819e6405Schristos 	}
165819e6405Schristos #endif
166819e6405Schristos 
1671b108b8bSchristos 	if (ret) {
1681b108b8bSchristos 		if (ms->flags & MAGIC_ERROR) {
1691b108b8bSchristos 			file_error(ms, errno, "cannot stat `%s'", fn);
1701b108b8bSchristos 			return -1;
1711b108b8bSchristos 		}
1721b108b8bSchristos 		if (file_printf(ms, "cannot open `%s' (%s)",
1731b108b8bSchristos 		    fn, strerror(errno)) == -1)
1741b108b8bSchristos 			return -1;
1758dd459ccSchristos 		return 0;
1761b108b8bSchristos 	}
1771b108b8bSchristos 
178c2e19894Schristos 	ret = 1;
179051bdd9aSchristos 	if (!mime && !silent) {
1801b108b8bSchristos #ifdef S_ISUID
1811b108b8bSchristos 		if (sb->st_mode & S_ISUID)
18220d96732Schristos 			if (file_printf(ms, "%ssetuid", COMMA) == -1)
1831b108b8bSchristos 				return -1;
1841b108b8bSchristos #endif
1851b108b8bSchristos #ifdef S_ISGID
1861b108b8bSchristos 		if (sb->st_mode & S_ISGID)
18720d96732Schristos 			if (file_printf(ms, "%ssetgid", COMMA) == -1)
1881b108b8bSchristos 				return -1;
1891b108b8bSchristos #endif
1901b108b8bSchristos #ifdef S_ISVTX
1911b108b8bSchristos 		if (sb->st_mode & S_ISVTX)
19220d96732Schristos 			if (file_printf(ms, "%ssticky", COMMA) == -1)
1931b108b8bSchristos 				return -1;
1941b108b8bSchristos #endif
1951b108b8bSchristos 	}
1961b108b8bSchristos 
1971b108b8bSchristos 	switch (sb->st_mode & S_IFMT) {
1981b108b8bSchristos 	case S_IFDIR:
1991b108b8bSchristos 		if (mime) {
20079bb278aSchristos 			if (handle_mime(ms, mime, "directory") == -1)
2011b108b8bSchristos 				return -1;
202051bdd9aSchristos 		} else if (silent) {
20320d96732Schristos 		} else if (file_printf(ms, "%sdirectory", COMMA) == -1)
2041b108b8bSchristos 			return -1;
205c2e19894Schristos 		break;
2061b108b8bSchristos #ifdef S_IFCHR
2071b108b8bSchristos 	case S_IFCHR:
2081b108b8bSchristos 		/*
2091b108b8bSchristos 		 * If -s has been specified, treat character special files
2101b108b8bSchristos 		 * like ordinary files.  Otherwise, just report that they
2111b108b8bSchristos 		 * are block special files and go on to the next file.
2121b108b8bSchristos 		 */
213c2e19894Schristos 		if ((ms->flags & MAGIC_DEVICES) != 0) {
214c2e19894Schristos 			ret = 0;
2151b108b8bSchristos 			break;
216c2e19894Schristos 		}
2171b108b8bSchristos 		if (mime) {
21879bb278aSchristos 			if (handle_mime(ms, mime, "chardevice") == -1)
2191b108b8bSchristos 				return -1;
220051bdd9aSchristos 		} else if (silent) {
2211b108b8bSchristos 		} else {
222819e6405Schristos #ifdef HAVE_STRUCT_STAT_ST_RDEV
2231b108b8bSchristos # ifdef dv_unit
22420d96732Schristos 			if (file_printf(ms, "%scharacter special (%d/%d/%d)",
22520d96732Schristos 			    COMMA, major(sb->st_rdev), dv_unit(sb->st_rdev),
2261b108b8bSchristos 					dv_subunit(sb->st_rdev)) == -1)
2271b108b8bSchristos 				return -1;
2281b108b8bSchristos # else
22920d96732Schristos 			if (file_printf(ms, "%scharacter special (%ld/%ld)",
23020d96732Schristos 			    COMMA, (long)major(sb->st_rdev),
23120d96732Schristos 			    (long)minor(sb->st_rdev)) == -1)
2321b108b8bSchristos 				return -1;
2331b108b8bSchristos # endif
2341b108b8bSchristos #else
23520d96732Schristos 			if (file_printf(ms, "%scharacter special", COMMA) == -1)
2361b108b8bSchristos 				return -1;
2371b108b8bSchristos #endif
2381b108b8bSchristos 		}
239c2e19894Schristos 		break;
2401b108b8bSchristos #endif
2411b108b8bSchristos #ifdef S_IFBLK
2421b108b8bSchristos 	case S_IFBLK:
2431b108b8bSchristos 		/*
2441b108b8bSchristos 		 * If -s has been specified, treat block special files
2451b108b8bSchristos 		 * like ordinary files.  Otherwise, just report that they
2461b108b8bSchristos 		 * are block special files and go on to the next file.
2471b108b8bSchristos 		 */
248c2e19894Schristos 		if ((ms->flags & MAGIC_DEVICES) != 0) {
249c2e19894Schristos 			ret = 0;
2501b108b8bSchristos 			break;
251c2e19894Schristos 		}
2521b108b8bSchristos 		if (mime) {
25379bb278aSchristos 			if (handle_mime(ms, mime, "blockdevice") == -1)
2541b108b8bSchristos 				return -1;
255051bdd9aSchristos 		} else if (silent) {
2561b108b8bSchristos 		} else {
257819e6405Schristos #ifdef HAVE_STRUCT_STAT_ST_RDEV
2581b108b8bSchristos # ifdef dv_unit
25920d96732Schristos 			if (file_printf(ms, "%sblock special (%d/%d/%d)",
26020d96732Schristos 			    COMMA, major(sb->st_rdev), dv_unit(sb->st_rdev),
2611b108b8bSchristos 			    dv_subunit(sb->st_rdev)) == -1)
2621b108b8bSchristos 				return -1;
2631b108b8bSchristos # else
26420d96732Schristos 			if (file_printf(ms, "%sblock special (%ld/%ld)",
26520d96732Schristos 			    COMMA, (long)major(sb->st_rdev),
26620d96732Schristos 			    (long)minor(sb->st_rdev)) == -1)
2671b108b8bSchristos 				return -1;
2681b108b8bSchristos # endif
2691b108b8bSchristos #else
27020d96732Schristos 			if (file_printf(ms, "%sblock special", COMMA) == -1)
2711b108b8bSchristos 				return -1;
2721b108b8bSchristos #endif
2731b108b8bSchristos 		}
274c2e19894Schristos 		break;
2751b108b8bSchristos #endif
2761b108b8bSchristos 	/* TODO add code to handle V7 MUX and Blit MUX files */
2771b108b8bSchristos #ifdef	S_IFIFO
2781b108b8bSchristos 	case S_IFIFO:
2791b108b8bSchristos 		if((ms->flags & MAGIC_DEVICES) != 0)
2801b108b8bSchristos 			break;
2811b108b8bSchristos 		if (mime) {
28279bb278aSchristos 			if (handle_mime(ms, mime, "fifo") == -1)
2831b108b8bSchristos 				return -1;
284051bdd9aSchristos 		} else if (silent) {
28520d96732Schristos 		} else if (file_printf(ms, "%sfifo (named pipe)", COMMA) == -1)
2861b108b8bSchristos 			return -1;
287c2e19894Schristos 		break;
2881b108b8bSchristos #endif
2891b108b8bSchristos #ifdef	S_IFDOOR
2901b108b8bSchristos 	case S_IFDOOR:
2911b108b8bSchristos 		if (mime) {
29279bb278aSchristos 			if (handle_mime(ms, mime, "door") == -1)
2931b108b8bSchristos 				return -1;
294051bdd9aSchristos 		} else if (silent) {
29520d96732Schristos 		} else if (file_printf(ms, "%sdoor", COMMA) == -1)
2961b108b8bSchristos 			return -1;
297c2e19894Schristos 		break;
2981b108b8bSchristos #endif
2991b108b8bSchristos #ifdef	S_IFLNK
3001b108b8bSchristos 	case S_IFLNK:
3011b108b8bSchristos 		if ((nch = readlink(fn, buf, BUFSIZ-1)) <= 0) {
3021b108b8bSchristos 			if (ms->flags & MAGIC_ERROR) {
3031b108b8bSchristos 			    file_error(ms, errno, "unreadable symlink `%s'",
3041b108b8bSchristos 				fn);
3051b108b8bSchristos 			    return -1;
3061b108b8bSchristos 			}
3071b108b8bSchristos 			if (mime) {
30879bb278aSchristos 				if (handle_mime(ms, mime, "symlink") == -1)
3091b108b8bSchristos 					return -1;
310051bdd9aSchristos 			} else if (silent) {
3111b108b8bSchristos 			} else if (file_printf(ms,
31220d96732Schristos 			    "%sunreadable symlink `%s' (%s)", COMMA, fn,
3131b108b8bSchristos 			    strerror(errno)) == -1)
3141b108b8bSchristos 				return -1;
315c2e19894Schristos 			break;
3161b108b8bSchristos 		}
3171b108b8bSchristos 		buf[nch] = '\0';	/* readlink(2) does not do this */
3181b108b8bSchristos 
3191b108b8bSchristos 		/* If broken symlink, say so and quit early. */
320c02f7f97Schristos #ifdef __linux__
321c02f7f97Schristos 		/*
322c02f7f97Schristos 		 * linux procfs/devfs makes symlinks like pipe:[3515864880]
323c02f7f97Schristos 		 * that we can't stat their readlink output, so stat the
324c02f7f97Schristos 		 * original filename instead.
325c02f7f97Schristos 		 */
326c02f7f97Schristos 		if (stat(fn, &tstatbuf) < 0)
327c02f7f97Schristos 			return bad_link(ms, errno, buf);
328c02f7f97Schristos #else
3291b108b8bSchristos 		if (*buf == '/') {
3301b108b8bSchristos 			if (stat(buf, &tstatbuf) < 0)
3311b108b8bSchristos 				return bad_link(ms, errno, buf);
3321b108b8bSchristos 		} else {
3331b108b8bSchristos 			char *tmp;
3341b108b8bSchristos 			char buf2[BUFSIZ+BUFSIZ+4];
3351b108b8bSchristos 
3361d4cb158Schristos 			if ((tmp = CCAST(char *, strrchr(fn,  '/'))) == NULL) {
3371b108b8bSchristos 				tmp = buf; /* in current directory anyway */
3381b108b8bSchristos 			} else {
3391b108b8bSchristos 				if (tmp - fn + 1 > BUFSIZ) {
3401b108b8bSchristos 					if (ms->flags & MAGIC_ERROR) {
3411b108b8bSchristos 						file_error(ms, 0,
3421b108b8bSchristos 						    "path too long: `%s'", buf);
3431b108b8bSchristos 						return -1;
3441b108b8bSchristos 					}
3451b108b8bSchristos 					if (mime) {
3461b108b8bSchristos 						if (handle_mime(ms, mime,
3471b108b8bSchristos 						    "x-path-too-long") == -1)
3481b108b8bSchristos 							return -1;
349051bdd9aSchristos 					} else if (silent) {
3501b108b8bSchristos 					} else if (file_printf(ms,
35120d96732Schristos 					    "%spath too long: `%s'", COMMA,
35220d96732Schristos 					    fn) == -1)
3531b108b8bSchristos 						return -1;
354c2e19894Schristos 					break;
3551b108b8bSchristos 				}
3561b108b8bSchristos 				/* take dir part */
3571b108b8bSchristos 				(void)strlcpy(buf2, fn, sizeof buf2);
3581b108b8bSchristos 				buf2[tmp - fn + 1] = '\0';
3591b108b8bSchristos 				/* plus (rel) link */
3601b108b8bSchristos 				(void)strlcat(buf2, buf, sizeof buf2);
3611b108b8bSchristos 				tmp = buf2;
3621b108b8bSchristos 			}
3631b108b8bSchristos 			if (stat(tmp, &tstatbuf) < 0)
3641b108b8bSchristos 				return bad_link(ms, errno, buf);
3651b108b8bSchristos 		}
366c02f7f97Schristos #endif
3671b108b8bSchristos 
3681b108b8bSchristos 		/* Otherwise, handle it. */
3691b108b8bSchristos 		if ((ms->flags & MAGIC_SYMLINK) != 0) {
3701b108b8bSchristos 			const char *p;
3711b108b8bSchristos 			ms->flags &= MAGIC_SYMLINK;
3721b108b8bSchristos 			p = magic_file(ms, buf);
3731b108b8bSchristos 			ms->flags |= MAGIC_SYMLINK;
374c2e19894Schristos 			if (p == NULL)
375c2e19894Schristos 				return -1;
3761b108b8bSchristos 		} else { /* just print what it points to */
3771b108b8bSchristos 			if (mime) {
37879bb278aSchristos 				if (handle_mime(ms, mime, "symlink") == -1)
3791b108b8bSchristos 					return -1;
380051bdd9aSchristos 			} else if (silent) {
38167990773Schristos 			} else if (file_printf(ms, "%ssymbolic link to %s",
38220d96732Schristos 			    COMMA, buf) == -1)
3831b108b8bSchristos 				return -1;
3841b108b8bSchristos 		}
385c2e19894Schristos 		break;
3861b108b8bSchristos #endif
3871b108b8bSchristos #ifdef	S_IFSOCK
3881b108b8bSchristos #ifndef __COHERENT__
3891b108b8bSchristos 	case S_IFSOCK:
3901b108b8bSchristos 		if (mime) {
39179bb278aSchristos 			if (handle_mime(ms, mime, "socket") == -1)
3921b108b8bSchristos 				return -1;
393051bdd9aSchristos 		} else if (silent) {
39420d96732Schristos 		} else if (file_printf(ms, "%ssocket", COMMA) == -1)
3951b108b8bSchristos 			return -1;
396c2e19894Schristos 		break;
3971b108b8bSchristos #endif
3981b108b8bSchristos #endif
3991b108b8bSchristos 	case S_IFREG:
4001b108b8bSchristos 		/*
4011b108b8bSchristos 		 * regular file, check next possibility
4021b108b8bSchristos 		 *
4031b108b8bSchristos 		 * If stat() tells us the file has zero length, report here that
4041b108b8bSchristos 		 * the file is empty, so we can skip all the work of opening and
4051b108b8bSchristos 		 * reading the file.
406c2e19894Schristos 		 * But if the -s option has been given, we skip this
407c2e19894Schristos 		 * optimization, since on some systems, stat() reports zero
408c2e19894Schristos 		 * size for raw disk partitions. (If the block special device
409c2e19894Schristos 		 * really has zero length, the fact that it is empty will be
410c2e19894Schristos 		 * detected and reported correctly when we read the file.)
4111b108b8bSchristos 		 */
4121b108b8bSchristos 		if ((ms->flags & MAGIC_DEVICES) == 0 && sb->st_size == 0) {
4131b108b8bSchristos 			if (mime) {
4141b108b8bSchristos 				if (handle_mime(ms, mime, "x-empty") == -1)
4151b108b8bSchristos 					return -1;
416051bdd9aSchristos 			} else if (silent) {
41720d96732Schristos 			} else if (file_printf(ms, "%sempty", COMMA) == -1)
4181b108b8bSchristos 				return -1;
419c2e19894Schristos 			break;
4201b108b8bSchristos 		}
421c2e19894Schristos 		ret = 0;
422c2e19894Schristos 		break;
423c2e19894Schristos 
424c2e19894Schristos 	default:
425c2e19894Schristos 		file_error(ms, 0, "invalid mode 0%o", sb->st_mode);
426c2e19894Schristos 		return -1;
427c2e19894Schristos 		/*NOTREACHED*/
428c2e19894Schristos 	}
429c2e19894Schristos 
430051bdd9aSchristos 	if (!silent && !mime && did && ret == 0) {
431c2e19894Schristos 	    if (file_printf(ms, " ") == -1)
432c2e19894Schristos 		    return -1;
433c2e19894Schristos 	}
43478a23c3aSchristos 	/*
43578a23c3aSchristos 	 * If we were looking for extensions or apple (silent) it is not our
43678a23c3aSchristos 	 * job to print here, so don't count this as a match.
43778a23c3aSchristos 	 */
43878a23c3aSchristos 	if (ret == 1 && silent)
43978a23c3aSchristos 		return 0;
440c2e19894Schristos 	return ret;
4411b108b8bSchristos }
442