2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
24 /* $OpenBSD: fsmagic.c,v 1.3 1997/02/09 23:58:24 millert Exp $ */
27 * fsmagic - magic based on filesystem info - directory, special files, etc.
29 * Copyright (c) Ian F. Darwin, 1987.
30 * Written by Ian F. Darwin.
32 * This software is not subject to any license of the American Telephone
33 * and Telegraph Company or of the Regents of the University of California.
35 * Permission is granted to anyone to use this software for any purpose on
36 * any computer system, and to alter it and redistribute it freely, subject
37 * to the following restrictions:
39 * 1. The author is not responsible for the consequences of use of this
40 * software, no matter how awful, even if they arise from flaws in it.
42 * 2. The origin of this software must not be misrepresented, either by
43 * explicit claim or by omission. Since few users ever read sources,
44 * credits must appear in the documentation.
46 * 3. Altered versions must be plainly marked as such, and must not be
47 * misrepresented as being the original software. Since few users
48 * ever read sources, credits must appear in the documentation.
50 * 4. This notice may not be removed or altered.
55 #include <sys/types.h>
60 # if defined(__SVR4) || defined(_SVR4_SOURCE)
61 # include <sys/mkdev.h>
64 #ifndef major /* if `major' not defined in types.h, */
65 #include <sys/sysmacros.h> /* try this one. */
67 #ifndef major /* still not defined? give up, manual intervention needed */
68 /* If cc tries to compile this, read and act on it. */
69 /* On most systems cpp will discard it automatically */
70 Congratulations
, you have found a portability bug
.
71 Please grep
/usr
/include
/sys
and edit the above
#include
72 to point at the file that defines the
"major" macro
.
79 static char *moduleid
= "$OpenBSD: fsmagic.c,v 1.3 1997/02/09 23:58:24 millert Exp $";
91 * Fstat is cheaper but fails for files you don't have read perms on.
92 * On 4.2BSD and similar systems, use lstat() to identify symlinks.
99 ret
= stat(fn
, sb
); /* don't merge into if; see "ret =" above */
103 /* Yes, I do mean stdout. */
104 /* No \n, caller will provide. */
105 "can't stat `%s' (%s).", fn
, strerror(errno
));
109 if (sb
->st_mode
& S_ISUID
) ckfputs("setuid ", stdout
);
110 if (sb
->st_mode
& S_ISGID
) ckfputs("setgid ", stdout
);
111 if (sb
->st_mode
& S_ISVTX
) ckfputs("sticky ", stdout
);
113 switch (sb
->st_mode
& S_IFMT
) {
115 ckfputs("directory", stdout
);
118 (void) printf("character special (%ld/%ld)",
119 (long) major(sb
->st_rdev
), (long) minor(sb
->st_rdev
));
122 (void) printf("block special (%ld/%ld)",
123 (long) major(sb
->st_rdev
), (long) minor(sb
->st_rdev
));
125 /* TODO add code to handle V7 MUX and Blit MUX files */
128 ckfputs("fifo (named pipe)", stdout
);
136 struct stat tstatbuf
;
138 if ((nch
= readlink(fn
, buf
, BUFSIZ
-1)) <= 0) {
139 ckfprintf(stdout
, "unreadable symlink (%s).",
143 buf
[nch
] = '\0'; /* readlink(2) forgets this */
145 /* If broken symlink, say so and quit early. */
147 if (stat(buf
, &tstatbuf
) < 0) {
149 "broken symbolic link to %s", buf
);
155 char buf2
[BUFSIZ
+BUFSIZ
+4];
157 if ((tmp
= strrchr(fn
, '/')) == NULL
) {
158 tmp
= buf
; /* in current directory anyway */
161 strcpy (buf2
, fn
); /* take directory part */
162 buf2
[tmp
-fn
+1] = '\0';
163 strcat (buf2
, buf
); /* plus (relative) symlink */
166 if (stat(tmp
, &tstatbuf
) < 0) {
168 "broken symbolic link to %s", buf
);
173 /* Otherwise, handle it. */
175 process(buf
, strlen(buf
));
177 } else { /* just print what it points to */
178 ckfputs("symbolic link to ", stdout
);
179 ckfputs(buf
, stdout
);
187 ckfputs("socket", stdout
);
194 error("invalid mode 0%o.\n", sb
->st_mode
);
199 * regular file, check next possibility
201 if (sb
->st_size
== 0) {
202 ckfputs("empty", stdout
);