386BSD 0.1 development
[unix-history] / usr / src / usr.bin / file / fsmagic.c
CommitLineData
5a5d6702
WJ
1/*
2 * fsmagic - magic based on filesystem info - directory, special files, etc.
3 *
4 * Copyright (c) Ian F. Darwin, 1987.
5 * Written by Ian F. Darwin.
6 *
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.
9 *
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:
13 *
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.
16 *
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.
20 *
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.
24 *
25 * 4. This notice may not be removed or altered.
26 */
27
28#include <stdio.h>
29#include <sys/types.h>
30#ifndef major /* if `major' not defined in types.h, */
31#include <sys/sysmacros.h> /* try this one. */
32#endif
33#ifndef major /* still not defined? give up, manual intervention needed */
34 /* If cc tries to compile this, read and act on it. */
35 /* On most systems cpp will discard it automatically */
36 Congratulations, you have found a portability bug.
37 Please grep /usr/include/sys and edit the above #include
38 to point at the file that defines the major macro.
39#endif /*major*/
40#include <sys/stat.h>
41#include "file.h"
42
43#ifndef lint
44static char *moduleid =
45 "@(#)$Header: fsmagic.c,v 1.8 88/01/15 12:13:52 ian Exp $";
46#endif /* lint */
47
48extern char *progname;
49extern char *ckfmsg, *magicfile;
50extern int debug;
51extern FILE *efopen();
52
53fsmagic(fn)
54char *fn;
55{
56 extern struct stat statbuf;
57
58 /*
59 * Fstat is cheaper but fails for files you don't have read perms on.
60 * On 4.2BSD and similar systems, use lstat() so identify symlinks.
61 */
62#ifdef S_IFLNK
63 if (lstat(fn, &statbuf) <0)
64#else
65 if (stat(fn, &statbuf) <0)
66#endif
67 {
68 warning("can't stat", "");
69 return -1;
70 }
71
72 if (statbuf.st_mode & S_ISUID) ckfputs("setuid ", stdout);
73 if (statbuf.st_mode & S_ISGID) ckfputs("setgid ", stdout);
74 if (statbuf.st_mode & S_ISVTX) ckfputs("sticky ", stdout);
75
76 switch (statbuf.st_mode & S_IFMT) {
77 case S_IFDIR:
78 ckfputs("directory", stdout);
79 return 1;
80 case S_IFCHR:
81 (void) printf("character special (%d/%d)",
82 major(statbuf.st_rdev), minor(statbuf.st_rdev));
83 return 1;
84 case S_IFBLK:
85 (void) printf("block special (%d/%d)",
86 major(statbuf.st_rdev), minor(statbuf.st_rdev));
87 return 1;
88 /* TODO add code to handle V7 MUX and Blit MUX files */
89#ifdef S_IFIFO
90 case S_IFIFO:
91 ckfputs("fifo (named pipe)", stdout);
92 return 1;
93#endif
94#ifdef S_IFLNK
95 case S_IFLNK:
96 ckfputs("symbolic link", stdout);
97 return 1;
98#endif
99#ifdef S_IFSOCK
100 case S_IFSOCK:
101 ckfputs("socket", stdout);
102 return 1;
103#endif
104 case S_IFREG:
105 break;
106 default:
107 warning("invalid st_mode %d in statbuf!", statbuf.st_mode);
108 }
109
110 /*
111 * regular file, check next possibility
112 */
113 if (statbuf.st_size == 0) {
114 ckfputs("empty", stdout);
115 return 1;
116 }
117 return 0;
118}
119