file.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. #ifndef lint
  28. static char *moduleid =
  29. "@(#)$Id: file.c,v 1.35 1996/06/22 22:04:22 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. #include <utime.h>
  42. #endif
  43. #include <unistd.h> /* for read() */
  44. #include <getopt.h> /* for long options (is this portable?)*/
  45. #include "readelf.h"
  46. #include <netinet/in.h> /* for byte swapping */
  47. #include "patchlevel.h"
  48. #include "file.h"
  49. #ifdef S_IFLNK
  50. # define USAGE "Usage: %s [-vczL] [-f namefile] [-m magicfiles] file...\n"
  51. #else
  52. # define USAGE "Usage: %s [-vcz] [-f namefile] [-m magicfiles] file...\n"
  53. #endif
  54. #ifndef MAGIC
  55. # define MAGIC "/etc/magic"
  56. #endif
  57. int /* Global command-line options */
  58. debug = 0, /* debugging */
  59. lflag = 0, /* follow Symlinks (BSD only) */
  60. zflag = 0; /* follow (uncompress) compressed files */
  61. int /* Misc globals */
  62. nmagic = 0; /* number of valid magic[]s */
  63. struct magic *magic; /* array of magic entries */
  64. char *magicfile; /* where magic be found */
  65. char *progname; /* used throughout */
  66. int lineno; /* line number in the magic file */
  67. static void unwrap __P((char *fn));
  68. static int byteconv4 __P((int, int, int));
  69. static short byteconv2 __P((int, int, int));
  70. /*
  71. * main - parse arguments and handle options
  72. */
  73. int
  74. main(argc, argv)
  75. int argc;
  76. char *argv[];
  77. {
  78. int c;
  79. int check = 0, didsomefiles = 0, errflg = 0, ret = 0, app = 0;
  80. static struct option long_options[] =
  81. {
  82. {"version", 0, 0, 'v'},
  83. {"checking-printout", 0, 0, 'c'},
  84. {"debug", 0, 0, 'd'},
  85. {"files-from", 1, 0, 'f'},
  86. #ifdef S_IFLNK
  87. {"dereference", 0, 0, 'L'},
  88. #endif
  89. {"magic-file", 1, 0, 'm'},
  90. {"uncompress", 0, 0, 'z'},
  91. {0, 0, 0, 0},
  92. };
  93. if ((progname = strrchr(argv[0], '/')) != NULL)
  94. progname++;
  95. else
  96. progname = argv[0];
  97. if (!(magicfile = getenv("MAGIC")))
  98. magicfile = MAGIC;
  99. while ((c = getopt_long(argc, argv, "vcdf:Lm:z",long_options,NULL)) != EOF)
  100. switch (c) {
  101. case 'v':
  102. (void) fprintf(stdout, "%s-%d.%d\n", progname,
  103. FILE_VERSION_MAJOR, patchlevel);
  104. return 1;
  105. case 'c':
  106. ++check;
  107. break;
  108. case 'd':
  109. ++debug;
  110. break;
  111. case 'f':
  112. if (!app) {
  113. ret = apprentice(magicfile, check);
  114. if (check)
  115. exit(ret);
  116. app = 1;
  117. }
  118. unwrap(optarg);
  119. ++didsomefiles;
  120. break;
  121. #ifdef S_IFLNK
  122. case 'L':
  123. ++lflag;
  124. break;
  125. #endif
  126. case 'm':
  127. magicfile = optarg;
  128. break;
  129. case 'z':
  130. zflag++;
  131. break;
  132. case '?':
  133. default:
  134. errflg++;
  135. break;
  136. }
  137. if (errflg) {
  138. (void) fprintf(stderr, USAGE, progname);
  139. exit(2);
  140. }
  141. if (!app) {
  142. ret = apprentice(magicfile, check);
  143. if (check)
  144. exit(ret);
  145. app = 1;
  146. }
  147. if (optind == argc) {
  148. if (!didsomefiles) {
  149. (void)fprintf(stderr, USAGE, progname);
  150. exit(2);
  151. }
  152. }
  153. else {
  154. int i, wid, nw;
  155. for (wid = 0, i = optind; i < argc; i++) {
  156. nw = strlen(argv[i]);
  157. if (nw > wid)
  158. wid = nw;
  159. }
  160. for (; optind < argc; optind++)
  161. process(argv[optind], wid);
  162. }
  163. return 0;
  164. }
  165. /*
  166. * unwrap -- read a file of filenames, do each one.
  167. */
  168. static void
  169. unwrap(fn)
  170. char *fn;
  171. {
  172. char buf[MAXPATHLEN];
  173. FILE *f;
  174. int wid = 0, cwid;
  175. if (strcmp("-", fn) == 0) {
  176. f = stdin;
  177. wid = 1;
  178. } else {
  179. if ((f = fopen(fn, "r")) == NULL) {
  180. error("Cannot open `%s' (%s).\n", fn, strerror(errno));
  181. /*NOTREACHED*/
  182. }
  183. while (fgets(buf, MAXPATHLEN, f) != NULL) {
  184. cwid = strlen(buf) - 1;
  185. if (cwid > wid)
  186. wid = cwid;
  187. }
  188. rewind(f);
  189. }
  190. while (fgets(buf, MAXPATHLEN, f) != NULL) {
  191. buf[strlen(buf)-1] = '\0';
  192. process(buf, wid);
  193. }
  194. (void) fclose(f);
  195. }
  196. /*
  197. * byteconv4
  198. * Input:
  199. * from 4 byte quantity to convert
  200. * same whether to perform byte swapping
  201. * big_endian whether we are a big endian host
  202. */
  203. static int
  204. byteconv4(from, same, big_endian)
  205. int from;
  206. int same;
  207. int big_endian;
  208. {
  209. if (same)
  210. return from;
  211. else if (big_endian) /* lsb -> msb conversion on msb */
  212. {
  213. union {
  214. int i;
  215. char c[4];
  216. } retval, tmpval;
  217. tmpval.i = from;
  218. retval.c[0] = tmpval.c[3];
  219. retval.c[1] = tmpval.c[2];
  220. retval.c[2] = tmpval.c[1];
  221. retval.c[3] = tmpval.c[0];
  222. return retval.i;
  223. }
  224. else
  225. return ntohl(from); /* msb -> lsb conversion on lsb */
  226. }
  227. /*
  228. * byteconv2
  229. * Same as byteconv4, but for shorts
  230. */
  231. static short
  232. byteconv2(from, same, big_endian)
  233. int from;
  234. int same;
  235. int big_endian;
  236. {
  237. if (same)
  238. return from;
  239. else if (big_endian) /* lsb -> msb conversion on msb */
  240. {
  241. union {
  242. short s;
  243. char c[2];
  244. } retval, tmpval;
  245. tmpval.s = (short) from;
  246. retval.c[0] = tmpval.c[1];
  247. retval.c[1] = tmpval.c[0];
  248. return retval.s;
  249. }
  250. else
  251. return ntohs(from); /* msb -> lsb conversion on lsb */
  252. }
  253. /*
  254. * process - process input file
  255. */
  256. void
  257. process(inname, wid)
  258. const char *inname;
  259. int wid;
  260. {
  261. int fd = 0;
  262. static const char stdname[] = "standard input";
  263. unsigned char buf[HOWMANY+1]; /* one extra for terminating '\0' */
  264. struct utimbuf utbuf;
  265. struct stat sb;
  266. int nbytes = 0; /* number of bytes read from a datafile */
  267. char match = '\0';
  268. if (strcmp("-", inname) == 0) {
  269. if (fstat(0, &sb)<0) {
  270. error("cannot fstat `%s' (%s).\n", stdname,
  271. strerror(errno));
  272. /*NOTREACHED*/
  273. }
  274. inname = stdname;
  275. }
  276. if (wid > 0)
  277. (void) printf("%s:%*s ", inname,
  278. (int) (wid - strlen(inname)), "");
  279. if (inname != stdname) {
  280. /*
  281. * first try judging the file based on its filesystem status
  282. */
  283. if (fsmagic(inname, &sb) != 0) {
  284. putchar('\n');
  285. return;
  286. }
  287. if ((fd = open(inname, O_RDONLY)) < 0) {
  288. /* We can't open it, but we were able to stat it. */
  289. if (sb.st_mode & 0002) ckfputs("writeable, ", stdout);
  290. if (sb.st_mode & 0111) ckfputs("executable, ", stdout);
  291. ckfprintf(stdout, "can't read `%s' (%s).\n",
  292. inname, strerror(errno));
  293. return;
  294. }
  295. }
  296. /*
  297. * try looking at the first HOWMANY bytes
  298. */
  299. if ((nbytes = read(fd, (char *)buf, HOWMANY)) == -1) {
  300. error("read failed (%s).\n", strerror(errno));
  301. /*NOTREACHED*/
  302. }
  303. if (nbytes == 0)
  304. ckfputs("empty", stdout);
  305. else {
  306. buf[nbytes++] = '\0'; /* null-terminate it */
  307. match = tryit(buf, nbytes, zflag);
  308. }
  309. /*
  310. * ELF executables have multiple section headers in arbitrary
  311. * file locations and thus file(1) cannot determine it from easily.
  312. * Instead we traverse thru all section headers until a symbol table
  313. * one is found or else the binary is stripped.
  314. */
  315. if (match == 's' && nbytes > sizeof (Elf32_Ehdr) &&
  316. buf[EI_MAG0] == ELFMAG0 &&
  317. buf[EI_MAG1] == ELFMAG1 &&
  318. buf[EI_MAG2] == ELFMAG2 &&
  319. buf[EI_MAG3] == ELFMAG3) {
  320. union {
  321. int l;
  322. char c[sizeof (int)];
  323. } u;
  324. Elf32_Ehdr elfhdr;
  325. int stripped = 1;
  326. int be,same;
  327. short shnum;
  328. u.l = 1;
  329. (void) memcpy(&elfhdr, buf, sizeof elfhdr);
  330. /*
  331. * If the system byteorder does not equal the object byteorder
  332. * then need to do byte-swapping.
  333. */
  334. be = u.c[sizeof(int) - 1] == 1; /* are we big endian? */
  335. same = (u.c[sizeof(int) - 1] + 1) == elfhdr.e_ident[5];
  336. /* are we the same endianness? */;
  337. if (lseek(fd, byteconv4(elfhdr.e_shoff,same,be), SEEK_SET)<0)
  338. error("lseek failed (%s).\n", strerror(errno));
  339. for (shnum = byteconv2(elfhdr.e_shnum,same,be);
  340. shnum; shnum--) {
  341. if (read(fd, buf,
  342. byteconv2(elfhdr.e_shentsize,same,be))<0)
  343. error("read failed (%s).\n", strerror(errno));
  344. if (byteconv4(((Elf32_Shdr *)buf)->sh_type,same,be)
  345. == SHT_SYMTAB) {
  346. stripped = 0;
  347. break;
  348. }
  349. }
  350. if (stripped)
  351. (void) printf (", stripped");
  352. else
  353. (void) printf (", not stripped");
  354. }
  355. if (inname != stdname) {
  356. /*
  357. * Try to restore access, modification times if read it.
  358. */
  359. utbuf.actime = sb.st_atime;
  360. utbuf.modtime = sb.st_mtime;
  361. (void) utime(inname, &utbuf); /* don't care if loses */
  362. (void) close(fd);
  363. }
  364. (void) putchar('\n');
  365. }
  366. int
  367. tryit(buf, nb, zflag)
  368. unsigned char *buf;
  369. int nb, zflag;
  370. {
  371. /* try compression stuff */
  372. if (zflag && zmagic(buf, nb))
  373. return 'z';
  374. /* try tests in /etc/magic (or surrogate magic file) */
  375. if (softmagic(buf, nb))
  376. return 's';
  377. /* try known keywords, check whether it is ASCII */
  378. if (ascmagic(buf, nb))
  379. return 'a';
  380. /* abandon hope, all ye who remain here */
  381. ckfputs("data", stdout);
  382. return '\0';
  383. }