file.c 8.3 KB

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