file.c 8.6 KB

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