file.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /*
  2. * Copyright (c) Ian F. Darwin 1986-1995.
  3. * Software written by Ian F. Darwin and others;
  4. * maintained 1995-present by Christos Zoulas and others.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice immediately at the beginning of the file, without modification,
  11. * this list of conditions, and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  20. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. */
  28. /*
  29. * file - find type of a file or files - main program.
  30. */
  31. #include "file.h"
  32. #include "magic.h"
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <unistd.h>
  36. #include <string.h>
  37. #include <sys/types.h>
  38. #include <sys/param.h> /* for MAXPATHLEN */
  39. #include <sys/stat.h>
  40. #include <fcntl.h> /* for open() */
  41. #ifdef RESTORE_TIME
  42. # if (__COHERENT__ >= 0x420)
  43. # include <sys/utime.h>
  44. # else
  45. # ifdef USE_UTIMES
  46. # include <sys/time.h>
  47. # else
  48. # include <utime.h>
  49. # endif
  50. # endif
  51. #endif
  52. #ifdef HAVE_UNISTD_H
  53. #include <unistd.h> /* for read() */
  54. #endif
  55. #ifdef HAVE_LOCALE_H
  56. #include <locale.h>
  57. #endif
  58. #ifdef HAVE_WCHAR_H
  59. #include <wchar.h>
  60. #endif
  61. #ifdef HAVE_GETOPT_H
  62. #include <getopt.h> /* for long options (is this portable?)*/
  63. #else
  64. #undef HAVE_GETOPT_LONG
  65. #endif
  66. #include <netinet/in.h> /* for byte swapping */
  67. #include "patchlevel.h"
  68. #ifndef lint
  69. FILE_RCSID("@(#)$Id: file.c,v 1.95 2004/09/27 15:28:37 christos Exp $")
  70. #endif /* lint */
  71. #ifdef S_IFLNK
  72. #define SYMLINKFLAG "L"
  73. #else
  74. #define SYMLINKFLAG ""
  75. #endif
  76. # define USAGE "Usage: %s [-bcik" SYMLINKFLAG "nNsvz] [-f namefile] [-F separator] [-m magicfiles] file...\n %s -C -m magicfiles\n"
  77. #ifndef MAXPATHLEN
  78. #define MAXPATHLEN 512
  79. #endif
  80. private int /* Global command-line options */
  81. bflag = 0, /* brief output format */
  82. nopad = 0, /* Don't pad output */
  83. nobuffer = 0; /* Do not buffer stdout */
  84. private const char *magicfile = 0; /* where the magic is */
  85. private const char *default_magicfile = MAGIC;
  86. private char *separator = ":"; /* Default field separator */
  87. private char *progname; /* used throughout */
  88. private struct magic_set *magic;
  89. private void unwrap(char *);
  90. private void usage(void);
  91. #ifdef HAVE_GETOPT_LONG
  92. private void help(void);
  93. #endif
  94. #if 0
  95. private int byteconv4(int, int, int);
  96. private short byteconv2(int, int, int);
  97. #endif
  98. int main(int, char *[]);
  99. private void process(const char *, int);
  100. private void load(const char *, int);
  101. /*
  102. * main - parse arguments and handle options
  103. */
  104. int
  105. main(int argc, char *argv[])
  106. {
  107. int c;
  108. int action = 0, didsomefiles = 0, errflg = 0;
  109. int flags = 0;
  110. char *home, *usermagic;
  111. struct stat sb;
  112. #define OPTSTRING "bcCdf:F:ikLm:nNprsvz"
  113. #ifdef HAVE_GETOPT_LONG
  114. int longindex;
  115. private struct option long_options[] =
  116. {
  117. {"version", 0, 0, 'v'},
  118. {"help", 0, 0, 0},
  119. {"brief", 0, 0, 'b'},
  120. {"checking-printout", 0, 0, 'c'},
  121. {"debug", 0, 0, 'd'},
  122. {"files-from", 1, 0, 'f'},
  123. {"separator", 1, 0, 'F'},
  124. {"mime", 0, 0, 'i'},
  125. {"keep-going", 0, 0, 'k'},
  126. #ifdef S_IFLNK
  127. {"dereference", 0, 0, 'L'},
  128. #endif
  129. {"magic-file", 1, 0, 'm'},
  130. #if defined(HAVE_UTIME) || defined(HAVE_UTIMES)
  131. {"preserve-date", 0, 0, 'p'},
  132. #endif
  133. {"uncompress", 0, 0, 'z'},
  134. {"raw", 0, 0, 'r'},
  135. {"no-buffer", 0, 0, 'n'},
  136. {"no-pad", 0, 0, 'N'},
  137. {"special-files", 0, 0, 's'},
  138. {"compile", 0, 0, 'C'},
  139. {0, 0, 0, 0},
  140. };
  141. #endif
  142. #ifdef LC_CTYPE
  143. setlocale(LC_CTYPE, ""); /* makes islower etc work for other langs */
  144. #endif
  145. #ifdef __EMX__
  146. /* sh-like wildcard expansion! Shouldn't hurt at least ... */
  147. _wildcard(&argc, &argv);
  148. #endif
  149. if ((progname = strrchr(argv[0], '/')) != NULL)
  150. progname++;
  151. else
  152. progname = argv[0];
  153. magicfile = default_magicfile;
  154. if ((usermagic = getenv("MAGIC")) != NULL)
  155. magicfile = usermagic;
  156. else
  157. if ((home = getenv("HOME")) != NULL) {
  158. if ((usermagic = malloc(strlen(home) + 8)) != NULL) {
  159. (void)strcpy(usermagic, home);
  160. (void)strcat(usermagic, "/.magic");
  161. if (stat(usermagic, &sb)<0)
  162. free(usermagic);
  163. else
  164. magicfile = usermagic;
  165. }
  166. }
  167. #ifndef HAVE_GETOPT_LONG
  168. while ((c = getopt(argc, argv, OPTSTRING)) != -1)
  169. #else
  170. while ((c = getopt_long(argc, argv, OPTSTRING, long_options,
  171. &longindex)) != -1)
  172. #endif
  173. switch (c) {
  174. #ifdef HAVE_GETOPT_LONG
  175. case 0 :
  176. if (longindex == 1)
  177. help();
  178. break;
  179. #endif
  180. case 'b':
  181. ++bflag;
  182. break;
  183. case 'c':
  184. action = FILE_CHECK;
  185. break;
  186. case 'C':
  187. action = FILE_COMPILE;
  188. break;
  189. case 'd':
  190. flags |= MAGIC_DEBUG|MAGIC_CHECK;
  191. break;
  192. case 'f':
  193. if(action)
  194. usage();
  195. load(magicfile, flags);
  196. unwrap(optarg);
  197. ++didsomefiles;
  198. break;
  199. case 'F':
  200. separator = optarg;
  201. break;
  202. case 'i':
  203. flags |= MAGIC_MIME;
  204. break;
  205. case 'k':
  206. flags |= MAGIC_CONTINUE;
  207. break;
  208. case 'm':
  209. magicfile = optarg;
  210. break;
  211. case 'n':
  212. ++nobuffer;
  213. break;
  214. case 'N':
  215. ++nopad;
  216. break;
  217. #if defined(HAVE_UTIME) || defined(HAVE_UTIMES)
  218. case 'p':
  219. flags |= MAGIC_PRESERVE_ATIME;
  220. break;
  221. #endif
  222. case 'r':
  223. flags |= MAGIC_RAW;
  224. break;
  225. case 's':
  226. flags |= MAGIC_DEVICES;
  227. break;
  228. case 'v':
  229. (void) fprintf(stdout, "%s-%d.%.2d\n", progname,
  230. FILE_VERSION_MAJOR, patchlevel);
  231. (void) fprintf(stdout, "magic file from %s\n",
  232. magicfile);
  233. return 1;
  234. case 'z':
  235. flags |= MAGIC_COMPRESS;
  236. break;
  237. #ifdef S_IFLNK
  238. case 'L':
  239. flags |= MAGIC_SYMLINK;
  240. break;
  241. #endif
  242. case '?':
  243. default:
  244. errflg++;
  245. break;
  246. }
  247. if (errflg) {
  248. usage();
  249. }
  250. switch(action) {
  251. case FILE_CHECK:
  252. case FILE_COMPILE:
  253. magic = magic_open(flags|MAGIC_CHECK);
  254. if (magic == NULL) {
  255. (void)fprintf(stderr, "%s: %s\n", progname,
  256. strerror(errno));
  257. return 1;
  258. }
  259. c = action == FILE_CHECK ? magic_check(magic, magicfile) :
  260. magic_compile(magic, magicfile);
  261. if (c == -1) {
  262. (void)fprintf(stderr, "%s: %s\n", progname,
  263. magic_error(magic));
  264. return -1;
  265. }
  266. return 0;
  267. default:
  268. load(magicfile, flags);
  269. break;
  270. }
  271. if (optind == argc) {
  272. if (!didsomefiles) {
  273. usage();
  274. }
  275. }
  276. else {
  277. int i, wid, nw;
  278. for (wid = 0, i = optind; i < argc; i++) {
  279. nw = file_mbswidth(argv[i]);
  280. if (nw > wid)
  281. wid = nw;
  282. }
  283. for (; optind < argc; optind++)
  284. process(argv[optind], wid);
  285. }
  286. magic_close(magic);
  287. return 0;
  288. }
  289. private void
  290. load(const char *m, int flags)
  291. {
  292. if (magic)
  293. return;
  294. magic = magic_open(flags);
  295. if (magic == NULL) {
  296. (void)fprintf(stderr, "%s: %s\n", progname, strerror(errno));
  297. exit(1);
  298. }
  299. if (magic_load(magic, magicfile) == -1) {
  300. (void)fprintf(stderr, "%s: %s\n",
  301. progname, magic_error(magic));
  302. exit(1);
  303. }
  304. }
  305. /*
  306. * unwrap -- read a file of filenames, do each one.
  307. */
  308. private void
  309. unwrap(char *fn)
  310. {
  311. char buf[MAXPATHLEN];
  312. FILE *f;
  313. int wid = 0, cwid;
  314. if (strcmp("-", fn) == 0) {
  315. f = stdin;
  316. wid = 1;
  317. } else {
  318. if ((f = fopen(fn, "r")) == NULL) {
  319. (void)fprintf(stderr, "%s: Cannot open `%s' (%s).\n",
  320. progname, fn, strerror(errno));
  321. exit(1);
  322. }
  323. while (fgets(buf, MAXPATHLEN, f) != NULL) {
  324. cwid = file_mbswidth(buf) - 1;
  325. if (cwid > wid)
  326. wid = cwid;
  327. }
  328. rewind(f);
  329. }
  330. while (fgets(buf, MAXPATHLEN, f) != NULL) {
  331. buf[file_mbswidth(buf)-1] = '\0';
  332. process(buf, wid);
  333. if(nobuffer)
  334. (void) fflush(stdout);
  335. }
  336. (void) fclose(f);
  337. }
  338. private void
  339. process(const char *inname, int wid)
  340. {
  341. const char *type;
  342. int std_in = strcmp(inname, "-") == 0;
  343. if (wid > 0 && !bflag)
  344. (void) printf("%s%s%*s ", std_in ? "/dev/stdin" : inname,
  345. separator, (int) (nopad ? 0 : (wid - file_mbswidth(inname))), "");
  346. type = magic_file(magic, std_in ? NULL : inname);
  347. if (type == NULL)
  348. printf("ERROR: %s\n", magic_error(magic));
  349. else
  350. printf("%s\n", type);
  351. }
  352. #if 0
  353. /*
  354. * byteconv4
  355. * Input:
  356. * from 4 byte quantity to convert
  357. * same whether to perform byte swapping
  358. * big_endian whether we are a big endian host
  359. */
  360. private int
  361. byteconv4(int from, int same, int big_endian)
  362. {
  363. if (same)
  364. return from;
  365. else if (big_endian) { /* lsb -> msb conversion on msb */
  366. union {
  367. int i;
  368. char c[4];
  369. } retval, tmpval;
  370. tmpval.i = from;
  371. retval.c[0] = tmpval.c[3];
  372. retval.c[1] = tmpval.c[2];
  373. retval.c[2] = tmpval.c[1];
  374. retval.c[3] = tmpval.c[0];
  375. return retval.i;
  376. }
  377. else
  378. return ntohl(from); /* msb -> lsb conversion on lsb */
  379. }
  380. /*
  381. * byteconv2
  382. * Same as byteconv4, but for shorts
  383. */
  384. private short
  385. byteconv2(int from, int same, int big_endian)
  386. {
  387. if (same)
  388. return from;
  389. else if (big_endian) { /* lsb -> msb conversion on msb */
  390. union {
  391. short s;
  392. char c[2];
  393. } retval, tmpval;
  394. tmpval.s = (short) from;
  395. retval.c[0] = tmpval.c[1];
  396. retval.c[1] = tmpval.c[0];
  397. return retval.s;
  398. }
  399. else
  400. return ntohs(from); /* msb -> lsb conversion on lsb */
  401. }
  402. #endif
  403. size_t
  404. file_mbswidth(const char *s)
  405. {
  406. #if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
  407. size_t bytesconsumed, old_n, n, width = 0;
  408. mbstate_t state;
  409. wchar_t nextchar;
  410. (void)memset(&state, 0, sizeof(mbstate_t));
  411. old_n = n = strlen(s);
  412. while (n > 0) {
  413. bytesconsumed = mbrtowc(&nextchar, s, n, &state);
  414. if (bytesconsumed == (size_t)(-1) ||
  415. bytesconsumed == (size_t)(-2)) {
  416. /* Something went wrong, return something reasonable */
  417. return old_n;
  418. }
  419. if (s[0] == '\n') {
  420. /*
  421. * do what strlen() would do, so that caller
  422. * is always right
  423. */
  424. width++;
  425. } else
  426. width += wcwidth(nextchar);
  427. s += bytesconsumed, n -= bytesconsumed;
  428. }
  429. return width;
  430. #else
  431. return strlen(s);
  432. #endif
  433. }
  434. private void
  435. usage(void)
  436. {
  437. (void)fprintf(stderr, USAGE, progname, progname);
  438. #ifdef HAVE_GETOPT_LONG
  439. (void)fputs("Try `file --help' for more information.\n", stderr);
  440. #endif
  441. exit(1);
  442. }
  443. #ifdef HAVE_GETOPT_LONG
  444. private void
  445. help(void)
  446. {
  447. puts(
  448. "Usage: file [OPTION]... [FILE]...\n"
  449. "Determine file type of FILEs.\n"
  450. "\n"
  451. " -m, --magic-file LIST use LIST as a colon-separated list of magic\n"
  452. " number files\n"
  453. " -z, --uncompress try to look inside compressed files\n"
  454. " -b, --brief do not prepend filenames to output lines\n"
  455. " -c, --checking-printout print the parsed form of the magic file, use in\n"
  456. " conjunction with -m to debug a new magic file\n"
  457. " before installing it\n"
  458. " -f, --files-from FILE read the filenames to be examined from FILE\n"
  459. " -F, --separator string use string as separator instead of `:'\n"
  460. " -i, --mime output mime type strings\n"
  461. " -k, --keep-going don't stop at the first match\n"
  462. " -L, --dereference causes symlinks to be followed\n"
  463. " -n, --no-buffer do not buffer output\n"
  464. " -N, --no-pad do not pad output\n"
  465. " -p, --preserve-date preserve access times on files\n"
  466. " -r, --raw don't translate unprintable chars to \\ooo\n"
  467. " -s, --special-files treat special (block/char devices) files as\n"
  468. " ordinary ones\n"
  469. " --help display this help and exit\n"
  470. " --version output version information and exit\n"
  471. );
  472. exit(0);
  473. }
  474. #endif