-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
220 lines (183 loc) · 4.89 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
* main.c
* Main entry point and global utility functions.
*
* Copyright (c) 2003 Christoph Pfisterer
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "global.h"
#ifdef USE_MACOS_TYPE
#include <CoreServices/CoreServices.h>
#endif
/*
* local functions
*/
static void analyze_file(const char *filename);
static void print_kind(int filekind, u8 size, int size_known);
#ifdef USE_MACOS_TYPE
static void show_macos_type(const char *filename);
#endif
/*
* entry point
*/
int main(int argc, char *argv[])
{
int i;
/* argument check */
if (argc < 2) {
fprintf(stderr, "Usage: %s <device/file>...\n", PROGNAME);
return 1;
}
/* loop over filenames */
print_line(0, "");
for (i = 1; i < argc; i++) {
analyze_file(argv[i]);
print_line(0, "");
}
return 0;
}
/*
* Analyze one file
*/
static void analyze_file(const char *filename)
{
int fd, filekind;
u8 filesize;
struct stat sb;
char *reason;
SOURCE *s;
print_line(0, "--- %s", filename);
/* stat check */
if (stat(filename, &sb) < 0) {
errore("Can't stat %.300s", filename);
return;
}
filekind = 0;
filesize = 0;
reason = NULL;
if (S_ISREG(sb.st_mode)) {
filesize = sb.st_size;
print_kind(filekind, filesize, 1);
} else if (S_ISBLK(sb.st_mode))
filekind = 1;
else if (S_ISCHR(sb.st_mode))
filekind = 2;
else if (S_ISDIR(sb.st_mode))
reason = "Is a directory";
else if (S_ISFIFO(sb.st_mode))
reason = "Is a FIFO";
#ifdef S_ISSOCK
else if (S_ISSOCK(sb.st_mode))
reason = "Is a socket";
#endif
else
reason = "Is an unknown kind of special file";
if (reason != NULL) {
error("%.300s: %s", filename, reason);
return;
}
/* Mac OS type & creator code (if running on Mac OS X) */
#ifdef USE_MACOS_TYPE
if (filekind == 0)
show_macos_type(filename);
#endif
/* empty regular files need no further analysis */
if (filekind == 0 && filesize == 0)
return;
/* open for reading */
fd = open(filename, O_RDONLY);
if (fd < 0) {
errore("Can't open %.300s", filename);
return;
}
/* (try to) guard against TTY character devices */
if (filekind == 2) {
if (isatty(fd)) {
error("%.300s: Is a TTY device", filename);
return;
}
}
/* create a source */
s = init_file_source(fd, filekind);
/* tell the user what it is */
if (filekind != 0)
print_kind(filekind, s->size, s->size_known);
/* now analyze it */
analyze_source(s, 0);
/* finish it up */
close_source(s);
}
static void print_kind(int filekind, u8 size, int size_known)
{
char buf[256], *kindname;
if (filekind == 0)
kindname = "Regular file";
else if (filekind == 1)
kindname = "Block device";
else if (filekind == 2)
kindname = "Character device";
else
kindname = "Unknown kind";
if (size_known) {
format_size_verbose(buf, size);
print_line(0, "%s, size %s", kindname, buf);
} else {
print_line(0, "%s, unknown size", kindname);
}
}
/*
* Mac OS type & creator code
*/
#ifdef USE_MACOS_TYPE
static void show_macos_type(const char *filename)
{
int err;
FSRef ref;
FSCatalogInfo info;
FInfo *finfo;
err = FSPathMakeRef(filename, &ref, NULL);
if (err == 0) {
err = FSGetCatalogInfo(&ref, kFSCatInfoFinderInfo,
&info, NULL, NULL, NULL);
}
if (err == 0) {
finfo = (FInfo *)(info.finderInfo);
if (finfo->fdType != 0 || finfo->fdCreator != 0) {
char typecode[5], creatorcode[5], s1[256], s2[256];
memcpy(typecode, &finfo->fdType, 4);
typecode[4] = 0;
format_ascii(typecode, s1);
memcpy(creatorcode, &finfo->fdCreator, 4);
creatorcode[4] = 0;
format_ascii(creatorcode, s2);
print_line(0, "Type code \"%s\", creator code \"%s\"",
s1, s2);
} else {
print_line(0, "No type and creator code");
}
}
if (err) {
print_line(0, "Type and creator code unknown (error %d)", err);
}
}
#endif
/* EOF */