2 * file - find type of a file or files - main program.
4 * Copyright (c) Ian F. Darwin, 1987.
5 * Written by Ian F. Darwin.
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.
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:
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.
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.
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.
25 * 4. This notice may not be removed or altered.
29 static const char rcsid[] =
34 #include <fcntl.h> /* for open() */
37 #include <sys/param.h> /* for MAXPATHLEN */
39 #if (__COHERENT__ >= 0x420)
40 # include <sys/utime.h>
43 # include <sys/time.h>
48 #include <unistd.h> /* for read() */
50 #include "patchlevel.h"
54 # define USAGE "usage: file [-vczL] [-f namefile] [-m magicfiles] file...\n"
56 # define USAGE "usage: file [-vcz] [-f namefile] [-m magicfiles] file...\n"
60 # define MAGIC "/etc/magic"
63 int /* Global command-line options */
64 debug = 0, /* debugging */
65 lflag = 0, /* follow Symlinks (BSD only) */
66 zflag = 0; /* follow (uncompress) compressed files */
68 int /* Misc globals */
69 nmagic = 0; /* number of valid magic[]s */
71 struct magic *magic; /* array of magic entries */
73 char *magicfile; /* where magic be found */
75 int lineno; /* line number in the magic file */
78 static void unwrap __P((char *fn));
79 static void usage __P((void));
81 static int byteconv4 __P((int, int, int));
82 static short byteconv2 __P((int, int, int));
86 * main - parse arguments and handle options
94 int check = 0, didsomefiles = 0, errflg = 0, ret = 0, app = 0;
96 if (!(magicfile = getenv("MAGIC")))
99 while ((c = getopt(argc, argv, "vcdf:Lm:z")) != -1)
102 (void) fprintf(stdout, "file-%d.%d\n",
103 FILE_VERSION_MAJOR, patchlevel);
113 ret = apprentice(magicfile, check);
142 ret = apprentice(magicfile, check);
148 if (optind == argc) {
154 for (wid = 0, i = optind; i < argc; i++) {
155 nw = strlen(argv[i]);
159 for (; optind < argc; optind++)
160 process(argv[optind], wid);
169 fprintf(stderr, USAGE);
174 * unwrap -- read a file of filenames, do each one.
180 char buf[MAXPATHLEN];
184 if (strcmp("-", fn) == 0) {
188 if ((f = fopen(fn, "r")) == NULL) {
189 err(1, "cannot open `%s'", fn);
193 while (fgets(buf, MAXPATHLEN, f) != NULL) {
194 cwid = strlen(buf) - 1;
202 while (fgets(buf, MAXPATHLEN, f) != NULL) {
203 buf[strlen(buf)-1] = '\0';
215 * from 4 byte quantity to convert
216 * same whether to perform byte swapping
217 * big_endian whether we are a big endian host
220 byteconv4(from, same, big_endian)
227 else if (big_endian) /* lsb -> msb conversion on msb */
235 retval.c[0] = tmpval.c[3];
236 retval.c[1] = tmpval.c[2];
237 retval.c[2] = tmpval.c[1];
238 retval.c[3] = tmpval.c[0];
243 return ntohl(from); /* msb -> lsb conversion on lsb */
248 * Same as byteconv4, but for shorts
251 byteconv2(from, same, big_endian)
258 else if (big_endian) /* lsb -> msb conversion on msb */
265 tmpval.s = (short) from;
266 retval.c[0] = tmpval.c[1];
267 retval.c[1] = tmpval.c[0];
272 return ntohs(from); /* msb -> lsb conversion on lsb */
277 * process - process input file
285 static const char stdname[] = "standard input";
286 unsigned char buf[HOWMANY+1]; /* one extra for terminating '\0' */
288 int nbytes = 0; /* number of bytes read from a datafile */
291 if (strcmp("-", inname) == 0) {
292 if (fstat(0, &sb)<0) {
293 err(1, "cannot fstat `%s'", stdname);
300 (void) printf("%s:%*s ", inname,
301 (int) (wid - strlen(inname)), "");
303 if (inname != stdname) {
305 * first try judging the file based on its filesystem status
307 if (fsmagic(inname, &sb) != 0) {
312 if ((fd = open(inname, O_RDONLY)) < 0) {
313 /* We can't open it, but we were able to stat it. */
314 if (sb.st_mode & 0002) ckfputs("writeable, ", stdout);
315 if (sb.st_mode & 0111) ckfputs("executable, ", stdout);
316 ckfprintf(stdout, "can't read `%s': %s.\n",
317 inname, strerror(errno));
324 * try looking at the first HOWMANY bytes
326 if ((nbytes = read(fd, (char *)buf, HOWMANY)) == -1) {
327 err(1, "read failed");
332 ckfputs("empty", stdout);
334 buf[nbytes++] = '\0'; /* null-terminate it */
335 match = tryit(buf, nbytes, zflag);
339 if (match == 's' && nbytes > 5)
340 tryelf(fd, buf, nbytes);
343 if (inname != stdname) {
346 * Try to restore access, modification times if read it.
349 struct timeval utsbuf[2];
350 utsbuf[0].tv_sec = sb.st_atime;
351 utsbuf[1].tv_sec = sb.st_mtime;
353 (void) utimes(inname, utsbuf); /* don't care if loses */
355 struct utimbuf utbuf;
357 utbuf.actime = sb.st_atime;
358 utbuf.modtime = sb.st_mtime;
359 (void) utime(inname, &utbuf); /* don't care if loses */
364 (void) putchar('\n');
369 tryit(buf, nb, lzflag)
373 /* try compression stuff */
374 if (lzflag && zmagic(buf, nb))
377 /* try tests in /etc/magic (or surrogate magic file) */
378 if (softmagic(buf, nb))
381 /* try known keywords, check whether it is ASCII */
382 if (ascmagic(buf, nb))
385 /* see if it's international language text */
386 if (internatmagic(buf, nb))
389 /* abandon hope, all ye who remain here */
390 ckfputs("data", stdout);