file.c 9.8 KB

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