file.c 11 KB

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