file.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*
  2. * file - find type of a file or files - main program.
  3. *
  4. * Copyright (c) Ian F. Darwin, 1987.
  5. * Written by Ian F. Darwin.
  6. *
  7. * This software is not subject to any license of the American Telephone
  8. * and Telegraph Company or of the Regents of the University of California.
  9. *
  10. * Permission is granted to anyone to use this software for any purpose on
  11. * any computer system, and to alter it and redistribute it freely, subject
  12. * to the following restrictions:
  13. *
  14. * 1. The author is not responsible for the consequences of use of this
  15. * software, no matter how awful, even if they arise from flaws in it.
  16. *
  17. * 2. The origin of this software must not be misrepresented, either by
  18. * explicit claim or by omission. Since few users ever read sources,
  19. * credits must appear in the documentation.
  20. *
  21. * 3. Altered versions must be plainly marked as such, and must not be
  22. * misrepresented as being the original software. Since few users
  23. * ever read sources, credits must appear in the documentation.
  24. *
  25. * 4. This notice may not be removed or altered.
  26. */
  27. #if 0
  28. static char *moduleid =
  29. "@(#)$Id: file.c,v 1.39 1998/02/15 23:18:53 christos Exp $";
  30. #endif /* lint */
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <sys/types.h>
  35. #include <sys/param.h> /* for MAXPATHLEN */
  36. #include <sys/stat.h>
  37. #include <fcntl.h> /* for open() */
  38. #if (__COHERENT__ >= 0x420)
  39. # include <sys/utime.h>
  40. #else
  41. # ifdef USE_UTIMES
  42. # include <sys/time.h>
  43. # else
  44. # include <utime.h>
  45. # endif
  46. #endif
  47. #include <unistd.h> /* for read() */
  48. #include <getopt.h> /* for long options (is this portable?)*/
  49. #include <netinet/in.h> /* for byte swapping */
  50. #include "patchlevel.h"
  51. #include "file.h"
  52. #ifdef S_IFLNK
  53. # define USAGE "Usage: %s [-vbczL] [-f namefile] [-m magicfiles] file...\n"
  54. #else
  55. # define USAGE "Usage: %s [-vbcz] [-f namefile] [-m magicfiles] file...\n"
  56. #endif
  57. #ifndef MAGIC
  58. # define MAGIC "/etc/magic"
  59. #endif
  60. int /* Global command-line options */
  61. debug = 0, /* debugging */
  62. lflag = 0, /* follow Symlinks (BSD only) */
  63. bflag = 0, /* brief output format */
  64. zflag = 0; /* follow (uncompress) compressed files */
  65. int /* Misc globals */
  66. nmagic = 0; /* number of valid magic[]s */
  67. struct magic *magic; /* array of magic entries */
  68. const char *magicfile; /* where magic be found */
  69. char *progname; /* used throughout */
  70. int lineno; /* line number in the magic file */
  71. static void unwrap __P((char *fn));
  72. #if 0
  73. static int byteconv4 __P((int, int, int));
  74. static short byteconv2 __P((int, int, int));
  75. #endif
  76. void help()
  77. {
  78. puts(
  79. "Usage: file [OPTION]... [FILE]...
  80. Determine file type of FILEs.
  81. -m, --magic-file LIST use LIST as a colon-separated list of magic
  82. number files
  83. -z, --uncompress try to look inside compressed files
  84. -b, --brief do not prepend filenames to output lines
  85. -c, --checking-printout print the parsed form of the magic file, use in
  86. conjunction with -m to debug a new magic file
  87. before installing it
  88. -f, --files-from FILE read the filenames to be examined from FILE
  89. -L, --dereference causes symlinks to be followed
  90. --help display this help and exit
  91. --version output version information and exit");
  92. exit(0);
  93. }
  94. /*
  95. * main - parse arguments and handle options
  96. */
  97. int
  98. main(argc, argv)
  99. int argc;
  100. char *argv[];
  101. {
  102. int c,longindex;
  103. int check = 0, didsomefiles = 0, errflg = 0, ret = 0, app = 0;
  104. static struct option long_options[] =
  105. {
  106. {"version", 0, 0, 'v'},
  107. {"help", 0, 0, 0},
  108. {"brief", 0, 0, 'b'},
  109. {"checking-printout", 0, 0, 'c'},
  110. {"debug", 0, 0, 'd'},
  111. {"files-from", 1, 0, 'f'},
  112. #ifdef S_IFLNK
  113. {"dereference", 0, 0, 'L'},
  114. #endif
  115. {"magic-file", 1, 0, 'm'},
  116. {"uncompress", 0, 0, 'z'},
  117. {0, 0, 0, 0},
  118. };
  119. if ((progname = strrchr(argv[0], '/')) != NULL)
  120. progname++;
  121. else
  122. progname = argv[0];
  123. if (!(magicfile = getenv("MAGIC")))
  124. magicfile = MAGIC;
  125. while ((c = getopt_long(argc, argv, "vbcdf:Lm:z",long_options,&longindex)) != EOF)
  126. switch (c) {
  127. case 0 :
  128. if (longindex==1)
  129. help();
  130. break;
  131. case 'v':
  132. (void) fprintf(stdout, "%s-%d.%d\n", progname,
  133. FILE_VERSION_MAJOR, patchlevel);
  134. return 1;
  135. case 'b':
  136. ++bflag;
  137. break;
  138. case 'c':
  139. ++check;
  140. break;
  141. case 'd':
  142. ++debug;
  143. break;
  144. case 'f':
  145. if (!app) {
  146. ret = apprentice(magicfile, check);
  147. if (check)
  148. exit(ret);
  149. app = 1;
  150. }
  151. unwrap(optarg);
  152. ++didsomefiles;
  153. break;
  154. #ifdef S_IFLNK
  155. case 'L':
  156. ++lflag;
  157. break;
  158. #endif
  159. case 'm':
  160. magicfile = optarg;
  161. break;
  162. case 'z':
  163. zflag++;
  164. break;
  165. case '?':
  166. default:
  167. errflg++;
  168. break;
  169. }
  170. if (errflg) {
  171. (void) fprintf(stderr, USAGE, progname);
  172. fputs("Try `file --help' for more information.\n",stderr);
  173. exit(2);
  174. }
  175. if (!app) {
  176. ret = apprentice(magicfile, check);
  177. if (check)
  178. exit(ret);
  179. app = 1;
  180. }
  181. if (optind == argc) {
  182. if (!didsomefiles) {
  183. (void)fprintf(stderr, USAGE, progname);
  184. exit(2);
  185. }
  186. }
  187. else {
  188. int i, wid, nw;
  189. for (wid = 0, i = optind; i < argc; i++) {
  190. nw = strlen(argv[i]);
  191. if (nw > wid)
  192. wid = nw;
  193. }
  194. for (; optind < argc; optind++)
  195. process(argv[optind], wid);
  196. }
  197. return 0;
  198. }
  199. /*
  200. * unwrap -- read a file of filenames, do each one.
  201. */
  202. static void
  203. unwrap(fn)
  204. char *fn;
  205. {
  206. char buf[MAXPATHLEN];
  207. FILE *f;
  208. int wid = 0, cwid;
  209. if (strcmp("-", fn) == 0) {
  210. f = stdin;
  211. wid = 1;
  212. } else {
  213. if ((f = fopen(fn, "r")) == NULL) {
  214. error("Cannot open `%s' (%m).\n", fn);
  215. /*NOTREACHED*/
  216. }
  217. while (fgets(buf, MAXPATHLEN, f) != NULL) {
  218. cwid = strlen(buf) - 1;
  219. if (cwid > wid)
  220. wid = cwid;
  221. }
  222. rewind(f);
  223. }
  224. while (fgets(buf, MAXPATHLEN, f) != NULL) {
  225. buf[strlen(buf)-1] = '\0';
  226. process(buf, wid);
  227. }
  228. (void) fclose(f);
  229. }
  230. #if 0
  231. /*
  232. * byteconv4
  233. * Input:
  234. * from 4 byte quantity to convert
  235. * same whether to perform byte swapping
  236. * big_endian whether we are a big endian host
  237. */
  238. static int
  239. byteconv4(from, same, big_endian)
  240. int from;
  241. int same;
  242. int big_endian;
  243. {
  244. if (same)
  245. return from;
  246. else if (big_endian) /* lsb -> msb conversion on msb */
  247. {
  248. union {
  249. int i;
  250. char c[4];
  251. } retval, tmpval;
  252. tmpval.i = from;
  253. retval.c[0] = tmpval.c[3];
  254. retval.c[1] = tmpval.c[2];
  255. retval.c[2] = tmpval.c[1];
  256. retval.c[3] = tmpval.c[0];
  257. return retval.i;
  258. }
  259. else
  260. return ntohl(from); /* msb -> lsb conversion on lsb */
  261. }
  262. /*
  263. * byteconv2
  264. * Same as byteconv4, but for shorts
  265. */
  266. static short
  267. byteconv2(from, same, big_endian)
  268. int from;
  269. int same;
  270. int big_endian;
  271. {
  272. if (same)
  273. return from;
  274. else if (big_endian) /* lsb -> msb conversion on msb */
  275. {
  276. union {
  277. short s;
  278. char c[2];
  279. } retval, tmpval;
  280. tmpval.s = (short) from;
  281. retval.c[0] = tmpval.c[1];
  282. retval.c[1] = tmpval.c[0];
  283. return retval.s;
  284. }
  285. else
  286. return ntohs(from); /* msb -> lsb conversion on lsb */
  287. }
  288. #endif
  289. /*
  290. * process - process input file
  291. */
  292. void
  293. process(inname, wid)
  294. const char *inname;
  295. int wid;
  296. {
  297. int fd = 0;
  298. static const char stdname[] = "standard input";
  299. unsigned char buf[HOWMANY+1]; /* one extra for terminating '\0' */
  300. struct stat sb;
  301. int nbytes = 0; /* number of bytes read from a datafile */
  302. char match = '\0';
  303. if (strcmp("-", inname) == 0) {
  304. if (fstat(0, &sb)<0) {
  305. error("cannot fstat `%s' (%m).\n", stdname);
  306. /*NOTREACHED*/
  307. }
  308. inname = stdname;
  309. }
  310. if (wid > 0 && !bflag)
  311. (void) printf("%s:%*s ", inname,
  312. (int) (wid - strlen(inname)), "");
  313. if (inname != stdname) {
  314. /*
  315. * first try judging the file based on its filesystem status
  316. */
  317. if (fsmagic(inname, &sb) != 0) {
  318. putchar('\n');
  319. return;
  320. }
  321. if ((fd = open(inname, O_RDONLY)) < 0) {
  322. /* We can't open it, but we were able to stat it. */
  323. if (sb.st_mode & 0002) ckfputs("writeable, ", stdout);
  324. if (sb.st_mode & 0111) ckfputs("executable, ", stdout);
  325. ckfprintf(stdout, "can't read `%s' (%m).\n",
  326. inname);
  327. return;
  328. }
  329. }
  330. /*
  331. * try looking at the first HOWMANY bytes
  332. */
  333. if ((nbytes = read(fd, (char *)buf, HOWMANY)) == -1) {
  334. error("read failed (%m).\n");
  335. /*NOTREACHED*/
  336. }
  337. if (nbytes == 0)
  338. ckfputs("empty", stdout);
  339. else {
  340. buf[nbytes++] = '\0'; /* null-terminate it */
  341. match = tryit(buf, nbytes, zflag);
  342. }
  343. #ifdef BUILTIN_ELF
  344. if (match == 's' && nbytes > 5)
  345. tryelf(fd, buf, nbytes);
  346. #endif
  347. if (inname != stdname) {
  348. #ifdef RESTORE_TIME
  349. /*
  350. * Try to restore access, modification times if read it.
  351. */
  352. # ifdef USE_UTIMES
  353. struct timeval utsbuf[2];
  354. utsbuf[0].tv_sec = sb.st_atime;
  355. utsbuf[1].tv_sec = sb.st_mtime;
  356. (void) utimes(inname, utsbuf); /* don't care if loses */
  357. # else
  358. struct utimbuf utbuf;
  359. utbuf.actime = sb.st_atime;
  360. utbuf.modtime = sb.st_mtime;
  361. (void) utime(inname, &utbuf); /* don't care if loses */
  362. # endif
  363. #endif
  364. (void) close(fd);
  365. }
  366. (void) putchar('\n');
  367. }
  368. int
  369. tryit(buf, nb, zfl)
  370. unsigned char *buf;
  371. int nb, zfl;
  372. {
  373. /* try compression stuff */
  374. if (zfl && zmagic(buf, nb))
  375. return 'z';
  376. /* try tests in /etc/magic (or surrogate magic file) */
  377. if (softmagic(buf, nb))
  378. return 's';
  379. /* try known keywords, check whether it is ASCII */
  380. if (ascmagic(buf, nb))
  381. return 'a';
  382. /* see if it's international language text */
  383. if (internatmagic(buf, nb))
  384. return 'i';
  385. /* abandon hope, all ye who remain here */
  386. ckfputs("data", stdout);
  387. return '\0';
  388. }