file.c 9.6 KB

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