#ifndef lint
#if 0
static char *moduleid = "$OpenBSD: file.c,v 1.5 1997/02/09 23:58:22 millert Exp $";
#endif
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <fcntl.h>
#if (__COHERENT__ >= 0x420)
# include <sys/utime.h>
#else
# ifdef USE_UTIMES
# include <sys/time.h>
# else
# include <utime.h>
# endif
#endif
#include <unistd.h>
#include <netinet/in.h>
#include "patchlevel.h"
#include "file.h"
#ifdef BUILTIN_FAT
#include <mach-o/fat.h>
#endif
#ifdef S_IFLNK
# define USAGE "Usage: %s [-vczL] [-f namefile] [-m magicfiles] file...\n"
#else
# define USAGE "Usage: %s [-vcz] [-f namefile] [-m magicfiles] file...\n"
#endif
#ifndef MAGIC
# define MAGIC "/etc/magic"
#endif
int
debug = 0,
lflag = 0,
zflag = 0;
int
nmagic = 0;
struct magic *magic;
char *magicfile;
char *progname;
int lineno;
static void unwrap __P((char *fn));
#if 0
static int byteconv4 __P((int, int, int));
static short byteconv2 __P((int, int, int));
#endif
int
main(argc, argv)
int argc;
char *argv[];
{
int c;
int check = 0, didsomefiles = 0, errflg = 0, ret = 0, app = 0;
if ((progname = strrchr(argv[0], '/')) != NULL)
progname++;
else
progname = argv[0];
if (!(magicfile = getenv("MAGIC")))
magicfile = MAGIC;
while ((c = getopt(argc, argv, "vcdf:Lm:z")) != -1)
switch (c) {
case 'v':
(void) fprintf(stdout, "%s-%d.%d\n", progname,
FILE_VERSION_MAJOR, patchlevel);
return 1;
case 'c':
++check;
break;
case 'd':
++debug;
break;
case 'f':
if (!app) {
ret = apprentice(magicfile, check);
if (check)
exit(ret);
app = 1;
}
unwrap(optarg);
++didsomefiles;
break;
#ifdef S_IFLNK
case 'L':
++lflag;
break;
#endif
case 'm':
magicfile = optarg;
break;
case 'z':
zflag++;
break;
case '?':
default:
errflg++;
break;
}
if (errflg) {
(void) fprintf(stderr, USAGE, progname);
exit(2);
}
if (!app) {
ret = apprentice(magicfile, check);
if (check)
exit(ret);
app = 1;
}
if (optind == argc) {
if (!didsomefiles) {
(void)fprintf(stderr, USAGE, progname);
exit(2);
}
}
else {
int i, wid, nw;
for (wid = 0, i = optind; i < argc; i++) {
nw = strlen(argv[i]);
if (nw > wid)
wid = nw;
}
for (; optind < argc; optind++)
process(argv[optind], wid);
}
return 0;
}
static void
unwrap(fn)
char *fn;
{
char buf[MAXPATHLEN];
FILE *f;
int wid = 0, cwid;
if (strcmp("-", fn) == 0) {
f = stdin;
wid = 1;
} else {
if ((f = fopen(fn, "r")) == NULL) {
error("Cannot open `%s' (%s).\n", fn, strerror(errno));
}
while (fgets(buf, sizeof(buf), f) != NULL) {
cwid = strlen(buf) - 1;
if (cwid > wid)
wid = cwid;
}
rewind(f);
}
while (fgets(buf, sizeof(buf), f) != NULL) {
buf[strlen(buf)-1] = '\0';
process(buf, wid);
}
(void) fclose(f);
}
#if 0
static int
byteconv4(from, same, big_endian)
int from;
int same;
int big_endian;
{
if (same)
return from;
else if (big_endian)
{
union {
int i;
char c[4];
} retval, tmpval;
tmpval.i = from;
retval.c[0] = tmpval.c[3];
retval.c[1] = tmpval.c[2];
retval.c[2] = tmpval.c[1];
retval.c[3] = tmpval.c[0];
return retval.i;
}
else
return ntohl(from);
}
static short
byteconv2(from, same, big_endian)
int from;
int same;
int big_endian;
{
if (same)
return from;
else if (big_endian)
{
union {
short s;
char c[2];
} retval, tmpval;
tmpval.s = (short) from;
retval.c[0] = tmpval.c[1];
retval.c[1] = tmpval.c[0];
return retval.s;
}
else
return ntohs(from);
}
#endif
void
process(inname, wid)
const char *inname;
int wid;
{
int fd = 0;
static const char stdname[] = "standard input";
unsigned char buf[HOWMANY+1];
struct stat sb;
int nbytes = 0;
char match = '\0';
#ifdef BUILTIN_FAT
unsigned magic;
#endif
if (strcmp("-", inname) == 0) {
if (fstat(0, &sb)<0) {
error("cannot fstat `%s' (%s).\n", stdname,
strerror(errno));
}
inname = stdname;
}
if (wid > 0)
(void) printf("%s:%*s ", inname,
(int) (wid - strlen(inname)), "");
if (inname != stdname) {
if (fsmagic(inname, &sb) != 0) {
putchar('\n');
return;
}
if ((fd = open(inname, O_RDONLY)) < 0) {
if (sb.st_mode & 0002) ckfputs("writeable, ", stdout);
if (sb.st_mode & 0111) ckfputs("executable, ", stdout);
ckfprintf(stdout, "can't read `%s' (%s).\n",
inname, strerror(errno));
return;
}
}
if ((nbytes = read(fd, (char *)buf, HOWMANY)) == -1) {
error("read failed (%s).\n", strerror(errno));
}
if (nbytes == 0)
ckfputs("empty", stdout);
else {
buf[nbytes++] = '\0';
match = tryit(buf, nbytes, zflag);
}
#ifdef BUILTIN_ELF
if (match == 's' && nbytes > 5)
tryelf(fd, buf, nbytes);
#endif
if (inname != stdname) {
#ifdef RESTORE_TIME
# ifdef USE_UTIMES
struct timeval utsbuf[2];
utsbuf[0].tv_sec = sb.st_atime;
utsbuf[1].tv_sec = sb.st_mtime;
(void) utimes(inname, utsbuf);
# else
struct utimbuf utbuf;
utbuf.actime = sb.st_atime;
utbuf.modtime = sb.st_mtime;
(void) utime(inname, &utbuf);
# endif
#endif
#ifdef BUILTIN_FAT
memcpy(&magic, buf, sizeof(unsigned long));
#ifdef __BIG_ENDIAN__
if(nbytes >= (int)sizeof(unsigned long) && magic == FAT_MAGIC)
#endif
#ifdef __LITTLE_ENDIAN__
if(nbytes >= sizeof(unsigned long) && magic == FAT_CIGAM)
#endif
tryfat(inname, fd, (char *)buf, nbytes);
#endif
(void) close(fd);
}
(void) putchar('\n');
}
int
tryit(buf, nb, zflag)
unsigned char *buf;
int nb, zflag;
{
if (zflag && zmagic(buf, nb))
return 'z';
if (softmagic(buf, nb))
return 's';
if (ascmagic(buf, nb))
return 'a';
if (internatmagic(buf, nb))
return 'i';
ckfputs("data", stdout);
return '\0';
}