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.
31 #include <sys/types.h>
37 /* Since major is a function on SVR4, we can't use `ifndef major'. */
39 # include <sys/mkdev.h>
42 #ifdef MAJOR_IN_SYSMACROS
43 # include <sys/sysmacros.h>
46 #ifdef major /* Might be defined in sys/types.h. */
51 # define major(dev) (((dev) >> 8) & 0xff)
52 # define minor(dev) ((dev) & 0xff)
57 FILE_RCSID("@(#)$Id: fsmagic.c,v 1.33 2000/08/05 17:36:48 christos Exp $")
68 * Fstat is cheaper but fails for files you don't have read perms on.
69 * On 4.2BSD and similar systems, use lstat() to identify symlinks.
76 ret = stat(fn, sb); /* don't merge into if; see "ret =" above */
80 /* Yes, I do mean stdout. */
81 /* No \n, caller will provide. */
82 "can't stat `%s' (%s).", fn, strerror(errno));
87 if ((sb->st_mode & S_IFMT) != S_IFREG) {
88 ckfputs("application/x-not-regular-file", stdout);
94 if (sb->st_mode & S_ISUID) ckfputs("setuid ", stdout);
97 if (sb->st_mode & S_ISGID) ckfputs("setgid ", stdout);
100 if (sb->st_mode & S_ISVTX) ckfputs("sticky ", stdout);
104 switch (sb->st_mode & S_IFMT) {
106 ckfputs("directory", stdout);
111 * If -s has been specified, treat character special files
112 * like ordinary files. Otherwise, just report that they
113 * are block special files and go on to the next file.
119 (void) printf("character special (%d/%d/%d)",
121 dv_unit(sb->st_rdev),
122 dv_subunit(sb->st_rdev));
124 (void) printf("character special (%ld/%ld)",
125 (long) major(sb->st_rdev), (long) minor(sb->st_rdev));
128 (void) printf("character special");
135 * If -s has been specified, treat block special files
136 * like ordinary files. Otherwise, just report that they
137 * are block special files and go on to the next file.
143 (void) printf("block special (%d/%d/%d)",
145 dv_unit(sb->st_rdev),
146 dv_subunit(sb->st_rdev));
148 (void) printf("block special (%ld/%ld)",
149 (long) major(sb->st_rdev), (long) minor(sb->st_rdev));
152 (void) printf("block special");
156 /* TODO add code to handle V7 MUX and Blit MUX files */
159 ckfputs("fifo (named pipe)", stdout);
164 ckfputs("door", stdout);
172 struct stat tstatbuf;
174 if ((nch = readlink(fn, buf, BUFSIZ-1)) <= 0) {
175 ckfprintf(stdout, "unreadable symlink (%s).",
179 buf[nch] = '\0'; /* readlink(2) forgets this */
181 /* If broken symlink, say so and quit early. */
183 if (stat(buf, &tstatbuf) < 0) {
185 "broken symbolic link to %s", buf);
191 char buf2[BUFSIZ+BUFSIZ+4];
193 if ((tmp = strrchr(fn, '/')) == NULL) {
194 tmp = buf; /* in current directory anyway */
197 strcpy (buf2, fn); /* take directory part */
198 buf2[tmp-fn+1] = '\0';
199 strcat (buf2, buf); /* plus (relative) symlink */
202 if (stat(tmp, &tstatbuf) < 0) {
204 "broken symbolic link to %s", buf);
209 /* Otherwise, handle it. */
211 process(buf, strlen(buf));
213 } else { /* just print what it points to */
214 ckfputs("symbolic link to ", stdout);
215 ckfputs(buf, stdout);
223 ckfputs("socket", stdout);
230 error("invalid mode 0%o.\n", sb->st_mode);
235 * regular file, check next possibility
237 * If stat() tells us the file has zero length, report here that
238 * the file is empty, so we can skip all the work of opening and
240 * But if the -s option has been given, we skip this optimization,
241 * since on some systems, stat() reports zero size for raw disk
242 * partitions. (If the block special device really has zero length,
243 * the fact that it is empty will be detected and reported correctly
244 * when we read the file.)
246 if (!sflag && sb->st_size == 0) {
247 ckfputs(iflag ? "application/x-empty" : "empty", stdout);