2 * fsmagic - magic based on filesystem info - directory, special files, etc.
4 * Copyright (c) Ian F. Darwin, 1987.
5 * Written by Ian F. Darwin.
7 * This software is not subject to any license of the American Telephone
8 * and Telegraph Company or of the Regents of the University of California.
10 * Permission is granted to anyone to use this software for any purpose on
11 * any computer system, and to alter it and redistribute it freely, subject
12 * to the following restrictions:
14 * 1. The author is not responsible for the consequences of use of this
15 * software, no matter how awful, even if they arise from flaws in it.
17 * 2. The origin of this software must not be misrepresented, either by
18 * explicit claim or by omission. Since few users ever read sources,
19 * credits must appear in the documentation.
21 * 3. Altered versions must be plainly marked as such, and must not be
22 * misrepresented as being the original software. Since few users
23 * ever read sources, credits must appear in the documentation.
25 * 4. This notice may not be removed or altered.
34 /* Since major is a function on SVR4, we can't use `ifndef major'. */
36 # include <sys/mkdev.h>
39 #ifdef MAJOR_IN_SYSMACROS
40 # include <sys/sysmacros.h>
43 #ifdef major /* Might be defined in sys/types.h. */
48 # define major(dev) (((dev) >> 8) & 0xff)
49 # define minor(dev) ((dev) & 0xff)
54 FILE_RCSID("@(#)$Id: fsmagic.c,v 1.36 2002/07/03 19:00:41 christos Exp $")
58 fsmagic(const char *fn, struct stat *sb)
63 * Fstat is cheaper but fails for files you don't have read perms on.
64 * On 4.2BSD and similar systems, use lstat() to identify symlinks.
71 ret = stat(fn, sb); /* don't merge into if; see "ret =" above */
75 /* Yes, I do mean stdout. */
76 /* No \n, caller will provide. */
77 "can't stat `%s' (%s).", fn, strerror(errno));
82 if ((sb->st_mode & S_IFMT) != S_IFREG) {
83 ckfputs("application/x-not-regular-file", stdout);
89 if (sb->st_mode & S_ISUID) ckfputs("setuid ", stdout);
92 if (sb->st_mode & S_ISGID) ckfputs("setgid ", stdout);
95 if (sb->st_mode & S_ISVTX) ckfputs("sticky ", stdout);
99 switch (sb->st_mode & S_IFMT) {
101 ckfputs("directory", stdout);
106 * If -s has been specified, treat character special files
107 * like ordinary files. Otherwise, just report that they
108 * are block special files and go on to the next file.
114 (void) printf("character special (%d/%d/%d)",
116 dv_unit(sb->st_rdev),
117 dv_subunit(sb->st_rdev));
119 (void) printf("character special (%ld/%ld)",
120 (long) major(sb->st_rdev), (long) minor(sb->st_rdev));
123 (void) printf("character special");
130 * If -s has been specified, treat block special files
131 * like ordinary files. Otherwise, just report that they
132 * are block special files and go on to the next file.
138 (void) printf("block special (%d/%d/%d)",
140 dv_unit(sb->st_rdev),
141 dv_subunit(sb->st_rdev));
143 (void) printf("block special (%ld/%ld)",
144 (long) major(sb->st_rdev), (long) minor(sb->st_rdev));
147 (void) printf("block special");
151 /* TODO add code to handle V7 MUX and Blit MUX files */
154 ckfputs("fifo (named pipe)", stdout);
159 ckfputs("door", stdout);
167 struct stat tstatbuf;
169 if ((nch = readlink(fn, buf, BUFSIZ-1)) <= 0) {
170 ckfprintf(stdout, "unreadable symlink (%s).",
174 buf[nch] = '\0'; /* readlink(2) forgets this */
176 /* If broken symlink, say so and quit early. */
178 if (stat(buf, &tstatbuf) < 0) {
180 "broken symbolic link to %s", buf);
186 char buf2[BUFSIZ+BUFSIZ+4];
188 if ((tmp = strrchr(fn, '/')) == NULL) {
189 tmp = buf; /* in current directory anyway */
192 strcpy (buf2, fn); /* take directory part */
193 buf2[tmp-fn+1] = '\0';
194 strcat (buf2, buf); /* plus (relative) symlink */
197 if (stat(tmp, &tstatbuf) < 0) {
199 "broken symbolic link to %s", buf);
204 /* Otherwise, handle it. */
206 process(buf, strlen(buf));
208 } else { /* just print what it points to */
209 ckfputs("symbolic link to ", stdout);
210 ckfputs(buf, stdout);
218 ckfputs("socket", stdout);
225 error("invalid mode 0%o.\n", sb->st_mode);
230 * regular file, check next possibility
232 * If stat() tells us the file has zero length, report here that
233 * the file is empty, so we can skip all the work of opening and
235 * But if the -s option has been given, we skip this optimization,
236 * since on some systems, stat() reports zero size for raw disk
237 * partitions. (If the block special device really has zero length,
238 * the fact that it is empty will be detected and reported correctly
239 * when we read the file.)
241 if (!sflag && sb->st_size == 0) {
242 ckfputs(iflag ? "application/x-empty" : "empty", stdout);