]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/file/fsmagic.c
This commit was generated by cvs2svn to compensate for changes in r56101,
[FreeBSD/FreeBSD.git] / usr.bin / file / fsmagic.c
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 #ifndef lint
29 static const char rcsid[] =
30   "$FreeBSD$";
31 #endif /* not lint */
32
33 #include <err.h>
34 #include <string.h>
35 #include <sys/stat.h>
36 #include <unistd.h>
37
38 #include "file.h"
39
40 int
41 fsmagic(fn, sb)
42 const char *fn;
43 struct stat *sb;
44 {
45         int ret = 0;
46
47         /*
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.
50          */
51 #ifdef  S_IFLNK
52         if (!lflag)
53                 ret = lstat(fn, sb);
54         else
55 #endif
56         ret = stat(fn, sb);     /* don't merge into if; see "ret =" above */
57
58         if (ret) {
59                 ckfprintf(stdout,
60                         /* Yes, I do mean stdout. */
61                         /* No \n, caller will provide. */
62                         "can't stat `%s': %s.", fn, strerror(errno));
63                 return 1;
64         }
65
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);
69
70         switch (sb->st_mode & S_IFMT) {
71         case S_IFDIR:
72                 ckfputs("directory", stdout);
73                 return 1;
74         case S_IFCHR:
75                 (void) printf("character special (%ld/%ld)",
76                         (long) major(sb->st_rdev), (long) minor(sb->st_rdev));
77                 return 1;
78         case S_IFBLK:
79                 (void) printf("block special (%ld/%ld)",
80                         (long) major(sb->st_rdev), (long) minor(sb->st_rdev));
81                 return 1;
82         /* TODO add code to handle V7 MUX and Blit MUX files */
83 #ifdef  S_IFIFO
84         case S_IFIFO:
85                 ckfputs("fifo (named pipe)", stdout);
86                 return 1;
87 #endif
88 #ifdef  S_IFLNK
89         case S_IFLNK:
90                 {
91                         char buf[BUFSIZ+4];
92                         register int nch;
93                         struct stat tstatbuf;
94
95                         if ((nch = readlink(fn, buf, BUFSIZ-1)) <= 0) {
96                                 ckfprintf(stdout, "unreadable symlink: %s.",
97                                       strerror(errno));
98                                 return 1;
99                         }
100                         buf[nch] = '\0';        /* readlink(2) forgets this */
101
102                         /* If broken symlink, say so and quit early. */
103                         if (*buf == '/') {
104                             if (stat(buf, &tstatbuf) < 0) {
105                                 ckfprintf(stdout,
106                                         "broken symbolic link to %s", buf);
107                                 return 1;
108                             }
109                         }
110                         else {
111                             char *tmp;
112                             char buf2[BUFSIZ+BUFSIZ+4];
113
114                             if ((tmp = strrchr(fn,  '/')) == NULL) {
115                                 tmp = buf; /* in current directory anyway */
116                             }
117                             else {
118                                 strcpy (buf2, fn);  /* take directory part */
119                                 buf2[tmp-fn+1] = '\0';
120                                 strcat (buf2, buf); /* plus (relative) symlink */
121                                 tmp = buf2;
122                             }
123                             if (stat(tmp, &tstatbuf) < 0) {
124                                 ckfprintf(stdout,
125                                         "broken symbolic link to %s", buf);
126                                 return 1;
127                             }
128                         }
129
130                         /* Otherwise, handle it. */
131                         if (lflag) {
132                                 process(buf, strlen(buf));
133                                 return 1;
134                         } else { /* just print what it points to */
135                                 ckfputs("symbolic link to ", stdout);
136                                 ckfputs(buf, stdout);
137                         }
138                 }
139                 return 1;
140 #endif
141 #ifdef  S_IFSOCK
142 #ifndef __COHERENT__
143         case S_IFSOCK:
144                 ckfputs("socket", stdout);
145                 return 1;
146 #endif
147 #endif
148         case S_IFREG:
149                 break;
150         default:
151                 err(1, "invalid mode 0%o", sb->st_mode);
152                 /*NOTREACHED*/
153         }
154
155         /*
156          * regular file, check next possibility
157          */
158         if (sb->st_size == 0) {
159                 ckfputs("empty", stdout);
160                 return 1;
161         }
162         return 0;
163 }