file.c (8342B)
1 /* see LICENSE file for copyright and license details */ 2 #include "../../shared/libmagic/magic.h" 3 #include "arg.h" 4 #include "filesystem.h" 5 #include "util.h" 6 7 #include <sys/stat.h> 8 #include <sys/types.h> 9 #include <errno.h> 10 #include <fcntl.h> 11 #include <limits.h> 12 #include <stdio.h> 13 #include <stdlib.h> 14 #include <string.h> 15 #include <unistd.h> 16 17 #define DESCLEN 4096 18 #define MIMELEN 256 19 20 enum Output { OUT_DESC, OUT_MIME_TYPE, OUT_MIME }; 21 22 static struct Magic *db; 23 static char *dbpath; /* NULL for the default databases */ 24 static int follow = 1; 25 static int iflag; 26 static int bflag; 27 static int output = OUT_DESC; 28 29 static void 30 usage(void) 31 { 32 #if FEATURE_FILE_EXT 33 eprintf("usage: %s [-bdhLi] [-M file] [-m file] [--mime-type | --mime] file ...\n", argv0); 34 #else 35 eprintf( 36 "usage: %s [-dh] [-M file] [-m file] file ...\n %s -i [-h] file ...\n", argv0, argv0 37 ); 38 #endif 39 } 40 41 static void 42 dbpath_add(const char *path) 43 { 44 size_t len = dbpath ? strlen(dbpath) : 0; 45 46 dbpath = erealloc(dbpath, len + strlen(path) + 2); 47 if (len) 48 dbpath[len++] = ':'; 49 strcpy(dbpath + len, path); 50 } 51 52 static void 53 dbpath_add_default(void) 54 { 55 const char *env; 56 57 dbpath_add((env = getenv("MAGIC")) ? env : MAGIC_PATH_DEFAULT); 58 } 59 60 /* load the databases only once a regular file needs them */ 61 static struct Magic * 62 dbget(void) 63 { 64 if (!db && !(db = magic_open(dbpath))) 65 eprintf("cannot load magic database %s:", dbpath ? dbpath : MAGIC_PATH_DEFAULT); 66 return db; 67 } 68 69 static void 70 put(const char *name, const char *s) 71 { 72 if (bflag) 73 printf("%s\n", s); 74 else 75 printf("%s: %s\n", name, s); 76 } 77 78 static void 79 put_mime(const char *name, const char *type, const char *charset) 80 { 81 char buf[MIMELEN + 64]; 82 83 if (output == OUT_MIME && charset) 84 snprintf(buf, sizeof(buf), "%s; charset=%s", type, charset); 85 else 86 snprintf(buf, sizeof(buf), "%s", type); 87 put(name, buf); 88 } 89 90 static void 91 cantopen(const char *name, int err) 92 { 93 char buf[PATH_MAX + 256]; 94 95 snprintf(buf, sizeof(buf), "cannot open `%s' (%s)", name, strerror(err)); 96 put(name, buf); 97 } 98 99 /* the charset --mime prints, from the same bytes the engine judges it by */ 100 static const char * 101 charset(int fd) 102 { 103 static unsigned char buf[MAGIC_CHARSET_MAX]; 104 ssize_t n; 105 106 if ((n = pread(fd, buf, sizeof(buf), 0)) < 0) 107 n = 0; 108 return magic_charset(buf, (size_t)n); 109 } 110 111 static int 112 classify(const char *name, int fd, const char *prefix) 113 { 114 char desc[DESCLEN], mime[MIMELEN], buf[DESCLEN + 64]; 115 116 if (magic_fd(dbget(), fd, desc, sizeof(desc), mime, sizeof(mime)) < 0) { 117 cantopen(name, errno); 118 return 1; 119 } 120 if (output != OUT_DESC) { 121 put_mime(name, mime, charset(fd)); 122 } else { 123 snprintf(buf, sizeof(buf), "%s%s%s", prefix, *prefix ? " " : "", desc); 124 put(name, buf); 125 } 126 return 0; 127 } 128 129 /* types that come from the file system rather than from the bytes */ 130 static void 131 put_fs(const char *name, const char *prefix, const char *desc, const char *mime, int charset) 132 { 133 char buf[DESCLEN]; 134 135 if (output != OUT_DESC) { 136 put_mime(name, mime, charset ? "binary" : NULL); 137 return; 138 } 139 snprintf(buf, sizeof(buf), "%s%s%s", prefix, *prefix ? ", " : "", desc); 140 put(name, buf); 141 } 142 143 static int 144 symlink_type(const char *name, const char *prefix) 145 { 146 struct stat st; 147 char target[PATH_MAX], buf[PATH_MAX + 64]; 148 ssize_t n; 149 150 if ((n = readlink(name, target, sizeof(target) - 1)) <= 0) { 151 snprintf(buf, sizeof(buf), "unreadable symlink `%s' (%s)", name, strerror(errno)); 152 put_fs(name, prefix, buf, "inode/symlink", 1); 153 return 0; 154 } 155 target[n] = '\0'; 156 if (stat(name, &st) < 0) { 157 snprintf(buf, sizeof(buf), "broken symbolic link to %s", target); 158 put_fs(name, prefix, buf, "inode/symlink", 0); 159 return 0; 160 } 161 snprintf(buf, sizeof(buf), "symbolic link to %s", target); 162 put_fs(name, prefix, buf, "inode/symlink", 1); 163 return 0; 164 } 165 166 static void 167 mode_prefix(char *buf, size_t n, mode_t mode) 168 { 169 static const struct { 170 mode_t bit; 171 const char *name; 172 } bits[] = {{S_ISUID, "setuid"}, {S_ISGID, "setgid"}, {S_ISVTX, "sticky"}}; 173 size_t i, len = 0; 174 175 buf[0] = '\0'; 176 for (i = 0; i < sizeof(bits) / sizeof(bits[0]); i++) { 177 if (mode & bits[i].bit) { 178 snprintf(buf + len, n - len, "%s%s", len ? ", " : "", bits[i].name); 179 len = strlen(buf); 180 } 181 } 182 } 183 184 static int 185 file(const char *name) 186 { 187 struct stat st; 188 char prefix[64], buf[128]; 189 int fd, ret; 190 191 prefix[0] = '\0'; 192 #if FEATURE_FILE_EXT 193 if (strcmp(name, "-") == 0) { 194 if (fstat(0, &st) < 0) { 195 cantopen(name, errno); 196 return 1; 197 } 198 return classify(name, 0, prefix); 199 } 200 #endif 201 /* a link to nothing is reported as a link even when following */ 202 if ((follow ? stat(name, &st) : lstat(name, &st)) < 0 && (!follow || lstat(name, &st) < 0)) { 203 cantopen(name, errno); 204 return 1; 205 } 206 mode_prefix(prefix, sizeof(prefix), st.st_mode); 207 switch (st.st_mode & S_IFMT) { 208 case S_IFDIR: 209 put_fs(name, prefix, "directory", "inode/directory", 1); 210 return 0; 211 case S_IFCHR: 212 snprintf( 213 buf, 214 sizeof(buf), 215 "character special (%ld/%ld)", 216 (long)fs_devmajor(st.st_rdev), 217 (long)fs_devminor(st.st_rdev) 218 ); 219 put_fs(name, prefix, buf, "inode/chardevice", 1); 220 return 0; 221 case S_IFBLK: 222 snprintf( 223 buf, 224 sizeof(buf), 225 "block special (%ld/%ld)", 226 (long)fs_devmajor(st.st_rdev), 227 (long)fs_devminor(st.st_rdev) 228 ); 229 put_fs(name, prefix, buf, "inode/blockdevice", 1); 230 return 0; 231 case S_IFIFO: 232 put_fs(name, prefix, "fifo (named pipe)", "inode/fifo", 1); 233 return 0; 234 case S_IFSOCK: 235 put_fs(name, prefix, "socket", "inode/socket", 1); 236 return 0; 237 case S_IFLNK: 238 return symlink_type(name, prefix); 239 case S_IFREG: 240 break; 241 default: 242 weprintf("%s: invalid mode 0%o\n", name, (unsigned)st.st_mode); 243 return 1; 244 } 245 if (st.st_size == 0) { 246 put_fs(name, prefix, "empty", "inode/x-empty", 1); 247 return 0; 248 } 249 if (iflag) { 250 put_fs(name, prefix, "regular file", "application/octet-stream", 1); 251 return 0; 252 } 253 if ((fd = open(name, O_RDONLY)) < 0) { 254 cantopen(name, errno); 255 return 1; 256 } 257 ret = classify(name, fd, prefix); 258 close(fd); 259 return ret; 260 } 261 262 // ?man file: determine file type 263 // ?man arguments: file ... 264 // ?man file classifies each file by the file system first (directory, device, fifo, 265 // ?man socket, symbolic link, empty) and then by its bytes, with the magic(5) tests of 266 // ?man the file(1) databases: a compiled magic.mgc or the text sources. without -M 267 // ?man the default databases are MAGIC from the environment, else a built in list 268 int 269 main(int argc, char *argv[]) 270 { 271 int ret = 0, Mflag = 0, dflag = 0; 272 273 ARGBEGIN 274 { 275 // ?man -d: apply the default tests, where it appears among -M and -m 276 case 'd': 277 if (!dflag) 278 dbpath_add_default(); 279 dflag = 1; 280 break; 281 // ?man -h: identify a symbolic link itself, not the file it points to 282 case 'h': 283 follow = 0; 284 break; 285 // ?man -i: dont classify regular files further than regular file 286 case 'i': 287 iflag = 1; 288 break; 289 // ?man -M:file: use the tests in file instead of the default tests 290 case 'M': 291 dbpath_add(EARGF(usage())); 292 Mflag = 1; 293 break; 294 // ?man -m:file: use the tests in file before the default tests 295 case 'm': 296 dbpath_add(EARGF(usage())); 297 break; 298 #if FEATURE_FILE_EXT 299 // ?man -b: brief, dont prefix lines with the file name 300 case 'b': 301 bflag = 1; 302 break; 303 // ?man -L: follow symbolic links 304 case 'L': 305 follow = 1; 306 break; 307 // ?man --mime-type: print the mime type instead of the description 308 // ?man --mime: print the mime type and the charset 309 case '-': 310 if (strcmp(argv[0], "-mime-type") == 0) 311 output = OUT_MIME_TYPE; 312 else if (strcmp(argv[0], "-mime") == 0) 313 output = OUT_MIME; 314 else 315 usage(); 316 brk_ = 1; 317 break; 318 #endif 319 default: 320 usage(); 321 } 322 ARGEND 323 324 if (!argc) 325 usage(); 326 if (dbpath && !Mflag && !dflag) 327 dbpath_add_default(); 328 for (; *argv; argv++) 329 ret |= file(*argv); 330 magic_close(db); 331 free(dbpath); 332 333 if (fshut(stdout, "<stdout>")) 334 ret = 1; 335 return ret; 336 }