file.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. #ifndef lint
  33. FILE_RCSID("@(#)$File: file.c,v 1.131 2009/02/13 18:48:05 christos Exp $")
  34. #endif /* lint */
  35. #include "magic.h"
  36. #include <stdlib.h>
  37. #include <unistd.h>
  38. #include <string.h>
  39. #ifdef RESTORE_TIME
  40. # if (__COHERENT__ >= 0x420)
  41. # include <sys/utime.h>
  42. # else
  43. # ifdef USE_UTIMES
  44. # include <sys/time.h>
  45. # else
  46. # include <utime.h>
  47. # endif
  48. # endif
  49. #endif
  50. #ifdef HAVE_UNISTD_H
  51. #include <unistd.h> /* for read() */
  52. #endif
  53. #ifdef HAVE_LOCALE_H
  54. #include <locale.h>
  55. #endif
  56. #ifdef HAVE_WCHAR_H
  57. #include <wchar.h>
  58. #endif
  59. #if defined(HAVE_GETOPT_H) && defined(HAVE_STRUCT_OPTION)
  60. #include <getopt.h>
  61. #else
  62. #include "mygetopt.h"
  63. #endif
  64. #ifndef HAVE_GETOPT_LONG
  65. int getopt_long(int argc, char * const *argv, const char *optstring, const struct option *longopts, int *longindex);
  66. #endif
  67. #include <netinet/in.h> /* for byte swapping */
  68. #include "patchlevel.h"
  69. #ifdef S_IFLNK
  70. #define SYMLINKFLAG "Lh"
  71. #else
  72. #define SYMLINKFLAG ""
  73. #endif
  74. # define USAGE "Usage: %s [-bcik" SYMLINKFLAG "nNrsvz0] [-e test] [-f namefile] [-F separator] [-m magicfiles] file...\n %s -C -m magicfiles\n"
  75. #ifndef MAXPATHLEN
  76. #define MAXPATHLEN 1024
  77. #endif
  78. private int /* Global command-line options */
  79. bflag = 0, /* brief output format */
  80. nopad = 0, /* Don't pad output */
  81. nobuffer = 0, /* Do not buffer stdout */
  82. nulsep = 0; /* Append '\0' to the separator */
  83. private const char *default_magicfile = MAGIC;
  84. private const char *separator = ":"; /* Default field separator */
  85. private const char hmagic[] = "/.magic";
  86. private const struct option long_options[] = {
  87. #define OPT(shortname, longname, opt, doc) \
  88. {longname, opt, NULL, shortname},
  89. #define OPT_LONGONLY(longname, opt, doc) \
  90. {longname, opt, NULL, 0},
  91. #include "file_opts.h"
  92. #undef OPT
  93. #undef OPT_LONGONLY
  94. {0, 0, NULL, 0}
  95. };
  96. #define OPTSTRING "bcCde:f:F:hikLm:nNprsvz0"
  97. private const struct {
  98. const char *name;
  99. int value;
  100. } nv[] = {
  101. { "apptype", MAGIC_NO_CHECK_APPTYPE },
  102. { "ascii", MAGIC_NO_CHECK_ASCII },
  103. { "cdf", MAGIC_NO_CHECK_CDF },
  104. { "compress", MAGIC_NO_CHECK_COMPRESS },
  105. { "elf", MAGIC_NO_CHECK_ELF },
  106. { "encoding", MAGIC_NO_CHECK_ENCODING },
  107. { "soft", MAGIC_NO_CHECK_SOFT },
  108. { "tar", MAGIC_NO_CHECK_TAR },
  109. { "tokens", MAGIC_NO_CHECK_TOKENS },
  110. };
  111. private char *progname; /* used throughout */
  112. private void usage(void);
  113. private void help(void);
  114. int main(int, char *[]);
  115. private int unwrap(struct magic_set *, const char *);
  116. private int process(struct magic_set *ms, const char *, int);
  117. private struct magic_set *load(const char *, int);
  118. /*
  119. * main - parse arguments and handle options
  120. */
  121. int
  122. main(int argc, char *argv[])
  123. {
  124. int c;
  125. size_t i;
  126. int action = 0, didsomefiles = 0, errflg = 0;
  127. int flags = 0, e = 0;
  128. char *home, *usermagic;
  129. struct magic_set *magic = NULL;
  130. char magicpath[2 * MAXPATHLEN + 2];
  131. int longindex;
  132. const char *magicfile; /* where the magic is */
  133. /* makes islower etc work for other langs */
  134. (void)setlocale(LC_CTYPE, "");
  135. #ifdef __EMX__
  136. /* sh-like wildcard expansion! Shouldn't hurt at least ... */
  137. _wildcard(&argc, &argv);
  138. #endif
  139. if ((progname = strrchr(argv[0], '/')) != NULL)
  140. progname++;
  141. else
  142. progname = argv[0];
  143. magicfile = default_magicfile;
  144. if ((usermagic = getenv("MAGIC")) != NULL)
  145. magicfile = usermagic;
  146. else
  147. if ((home = getenv("HOME")) != NULL) {
  148. (void)snprintf(magicpath, sizeof(magicpath), "%s%s",
  149. home, hmagic);
  150. if (access(magicpath, R_OK) == 0) {
  151. (void)snprintf(magicpath, sizeof(magicpath),
  152. "%s%s:%s", home, hmagic, magicfile);
  153. magicfile = magicpath;
  154. }
  155. }
  156. #ifdef S_IFLNK
  157. flags |= getenv("POSIXLY_CORRECT") ? MAGIC_SYMLINK : 0;
  158. #endif
  159. while ((c = getopt_long(argc, argv, OPTSTRING, long_options,
  160. &longindex)) != -1)
  161. switch (c) {
  162. case 0 :
  163. switch (longindex) {
  164. case 0:
  165. help();
  166. break;
  167. case 10:
  168. flags |= MAGIC_APPLE;
  169. break;
  170. case 11:
  171. flags |= MAGIC_MIME_TYPE;
  172. break;
  173. case 12:
  174. flags |= MAGIC_MIME_ENCODING;
  175. break;
  176. }
  177. break;
  178. case '0':
  179. nulsep = 1;
  180. break;
  181. case 'b':
  182. bflag++;
  183. break;
  184. case 'c':
  185. action = FILE_CHECK;
  186. break;
  187. case 'C':
  188. action = FILE_COMPILE;
  189. break;
  190. case 'd':
  191. flags |= MAGIC_DEBUG|MAGIC_CHECK;
  192. break;
  193. case 'e':
  194. for (i = 0; i < sizeof(nv) / sizeof(nv[0]); i++)
  195. if (strcmp(nv[i].name, optarg) == 0)
  196. break;
  197. if (i == sizeof(nv) / sizeof(nv[0]))
  198. errflg++;
  199. else
  200. flags |= nv[i].value;
  201. break;
  202. case 'f':
  203. if(action)
  204. usage();
  205. if (magic == NULL)
  206. if ((magic = load(magicfile, flags)) == NULL)
  207. return 1;
  208. e |= unwrap(magic, optarg);
  209. ++didsomefiles;
  210. break;
  211. case 'F':
  212. separator = optarg;
  213. break;
  214. case 'i':
  215. flags |= MAGIC_MIME;
  216. break;
  217. case 'k':
  218. flags |= MAGIC_CONTINUE;
  219. break;
  220. case 'm':
  221. magicfile = optarg;
  222. break;
  223. case 'n':
  224. ++nobuffer;
  225. break;
  226. case 'N':
  227. ++nopad;
  228. break;
  229. #if defined(HAVE_UTIME) || defined(HAVE_UTIMES)
  230. case 'p':
  231. flags |= MAGIC_PRESERVE_ATIME;
  232. break;
  233. #endif
  234. case 'r':
  235. flags |= MAGIC_RAW;
  236. break;
  237. case 's':
  238. flags |= MAGIC_DEVICES;
  239. break;
  240. case 'v':
  241. (void)fprintf(stderr, "%s-%d.%.2d\n", progname,
  242. FILE_VERSION_MAJOR, patchlevel);
  243. (void)fprintf(stderr, "magic file from %s\n",
  244. magicfile);
  245. return 1;
  246. case 'z':
  247. flags |= MAGIC_COMPRESS;
  248. break;
  249. #ifdef S_IFLNK
  250. case 'L':
  251. flags |= MAGIC_SYMLINK;
  252. break;
  253. case 'h':
  254. flags &= ~MAGIC_SYMLINK;
  255. break;
  256. #endif
  257. case '?':
  258. default:
  259. errflg++;
  260. break;
  261. }
  262. if (errflg) {
  263. usage();
  264. }
  265. if (e)
  266. return e;
  267. switch(action) {
  268. case FILE_CHECK:
  269. case FILE_COMPILE:
  270. /*
  271. * Don't try to check/compile ~/.magic unless we explicitly
  272. * ask for it.
  273. */
  274. if (magicfile == magicpath)
  275. magicfile = default_magicfile;
  276. magic = magic_open(flags|MAGIC_CHECK);
  277. if (magic == NULL) {
  278. (void)fprintf(stderr, "%s: %s\n", progname,
  279. strerror(errno));
  280. return 1;
  281. }
  282. c = action == FILE_CHECK ? magic_check(magic, magicfile) :
  283. magic_compile(magic, magicfile);
  284. if (c == -1) {
  285. (void)fprintf(stderr, "%s: %s\n", progname,
  286. magic_error(magic));
  287. return 1;
  288. }
  289. return 0;
  290. default:
  291. if (magic == NULL)
  292. if ((magic = load(magicfile, flags)) == NULL)
  293. return 1;
  294. break;
  295. }
  296. if (optind == argc) {
  297. if (!didsomefiles)
  298. usage();
  299. }
  300. else {
  301. size_t j, wid, nw;
  302. for (wid = 0, j = (size_t)optind; j < (size_t)argc; j++) {
  303. nw = file_mbswidth(argv[j]);
  304. if (nw > wid)
  305. wid = nw;
  306. }
  307. /*
  308. * If bflag is only set twice, set it depending on
  309. * number of files [this is undocumented, and subject to change]
  310. */
  311. if (bflag == 2) {
  312. bflag = optind >= argc - 1;
  313. }
  314. for (; optind < argc; optind++)
  315. e |= process(magic, argv[optind], wid);
  316. }
  317. if (magic)
  318. magic_close(magic);
  319. return e;
  320. }
  321. private struct magic_set *
  322. /*ARGSUSED*/
  323. load(const char *magicfile, int flags)
  324. {
  325. struct magic_set *magic = magic_open(flags);
  326. if (magic == NULL) {
  327. (void)fprintf(stderr, "%s: %s\n", progname, strerror(errno));
  328. return NULL;
  329. }
  330. if (magic_load(magic, magicfile) == -1) {
  331. (void)fprintf(stderr, "%s: %s\n",
  332. progname, magic_error(magic));
  333. magic_close(magic);
  334. return NULL;
  335. }
  336. return magic;
  337. }
  338. /*
  339. * unwrap -- read a file of filenames, do each one.
  340. */
  341. private int
  342. unwrap(struct magic_set *ms, const char *fn)
  343. {
  344. char buf[MAXPATHLEN];
  345. FILE *f;
  346. int wid = 0, cwid;
  347. int e = 0;
  348. if (strcmp("-", fn) == 0) {
  349. f = stdin;
  350. wid = 1;
  351. } else {
  352. if ((f = fopen(fn, "r")) == NULL) {
  353. (void)fprintf(stderr, "%s: Cannot open `%s' (%s).\n",
  354. progname, fn, strerror(errno));
  355. return 1;
  356. }
  357. while (fgets(buf, sizeof(buf), f) != NULL) {
  358. buf[strcspn(buf, "\n")] = '\0';
  359. cwid = file_mbswidth(buf);
  360. if (cwid > wid)
  361. wid = cwid;
  362. }
  363. rewind(f);
  364. }
  365. while (fgets(buf, sizeof(buf), f) != NULL) {
  366. buf[strcspn(buf, "\n")] = '\0';
  367. e |= process(ms, buf, wid);
  368. if(nobuffer)
  369. (void)fflush(stdout);
  370. }
  371. (void)fclose(f);
  372. return e;
  373. }
  374. /*
  375. * Called for each input file on the command line (or in a list of files)
  376. */
  377. private int
  378. process(struct magic_set *ms, const char *inname, int wid)
  379. {
  380. const char *type;
  381. int std_in = strcmp(inname, "-") == 0;
  382. if (wid > 0 && !bflag) {
  383. (void)printf("%s", std_in ? "/dev/stdin" : inname);
  384. if (nulsep)
  385. (void)putc('\0', stdout);
  386. else
  387. (void)printf("%s", separator);
  388. (void)printf("%*s ",
  389. (int) (nopad ? 0 : (wid - file_mbswidth(inname))), "");
  390. }
  391. type = magic_file(ms, std_in ? NULL : inname);
  392. if (type == NULL) {
  393. (void)printf("ERROR: %s\n", magic_error(ms));
  394. return 1;
  395. } else {
  396. (void)printf("%s\n", type);
  397. return 0;
  398. }
  399. }
  400. size_t
  401. file_mbswidth(const char *s)
  402. {
  403. #if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
  404. size_t bytesconsumed, old_n, n, width = 0;
  405. mbstate_t state;
  406. wchar_t nextchar;
  407. (void)memset(&state, 0, sizeof(mbstate_t));
  408. old_n = n = strlen(s);
  409. while (n > 0) {
  410. bytesconsumed = mbrtowc(&nextchar, s, n, &state);
  411. if (bytesconsumed == (size_t)(-1) ||
  412. bytesconsumed == (size_t)(-2)) {
  413. /* Something went wrong, return something reasonable */
  414. return old_n;
  415. }
  416. if (s[0] == '\n') {
  417. /*
  418. * do what strlen() would do, so that caller
  419. * is always right
  420. */
  421. width++;
  422. } else
  423. width += wcwidth(nextchar);
  424. s += bytesconsumed, n -= bytesconsumed;
  425. }
  426. return width;
  427. #else
  428. return strlen(s);
  429. #endif
  430. }
  431. private void
  432. usage(void)
  433. {
  434. (void)fprintf(stderr, USAGE, progname, progname);
  435. (void)fputs("Try `file --help' for more information.\n", stderr);
  436. exit(1);
  437. }
  438. private void
  439. help(void)
  440. {
  441. (void)fputs(
  442. "Usage: file [OPTION...] [FILE...]\n"
  443. "Determine type of FILEs.\n"
  444. "\n", stderr);
  445. #define OPT(shortname, longname, opt, doc) \
  446. fprintf(stderr, " -%c, --" longname doc, shortname);
  447. #define OPT_LONGONLY(longname, opt, doc) \
  448. fprintf(stderr, " --" longname doc);
  449. #include "file_opts.h"
  450. #undef OPT
  451. #undef OPT_LONGONLY
  452. exit(0);
  453. }