file.c 8.9 KB

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