| 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 |
#include "global.h" |
| 29 |
|
| 30 |
#ifdef USE_MACOS_TYPE |
| 31 |
#include <CoreServices/CoreServices.h> |
| 32 |
#endif |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
static void analyze_file(const char *filename); |
| 39 |
static void print_kind(int filekind, u8 size, int size_known); |
| 40 |
|
| 41 |
#ifdef USE_MACOS_TYPE |
| 42 |
static void show_macos_type(const char *filename); |
| 43 |
#endif |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
int main(int argc, char *argv[]) |
| 50 |
{ |
| 51 |
int i; |
| 52 |
|
| 53 |
|
| 54 |
if (argc < 2) { |
| 55 |
fprintf(stderr, "Usage: %s <device/file>...\n", PROGNAME); |
| 56 |
return 1; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
print_line(0, ""); |
| 61 |
for (i = 1; i < argc; i++) { |
| 62 |
analyze_file(argv[i]); |
| 63 |
print_line(0, ""); |
| 64 |
} |
| 65 |
|
| 66 |
return 0; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
static void analyze_file(const char *filename) |
| 74 |
{ |
| 75 |
int fd, filekind; |
| 76 |
u8 filesize; |
| 77 |
struct stat sb; |
| 78 |
char *reason; |
| 79 |
SOURCE *s; |
| 80 |
|
| 81 |
print_line(0, "--- %s", filename); |
| 82 |
|
| 83 |
|
| 84 |
if (stat(filename, &sb) < 0) { |
| 85 |
errore("Can't stat %.300s", filename); |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
filekind = 0; |
| 90 |
filesize = 0; |
| 91 |
reason = NULL; |
| 92 |
if (S_ISREG(sb.st_mode)) { |
| 93 |
filesize = sb.st_size; |
| 94 |
print_kind(filekind, filesize, 1); |
| 95 |
} else if (S_ISBLK(sb.st_mode)) |
| 96 |
filekind = 1; |
| 97 |
else if (S_ISCHR(sb.st_mode)) |
| 98 |
filekind = 2; |
| 99 |
else if (S_ISDIR(sb.st_mode)) |
| 100 |
reason = "Is a directory"; |
| 101 |
else if (S_ISFIFO(sb.st_mode)) |
| 102 |
reason = "Is a FIFO"; |
| 103 |
#ifdef S_ISSOCK |
| 104 |
else if (S_ISSOCK(sb.st_mode)) |
| 105 |
reason = "Is a socket"; |
| 106 |
#endif |
| 107 |
else |
| 108 |
reason = "Is an unknown kind of special file"; |
| 109 |
|
| 110 |
if (reason != NULL) { |
| 111 |
error("%.300s: %s", filename, reason); |
| 112 |
return; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
#ifdef USE_MACOS_TYPE |
| 117 |
if (filekind == 0) |
| 118 |
show_macos_type(filename); |
| 119 |
#endif |
| 120 |
|
| 121 |
|
| 122 |
if (filekind == 0 && filesize == 0) |
| 123 |
return; |
| 124 |
|
| 125 |
|
| 126 |
fd = open(filename, O_RDONLY); |
| 127 |
if (fd < 0) { |
| 128 |
errore("Can't open %.300s", filename); |
| 129 |
return; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
if (filekind == 2) { |
| 134 |
if (isatty(fd)) { |
| 135 |
error("%.300s: Is a TTY device", filename); |
| 136 |
return; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
s = init_file_source(fd, filekind); |
| 142 |
|
| 143 |
|
| 144 |
if (filekind != 0) |
| 145 |
print_kind(filekind, s->size, s->size_known); |
| 146 |
|
| 147 |
|
| 148 |
analyze_source(s, 0); |
| 149 |
|
| 150 |
|
| 151 |
close_source(s); |
| 152 |
} |
| 153 |
|
| 154 |
static void print_kind(int filekind, u8 size, int size_known) |
| 155 |
{ |
| 156 |
char buf[256], *kindname; |
| 157 |
|
| 158 |
if (filekind == 0) |
| 159 |
kindname = "Regular file"; |
| 160 |
else if (filekind == 1) |
| 161 |
kindname = "Block device"; |
| 162 |
else if (filekind == 2) |
| 163 |
kindname = "Character device"; |
| 164 |
else |
| 165 |
kindname = "Unknown kind"; |
| 166 |
|
| 167 |
if (size_known) { |
| 168 |
format_size_verbose(buf, size); |
| 169 |
print_line(0, "%s, size %s", kindname, buf); |
| 170 |
} else { |
| 171 |
print_line(0, "%s, unknown size", kindname); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
#ifdef USE_MACOS_TYPE |
| 180 |
|
| 181 |
static void show_macos_type(const char *filename) |
| 182 |
{ |
| 183 |
int err; |
| 184 |
FSRef ref; |
| 185 |
FSCatalogInfo info; |
| 186 |
FInfo *finfo; |
| 187 |
|
| 188 |
err = FSPathMakeRef(filename, &ref, NULL); |
| 189 |
if (err == 0) { |
| 190 |
err = FSGetCatalogInfo(&ref, kFSCatInfoFinderInfo, |
| 191 |
&info, NULL, NULL, NULL); |
| 192 |
} |
| 193 |
|
| 194 |
if (err == 0) { |
| 195 |
finfo = (FInfo *)(info.finderInfo); |
| 196 |
if (finfo->fdType != 0 || finfo->fdCreator != 0) { |
| 197 |
char typecode[5], creatorcode[5], s1[256], s2[256]; |
| 198 |
|
| 199 |
memcpy(typecode, &finfo->fdType, 4); |
| 200 |
typecode[4] = 0; |
| 201 |
format_ascii(typecode, s1); |
| 202 |
|
| 203 |
memcpy(creatorcode, &finfo->fdCreator, 4); |
| 204 |
creatorcode[4] = 0; |
| 205 |
format_ascii(creatorcode, s2); |
| 206 |
|
| 207 |
print_line(0, "Type code \"%s\", creator code \"%s\"", |
| 208 |
s1, s2); |
| 209 |
} else { |
| 210 |
print_line(0, "No type and creator code"); |
| 211 |
} |
| 212 |
} |
| 213 |
if (err) { |
| 214 |
print_line(0, "Type and creator code unknown (error %d)", err); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
#endif |
| 219 |
|
| 220 |
|