file.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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.97 2005/08/12 14:19:33 christos Exp $")
  70. #endif /* lint */
  71. #ifdef S_IFLNK
  72. #define SYMLINKFLAG "Lh"
  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 const 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:hikLm: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. {"no-dereference", 0, 0, 'h'},
  129. #endif
  130. {"magic-file", 1, 0, 'm'},
  131. #if defined(HAVE_UTIME) || defined(HAVE_UTIMES)
  132. {"preserve-date", 0, 0, 'p'},
  133. #endif
  134. {"uncompress", 0, 0, 'z'},
  135. {"raw", 0, 0, 'r'},
  136. {"no-buffer", 0, 0, 'n'},
  137. {"no-pad", 0, 0, 'N'},
  138. {"special-files", 0, 0, 's'},
  139. {"compile", 0, 0, 'C'},
  140. {0, 0, 0, 0},
  141. };
  142. #endif
  143. #ifdef LC_CTYPE
  144. setlocale(LC_CTYPE, ""); /* makes islower etc work for other langs */
  145. #endif
  146. #ifdef __EMX__
  147. /* sh-like wildcard expansion! Shouldn't hurt at least ... */
  148. _wildcard(&argc, &argv);
  149. #endif
  150. if ((progname = strrchr(argv[0], '/')) != NULL)
  151. progname++;
  152. else
  153. progname = argv[0];
  154. magicfile = default_magicfile;
  155. if ((usermagic = getenv("MAGIC")) != NULL)
  156. magicfile = usermagic;
  157. else
  158. if ((home = getenv("HOME")) != NULL) {
  159. if ((usermagic = malloc(strlen(home) + 8)) != NULL) {
  160. (void)strcpy(usermagic, home);
  161. (void)strcat(usermagic, "/.magic");
  162. if (stat(usermagic, &sb)<0)
  163. free(usermagic);
  164. else
  165. magicfile = usermagic;
  166. }
  167. }
  168. #ifdef S_IFLNK
  169. flags |= getenv("POSIXLY_CORRECT") ? MAGIC_SYMLINK : 0;
  170. #endif
  171. #ifndef HAVE_GETOPT_LONG
  172. while ((c = getopt(argc, argv, OPTSTRING)) != -1)
  173. #else
  174. while ((c = getopt_long(argc, argv, OPTSTRING, long_options,
  175. &longindex)) != -1)
  176. #endif
  177. switch (c) {
  178. #ifdef HAVE_GETOPT_LONG
  179. case 0 :
  180. if (longindex == 1)
  181. help();
  182. break;
  183. #endif
  184. case 'b':
  185. ++bflag;
  186. break;
  187. case 'c':
  188. action = FILE_CHECK;
  189. break;
  190. case 'C':
  191. action = FILE_COMPILE;
  192. break;
  193. case 'd':
  194. flags |= MAGIC_DEBUG|MAGIC_CHECK;
  195. break;
  196. case 'f':
  197. if(action)
  198. usage();
  199. load(magicfile, flags);
  200. unwrap(optarg);
  201. ++didsomefiles;
  202. break;
  203. case 'F':
  204. separator = optarg;
  205. break;
  206. case 'i':
  207. flags |= MAGIC_MIME;
  208. break;
  209. case 'k':
  210. flags |= MAGIC_CONTINUE;
  211. break;
  212. case 'm':
  213. magicfile = optarg;
  214. break;
  215. case 'n':
  216. ++nobuffer;
  217. break;
  218. case 'N':
  219. ++nopad;
  220. break;
  221. #if defined(HAVE_UTIME) || defined(HAVE_UTIMES)
  222. case 'p':
  223. flags |= MAGIC_PRESERVE_ATIME;
  224. break;
  225. #endif
  226. case 'r':
  227. flags |= MAGIC_RAW;
  228. break;
  229. case 's':
  230. flags |= MAGIC_DEVICES;
  231. break;
  232. case 'v':
  233. (void) fprintf(stdout, "%s-%d.%.2d\n", progname,
  234. FILE_VERSION_MAJOR, patchlevel);
  235. (void) fprintf(stdout, "magic file from %s\n",
  236. magicfile);
  237. return 1;
  238. case 'z':
  239. flags |= MAGIC_COMPRESS;
  240. break;
  241. #ifdef S_IFLNK
  242. case 'L':
  243. flags |= MAGIC_SYMLINK;
  244. break;
  245. case 'h':
  246. flags &= ~MAGIC_SYMLINK;
  247. break;
  248. #endif
  249. case '?':
  250. default:
  251. errflg++;
  252. break;
  253. }
  254. if (errflg) {
  255. usage();
  256. }
  257. switch(action) {
  258. case FILE_CHECK:
  259. case FILE_COMPILE:
  260. magic = magic_open(flags|MAGIC_CHECK);
  261. if (magic == NULL) {
  262. (void)fprintf(stderr, "%s: %s\n", progname,
  263. strerror(errno));
  264. return 1;
  265. }
  266. c = action == FILE_CHECK ? magic_check(magic, magicfile) :
  267. magic_compile(magic, magicfile);
  268. if (c == -1) {
  269. (void)fprintf(stderr, "%s: %s\n", progname,
  270. magic_error(magic));
  271. return -1;
  272. }
  273. return 0;
  274. default:
  275. load(magicfile, flags);
  276. break;
  277. }
  278. if (optind == argc) {
  279. if (!didsomefiles) {
  280. usage();
  281. }
  282. }
  283. else {
  284. int i, wid, nw;
  285. for (wid = 0, i = optind; i < argc; i++) {
  286. nw = file_mbswidth(argv[i]);
  287. if (nw > wid)
  288. wid = nw;
  289. }
  290. for (; optind < argc; optind++)
  291. process(argv[optind], wid);
  292. }
  293. magic_close(magic);
  294. return 0;
  295. }
  296. private void
  297. load(const char *m, int flags)
  298. {
  299. if (magic)
  300. return;
  301. magic = magic_open(flags);
  302. if (magic == NULL) {
  303. (void)fprintf(stderr, "%s: %s\n", progname, strerror(errno));
  304. exit(1);
  305. }
  306. if (magic_load(magic, magicfile) == -1) {
  307. (void)fprintf(stderr, "%s: %s\n",
  308. progname, magic_error(magic));
  309. exit(1);
  310. }
  311. }
  312. /*
  313. * unwrap -- read a file of filenames, do each one.
  314. */
  315. private void
  316. unwrap(char *fn)
  317. {
  318. char buf[MAXPATHLEN];
  319. FILE *f;
  320. int wid = 0, cwid;
  321. if (strcmp("-", fn) == 0) {
  322. f = stdin;
  323. wid = 1;
  324. } else {
  325. if ((f = fopen(fn, "r")) == NULL) {
  326. (void)fprintf(stderr, "%s: Cannot open `%s' (%s).\n",
  327. progname, fn, strerror(errno));
  328. exit(1);
  329. }
  330. while (fgets(buf, MAXPATHLEN, f) != NULL) {
  331. cwid = file_mbswidth(buf) - 1;
  332. if (cwid > wid)
  333. wid = cwid;
  334. }
  335. rewind(f);
  336. }
  337. while (fgets(buf, MAXPATHLEN, f) != NULL) {
  338. buf[file_mbswidth(buf)-1] = '\0';
  339. process(buf, wid);
  340. if(nobuffer)
  341. (void) fflush(stdout);
  342. }
  343. (void) fclose(f);
  344. }
  345. private void
  346. process(const char *inname, int wid)
  347. {
  348. const char *type;
  349. int std_in = strcmp(inname, "-") == 0;
  350. if (wid > 0 && !bflag)
  351. (void) printf("%s%s%*s ", std_in ? "/dev/stdin" : inname,
  352. separator, (int) (nopad ? 0 : (wid - file_mbswidth(inname))), "");
  353. type = magic_file(magic, std_in ? NULL : inname);
  354. if (type == NULL)
  355. printf("ERROR: %s\n", magic_error(magic));
  356. else
  357. printf("%s\n", type);
  358. }
  359. #if 0
  360. /*
  361. * byteconv4
  362. * Input:
  363. * from 4 byte quantity to convert
  364. * same whether to perform byte swapping
  365. * big_endian whether we are a big endian host
  366. */
  367. private int
  368. byteconv4(int from, int same, int big_endian)
  369. {
  370. if (same)
  371. return from;
  372. else if (big_endian) { /* lsb -> msb conversion on msb */
  373. union {
  374. int i;
  375. char c[4];
  376. } retval, tmpval;
  377. tmpval.i = from;
  378. retval.c[0] = tmpval.c[3];
  379. retval.c[1] = tmpval.c[2];
  380. retval.c[2] = tmpval.c[1];
  381. retval.c[3] = tmpval.c[0];
  382. return retval.i;
  383. }
  384. else
  385. return ntohl(from); /* msb -> lsb conversion on lsb */
  386. }
  387. /*
  388. * byteconv2
  389. * Same as byteconv4, but for shorts
  390. */
  391. private short
  392. byteconv2(int from, int same, int big_endian)
  393. {
  394. if (same)
  395. return from;
  396. else if (big_endian) { /* lsb -> msb conversion on msb */
  397. union {
  398. short s;
  399. char c[2];
  400. } retval, tmpval;
  401. tmpval.s = (short) from;
  402. retval.c[0] = tmpval.c[1];
  403. retval.c[1] = tmpval.c[0];
  404. return retval.s;
  405. }
  406. else
  407. return ntohs(from); /* msb -> lsb conversion on lsb */
  408. }
  409. #endif
  410. size_t
  411. file_mbswidth(const char *s)
  412. {
  413. #if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
  414. size_t bytesconsumed, old_n, n, width = 0;
  415. mbstate_t state;
  416. wchar_t nextchar;
  417. (void)memset(&state, 0, sizeof(mbstate_t));
  418. old_n = n = strlen(s);
  419. while (n > 0) {
  420. bytesconsumed = mbrtowc(&nextchar, s, n, &state);
  421. if (bytesconsumed == (size_t)(-1) ||
  422. bytesconsumed == (size_t)(-2)) {
  423. /* Something went wrong, return something reasonable */
  424. return old_n;
  425. }
  426. if (s[0] == '\n') {
  427. /*
  428. * do what strlen() would do, so that caller
  429. * is always right
  430. */
  431. width++;
  432. } else
  433. width += wcwidth(nextchar);
  434. s += bytesconsumed, n -= bytesconsumed;
  435. }
  436. return width;
  437. #else
  438. return strlen(s);
  439. #endif
  440. }
  441. private void
  442. usage(void)
  443. {
  444. (void)fprintf(stderr, USAGE, progname, progname);
  445. #ifdef HAVE_GETOPT_LONG
  446. (void)fputs("Try `file --help' for more information.\n", stderr);
  447. #endif
  448. exit(1);
  449. }
  450. #ifdef HAVE_GETOPT_LONG
  451. private void
  452. help(void)
  453. {
  454. puts(
  455. "Usage: file [OPTION]... [FILE]...\n"
  456. "Determine file type of FILEs.\n"
  457. "\n"
  458. " -m, --magic-file LIST use LIST as a colon-separated list of magic\n"
  459. " number files\n"
  460. " -z, --uncompress try to look inside compressed files\n"
  461. " -b, --brief do not prepend filenames to output lines\n"
  462. " -c, --checking-printout print the parsed form of the magic file, use in\n"
  463. " conjunction with -m to debug a new magic file\n"
  464. " before installing it\n"
  465. " -f, --files-from FILE read the filenames to be examined from FILE\n"
  466. " -F, --separator string use string as separator instead of `:'\n"
  467. " -i, --mime output mime type strings\n"
  468. " -k, --keep-going don't stop at the first match\n"
  469. " -L, --dereference causes symlinks to be followed\n"
  470. " -n, --no-buffer do not buffer output\n"
  471. " -N, --no-pad do not pad output\n"
  472. " -p, --preserve-date preserve access times on files\n"
  473. " -r, --raw don't translate unprintable chars to \\ooo\n"
  474. " -s, --special-files treat special (block/char devices) files as\n"
  475. " ordinary ones\n"
  476. " --help display this help and exit\n"
  477. " --version output version information and exit\n"
  478. );
  479. exit(0);
  480. }
  481. #endif