file.c 8.1 KB

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