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.
29 static const char rcsid[] =
48 * Fstat is cheaper but fails for files you don't have read perms on.
49 * On 4.2BSD and similar systems, use lstat() to identify symlinks.
56 ret = stat(fn, sb); /* don't merge into if; see "ret =" above */
60 /* Yes, I do mean stdout. */
61 /* No \n, caller will provide. */
62 "can't stat `%s': %s.", fn, strerror(errno));
66 if (sb->st_mode & S_ISUID) ckfputs("setuid ", stdout);
67 if (sb->st_mode & S_ISGID) ckfputs("setgid ", stdout);
68 if (sb->st_mode & S_ISVTX) ckfputs("sticky ", stdout);
70 switch (sb->st_mode & S_IFMT) {
72 ckfputs("directory", stdout);
75 (void) printf("character special (%ld/%ld)",
76 (long) major(sb->st_rdev), (long) minor(sb->st_rdev));
79 (void) printf("block special (%ld/%ld)",
80 (long) major(sb->st_rdev), (long) minor(sb->st_rdev));
82 /* TODO add code to handle V7 MUX and Blit MUX files */
85 ckfputs("fifo (named pipe)", stdout);
95 if ((nch = readlink(fn, buf, BUFSIZ-1)) <= 0) {
96 ckfprintf(stdout, "unreadable symlink: %s.",
100 buf[nch] = '\0'; /* readlink(2) forgets this */
102 /* If broken symlink, say so and quit early. */
104 if (stat(buf, &tstatbuf) < 0) {
106 "broken symbolic link to %s", buf);
112 char buf2[BUFSIZ+BUFSIZ+4];
114 if ((tmp = strrchr(fn, '/')) == NULL) {
115 tmp = buf; /* in current directory anyway */
118 strcpy (buf2, fn); /* take directory part */
119 buf2[tmp-fn+1] = '\0';
120 strcat (buf2, buf); /* plus (relative) symlink */
123 if (stat(tmp, &tstatbuf) < 0) {
125 "broken symbolic link to %s", buf);
130 /* Otherwise, handle it. */
132 process(buf, strlen(buf));
134 } else { /* just print what it points to */
135 ckfputs("symbolic link to ", stdout);
136 ckfputs(buf, stdout);
144 ckfputs("socket", stdout);
151 err(1, "invalid mode 0%o", sb->st_mode);
156 * regular file, check next possibility
158 if (sb->st_size == 0) {
159 ckfputs("empty", stdout);