Update to version 3.22

This commit is contained in:
christos 1997-01-28 00:49:36 +00:00
parent c7fd1435c6
commit 704854c465
17 changed files with 510 additions and 177 deletions

View File

@ -1,4 +1,4 @@
# $NetBSD: Makefile,v 1.9 1997/01/09 20:18:47 tls Exp $
# $NetBSD: Makefile,v 1.10 1997/01/28 00:49:36 christos Exp $
MAGIC= /etc/magic
MAGICOWN= bin
@ -7,7 +7,7 @@ MAGICMODE= 444
PROG= file
SRCS= file.c apprentice.c fsmagic.c softmagic.c ascmagic.c is_tar.c \
print.c compress.c readelf.c
print.c compress.c readelf.c internat.c
CFLAGS+= -DMAGIC='"$(MAGIC)"'
MAN= file.1 magic.5
@ -16,7 +16,7 @@ all: file magic
MAGFILES= $(.CURDIR)/magdir/Header\
$(.CURDIR)/magdir/Localstuff\
$(.CURDIR)/magdir/NetBSD\
$(.CURDIR)/magdir/netbsd\
$(.CURDIR)/magdir/[a-z]*
magic: $(MAGFILES)
cat $(MAGFILES) > $(.TARGET)

View File

@ -1,11 +1,9 @@
** README for file(1) Command **
@(#) $NetBSD: README,v 1.4 1997/01/09 20:18:48 tls Exp $
@(#) $NetBSD: README,v 1.5 1997/01/28 00:49:37 christos Exp $
This is Release 3.x of Ian Darwin's (copyright but distributable)
file(1) command. Release 3.x is scheduled for inclusion in the
4.4 BSD (Berkeley Software Distribution) of UNIX-like
software, and is the standard "file" command for Linux, 386bsd,
and other systems. (See "patchlevel.h" for the exact release number).
file(1) command. This version is the standard "file" command for Linux,
*BSD, and other systems. (See "patchlevel.h" for the exact release number).
UNIX is a trademark of UNIX System Laboratories.
@ -58,15 +56,29 @@ magic.4 - man page for the magic file, courtesy Guy Harris.
file.c - main program
file.h - header file
fsmagic.c - first set of tests the program runs, based on filesystem info
is_tar.c - knows about tarchives (courtesy John Gilmore).
is_tar.c, tar.h - knows about tarchives (courtesy John Gilmore).
magdir - directory of /etc/magic pieces
magdir/Makefile - ADJUST THIS FOR YOUR CONFIGURATION
names.h - header file for ascmagic.c
softmagic.c - 2nd set of tests, based on /etc/magic
strtok.c, getopt.c - in case you them (courtesy of Henry Spencer).
strtol.c, strchr.c - in case you need them - public domain.
tst - simple test suite, built from tst/Makefile
readelf.[ch] - Standalone elf parsing code.
compress.c - on-the-fly decompression.
internat.c - recognize international `text' files.
print.c - print results, errors, warnings.
If your gzip sometimes fails to decompress things complaining about a short
file, apply this patch [which is going to be in the next version of gzip]:
*** - Tue Oct 29 02:06:35 1996
--- util.c Sun Jul 21 21:51:38 1996
*** 106,111 ****
--- 108,114 ----
if (insize == 0) {
if (eof_ok) return EOF;
+ flush_window();
read_error();
}
bytes_in += (ulg)insize;
E-mail: christos@deshaw.com, moraes@deshaw.com

View File

@ -1,4 +1,4 @@
/* $NetBSD: apprentice.c,v 1.12 1997/01/09 20:18:49 tls Exp $ */
/* $NetBSD: apprentice.c,v 1.13 1997/01/28 00:49:37 christos Exp $ */
/*
* apprentice - make one pass through /etc/magic, learning its secrets.
@ -36,7 +36,7 @@
#ifndef lint
static char *moduleid =
"@(#)$NetBSD: apprentice.c,v 1.12 1997/01/09 20:18:49 tls Exp $";
"@(#)$NetBSD: apprentice.c,v 1.13 1997/01/28 00:49:37 christos Exp $";
#endif /* lint */
#define EATAB {while (isascii((unsigned char) *l) && \
@ -135,10 +135,10 @@ int check; /* non-zero? checking-only run. */
/*
* extend the sign bit if the comparison is to be signed
*/
unsigned long
uint32
signextend(m, v)
struct magic *m;
unsigned long v;
uint32 v;
{
if (!(m->flag & UNSIGNED))
switch(m->type) {
@ -161,7 +161,7 @@ unsigned long v;
case LONG:
case BELONG:
case LELONG:
v = (long) v;
v = (int32) v;
break;
case STRING:
break;
@ -497,21 +497,16 @@ int plen, *slen;
*p++ = (char)val;
break;
/* \x and up to 3 hex digits */
/* \x and up to 2 hex digits */
case 'x':
val = 'x'; /* Default if no digits */
c = hextoint(*s++); /* Get next char */
if (c >= 0) {
val = c;
c = hextoint(*s++);
if (c >= 0) {
if (c >= 0)
val = (val << 4) + c;
c = hextoint(*s++);
if (c >= 0) {
val = (val << 4) + c;
} else
--s;
} else
else
--s;
} else
--s;

View File

@ -1,4 +1,4 @@
/* $NetBSD: ascmagic.c,v 1.9 1997/01/09 20:18:50 tls Exp $ */
/* $NetBSD: ascmagic.c,v 1.10 1997/01/28 00:49:38 christos Exp $ */
/*
* ASCII magic -- file types that we know based on keywords
@ -38,7 +38,7 @@
#ifndef lint
static char *moduleid =
"@(#)$NetBSD: ascmagic.c,v 1.9 1997/01/09 20:18:50 tls Exp $";
"@(#)$NetBSD: ascmagic.c,v 1.10 1997/01/28 00:49:38 christos Exp $";
#endif /* lint */
/* an optimisation over plain strcmp() */
@ -90,6 +90,13 @@ int nbytes; /* size actually read */
return 1;
}
/* Make sure we are dealing with ascii text before looking for tokens */
for (i = 0; i < nbytes; i++) {
if (!isascii(buf[i]))
return 0; /* not all ASCII */
}
/* look for tokens from names.h - this is expensive! */
/* make a copy of the buffer here because strtok() will destroy it */
s = (unsigned char*) memcpy(nbuf, buf, nbytes);
@ -108,12 +115,6 @@ int nbytes; /* size actually read */
}
}
for (i = 0; i < nbytes; i++) {
if (!isascii(buf[i]))
return 0; /* not all ASCII */
}
/* all else fails, but it is ASCII... */
ckfputs("ASCII text", stdout);
if (has_escapes) {

View File

@ -1,5 +1,5 @@
.TH FILE 1 "Copyright but distributable"
.\" $NetBSD: file.1,v 1.10 1997/01/09 20:18:51 tls Exp $
.\" $NetBSD: file.1,v 1.11 1997/01/28 00:49:39 christos Exp $
.SH NAME
file
\- determine file type
@ -16,7 +16,7 @@ namefile ]
magicfiles ]
file ...
.SH DESCRIPTION
This manual page documents version 3.21 of the
This manual page documents version 3.22 of the
.B file
command.
.B File
@ -43,7 +43,7 @@ meaning anything else (data is usually `binary' or non-printable).
Exceptions are well-known file formats (core files, tar archives)
that are known to contain binary data.
When modifying the file
.I /usr/local/etc/magic
.I /etc/magic
or the program itself,
.B "preserve these keywords" .
People depend on knowing that all the readable files in a directory
@ -79,7 +79,7 @@ The concept of `magic number' has been applied by extension to data files.
Any file with some invariant identifier at a small fixed
offset into the file can usually be described in this way.
The information in these files is read from the magic file
.I /usr/local/etc/magic.
.I /etc/magic.
.PP
If an argument appears to be an
.SM ASCII
@ -136,7 +136,7 @@ option causes symlinks to be followed, as the like-named option in
.BR ls (1).
(on systems that support symbolic links).
.SH FILES
.I /usr/local/etc/magic
.I /etc/magic
\- default list of magic numbers
.SH ENVIRONMENT
The environment variable
@ -202,7 +202,7 @@ If your old
command uses a magic file,
keep the old magic file around for comparison purposes
(rename it to
.IR /usr/local/etc/magic.orig ).
.IR /etc/magic.orig ).
.SH HISTORY
There has been a
.B file
@ -248,7 +248,7 @@ the process running
.RE
.PP
Changes by Ian Darwin and various authors including
Christos Zoulas (christos@ee.cornell.edu), 1990-1992.
Christos Zoulas (christos@deshaw.com), 1990-1997.
.SH LEGAL NOTICE
Copyright (c) Ian F. Darwin, Toronto, Canada,
1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993.
@ -360,6 +360,6 @@ This manual page, and particularly this section, is too long.
.SH AVAILABILITY
You can obtain the original author's latest version by anonymous FTP
on
.B tesla.ee.cornell.edu
.B ftp.deshaw.com
in the directory
.I /pub/file-X.YY.tar.gz
.I /pub/file/file-X.YY.tar.gz

View File

@ -1,4 +1,4 @@
/* $NetBSD: file.c,v 1.10 1997/01/09 20:18:52 tls Exp $ */
/* $NetBSD: file.c,v 1.11 1997/01/28 00:49:40 christos Exp $ */
/*
* file - find type of a file or files - main program.
@ -28,7 +28,7 @@
*/
#ifndef lint
static char *moduleid =
"@(#)$NetBSD: file.c,v 1.10 1997/01/09 20:18:52 tls Exp $";
"@(#)$NetBSD: file.c,v 1.11 1997/01/28 00:49:40 christos Exp $";
#endif /* lint */
#include <stdio.h>
@ -39,9 +39,13 @@ static char *moduleid =
#include <sys/stat.h>
#include <fcntl.h> /* for open() */
#if (__COHERENT__ >= 0x420)
#include <sys/utime.h>
# include <sys/utime.h>
#else
#include <utime.h>
# ifdef USE_UTIMES
# include <sys/time.h>
# else
# include <utime.h>
# endif
#endif
#include <unistd.h> /* for read() */
@ -77,8 +81,10 @@ int lineno; /* line number in the magic file */
static void unwrap __P((char *fn));
#if 0
static int byteconv4 __P((int, int, int));
static short byteconv2 __P((int, int, int));
#endif
/*
* main - parse arguments and handle options
@ -209,6 +215,7 @@ char *fn;
}
#if 0
/*
* byteconv4
* Input:
@ -271,6 +278,7 @@ byteconv2(from, same, big_endian)
else
return ntohs(from); /* msb -> lsb conversion on lsb */
}
#endif
/*
* process - process input file
@ -283,7 +291,6 @@ int wid;
int fd = 0;
static const char stdname[] = "standard input";
unsigned char buf[HOWMANY+1]; /* one extra for terminating '\0' */
struct utimbuf utbuf;
struct stat sb;
int nbytes = 0; /* number of bytes read from a datafile */
char match = '\0';
@ -342,12 +349,24 @@ int wid;
#endif
if (inname != stdname) {
#ifdef RESTORE_TIME
/*
* Try to restore access, modification times if read it.
*/
# 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); /* don't care if loses */
# else
struct utimbuf utbuf;
utbuf.actime = sb.st_atime;
utbuf.modtime = sb.st_mtime;
(void) utime(inname, &utbuf); /* don't care if loses */
# endif
#endif
(void) close(fd);
}
(void) putchar('\n');
@ -371,6 +390,10 @@ int nb, zflag;
if (ascmagic(buf, nb))
return 'a';
/* see if it's international language text */
if (internatmagic(buf, nb))
return 'i';
/* abandon hope, all ye who remain here */
ckfputs("data", stdout);
return '\0';

View File

@ -1,8 +1,8 @@
/* $NetBSD: file.h,v 1.9 1997/01/09 20:18:53 tls Exp $ */
/* $NetBSD: file.h,v 1.10 1997/01/28 00:49:41 christos Exp $ */
/*
* file.h - definitions for file(1) program
* @(#)$NetBSD: file.h,v 1.9 1997/01/09 20:18:53 tls Exp $
* @(#)$NetBSD: file.h,v 1.10 1997/01/28 00:49:41 christos Exp $
*
* Copyright (c) Ian F. Darwin, 1987.
* Written by Ian F. Darwin.
@ -28,6 +28,12 @@
* 4. This notice may not be removed or altered.
*/
#ifndef __file_h__
#define __file_h__
typedef int int32;
typedef unsigned int uint32;
#ifndef HOWMANY
# define HOWMANY 8192 /* how much of the file to look at */
#endif
@ -43,9 +49,9 @@ struct magic {
short cont_level; /* level of ">" */
struct {
char type; /* byte short long */
long offset; /* offset from indirection */
int32 offset; /* offset from indirection */
} in;
long offset; /* offset to magic number */
int32 offset; /* offset to magic number */
unsigned char reln; /* relation (0=eq, '>'=gt, etc) */
char type; /* int, short, long or string. */
char vallen; /* length of string value, if any */
@ -63,12 +69,12 @@ struct magic {
union VALUETYPE {
unsigned char b;
unsigned short h;
unsigned long l;
uint32 l;
char s[MAXstring];
unsigned char hs[2]; /* 2 bytes of a fixed-endian "short" */
unsigned char hl[4]; /* 2 bytes of a fixed-endian "long" */
} value; /* either number or string */
unsigned long mask; /* mask before comparison with value */
uint32 mask; /* mask before comparison with value */
char nospflag; /* supress space character */
char desc[MAXDESC]; /* description */
};
@ -100,8 +106,9 @@ extern int softmagic __P((unsigned char *, int));
extern int tryit __P((unsigned char *, int, int));
extern int zmagic __P((unsigned char *, int));
extern void ckfprintf __P((FILE *, const char *, ...));
extern unsigned long signextend __P((struct magic *, unsigned long));
extern uint32 signextend __P((struct magic *, unsigned int32));
extern int internatmagic __P((unsigned char *, int));
extern void tryelf __P((int, char *, int));
extern int errno; /* Some unixes don't define this.. */
@ -121,7 +128,16 @@ extern int lflag; /* follow symbolic links? */
extern int optind; /* From getopt(3) */
extern char *optarg;
#if !defined(__STDC__) || defined(sun) || defined(__sun__) || defined(__convex__)
#if defined(sun) || defined(__sun__) || defined (__sun)
# if defined(__svr4) || defined (__SVR4) || defined(__svr4__)
# define SOLARIS
# else
# define SUNOS
# endif
#endif
#if !defined(__STDC__) || defined(SUNOS) || defined(__convex__)
extern int sys_nerr;
extern char *sys_errlist[];
#define strerror(e) \
@ -132,3 +148,5 @@ extern char *sys_errlist[];
#ifndef MAXPATHLEN
#define MAXPATHLEN 512
#endif
#endif /* __file_h__ */

View File

@ -1,4 +1,4 @@
/* $NetBSD: fsmagic.c,v 1.9 1997/01/09 20:18:54 tls Exp $ */
/* $NetBSD: fsmagic.c,v 1.10 1997/01/28 00:49:41 christos Exp $ */
/*
* fsmagic - magic based on filesystem info - directory, special files, etc.
@ -53,7 +53,7 @@
#ifndef lint
static char *moduleid =
"@(#)$NetBSD: fsmagic.c,v 1.9 1997/01/09 20:18:54 tls Exp $";
"@(#)$NetBSD: fsmagic.c,v 1.10 1997/01/28 00:49:41 christos Exp $";
#endif /* lint */
int
@ -91,12 +91,12 @@ struct stat *sb;
ckfputs("directory", stdout);
return 1;
case S_IFCHR:
(void) printf("character special (%d/%d)",
major(sb->st_rdev), minor(sb->st_rdev));
(void) printf("character special (%ld/%ld)",
(long) major(sb->st_rdev), (long) minor(sb->st_rdev));
return 1;
case S_IFBLK:
(void) printf("block special (%d/%d)",
major(sb->st_rdev), minor(sb->st_rdev));
(void) printf("block special (%ld/%ld)",
(long) major(sb->st_rdev), (long) minor(sb->st_rdev));
return 1;
/* TODO add code to handle V7 MUX and Blit MUX files */
#ifdef S_IFIFO

72
usr.bin/file/internat.c Normal file
View File

@ -0,0 +1,72 @@
#include "file.h"
#include <string.h>
#define F 0
#define T 1
/*
* List of characters that look "reasonable" in international
* language texts. That's almost all characters :), except a
* few in the control range of ASCII (all the known international
* charactersets share the bottom half with ASCII).
*/
static char maybe_internat[256] = {
F, F, F, F, F, F, F, F, T, T, T, T, T, T, F, F, /* 0x0X */
F, F, F, F, F, F, F, F, F, F, F, T, F, F, F, F, /* 0x1X */
T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0x2X */
T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0x3X */
T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0x4X */
T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0x5X */
T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0x6X */
T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, F, /* 0x7X */
T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0x8X */
T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0x9X */
T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0xaX */
T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0xbX */
T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0xcX */
T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0xdX */
T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0xeX */
T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T /* 0xfX */
};
/* Maximal length of a line we consider "reasonable". */
#define MAXLINELEN 300
int
internatmagic(buf, nbytes)
unsigned char *buf;
int nbytes;
{
int i;
unsigned char *cp;
nbytes--;
/* First, look whether there are "unreasonable" characters. */
for (i = 0, cp = buf; i < nbytes; i++, cp++)
if (!maybe_internat[*cp])
return 0;
/*
* Now, look whether the file consists of lines of
* "reasonable" length.
*/
for (i = 0; i < nbytes;) {
cp = memchr(buf, '\n', nbytes - i);
if (cp == NULL) {
/* Don't fail if we hit the end of buffer. */
if (i + MAXLINELEN >= nbytes)
break;
else
return 0;
}
if (cp - buf > MAXLINELEN)
return 0;
i += (cp - buf + 1);
buf = cp + 1;
}
ckfputs("International language text", stdout);
return 1;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: is_tar.c,v 1.6 1997/01/09 20:18:55 tls Exp $ */
/* $NetBSD: is_tar.c,v 1.7 1997/01/28 00:49:42 christos Exp $ */
/*
* is_tar() -- figure out whether file is a tar archive.
@ -7,7 +7,7 @@
* Pubic Domain version written 26 Aug 1985 John Gilmore (ihnp4!hoptoad!gnu).
*
* @(#)list.c 1.18 9/23/86 Public Domain - gnu
* $NetBSD: is_tar.c,v 1.6 1997/01/09 20:18:55 tls Exp $
* $NetBSD: is_tar.c,v 1.7 1997/01/28 00:49:42 christos Exp $
*
* Comments changed and some code/comments reformatted
* for file command by Ian Darwin.
@ -21,9 +21,9 @@
#define isodigit(c) ( ((c) >= '0') && ((c) <= '7') )
#if defined(__STDC__) || defined(__cplusplus)
static long from_oct(int, char*); /* Decode octal number */
static int from_oct(int, char*); /* Decode octal number */
#else
static long from_oct();
static int from_oct();
#endif
/*
@ -39,7 +39,7 @@ int nbytes;
{
register union record *header = (union record *)buf;
register int i;
register long sum, recsum;
register int sum, recsum;
register char *p;
if (nbytes < sizeof(union record))
@ -77,12 +77,12 @@ int nbytes;
*
* Result is -1 if the field is invalid (all blank, or nonoctal).
*/
static long
static int
from_oct(digs, where)
register int digs;
register char *where;
{
register long value;
register int value;
while (isspace(*where)) { /* Skip spaces */
where++;

View File

@ -6,7 +6,7 @@ magic \- file command's magic number file
This manual page documents the format of the magic file as
used by the
.BR file (1)
command, version 3.21. The
command, version 3.22. The
.B file
command identifies the type of a file using,
among other tests,
@ -203,4 +203,4 @@ indirect offsets.
.\" the changes I posted to the S5R2 version.
.\"
.\" Modified for Ian Darwin's version of the file command.
.\" @(#)$NetBSD: magic.5,v 1.10 1997/01/09 20:18:56 tls Exp $
.\" @(#)$NetBSD: magic.5,v 1.11 1997/01/28 00:49:44 christos Exp $

View File

@ -1,4 +1,4 @@
/* $NetBSD: names.h,v 1.5 1997/01/09 20:18:57 tls Exp $ */
/* $NetBSD: names.h,v 1.6 1997/01/28 00:49:45 christos Exp $ */
/*
* Names.h - names and types used by ascmagic in file(1).
@ -12,22 +12,24 @@
*
* See LEGAL.NOTICE
*
* $NetBSD: names.h,v 1.5 1997/01/09 20:18:57 tls Exp $
* $NetBSD: names.h,v 1.6 1997/01/28 00:49:45 christos Exp $
*/
/* these types are used to index the table 'types': keep em in sync! */
#define L_C 0 /* first and foremost on UNIX */
#define L_FORT 1 /* the oldest one */
#define L_MAKE 2 /* Makefiles */
#define L_PLI 3 /* PL/1 */
#define L_MACH 4 /* some kinda assembler */
#define L_ENG 5 /* English */
#define L_PAS 6 /* Pascal */
#define L_MAIL 7 /* Electronic mail */
#define L_NEWS 8 /* Usenet Netnews */
#define L_CC 1 /* Bjarne's postincrement */
#define L_FORT 2 /* the oldest one */
#define L_MAKE 3 /* Makefiles */
#define L_PLI 4 /* PL/1 */
#define L_MACH 5 /* some kinda assembler */
#define L_ENG 6 /* English */
#define L_PAS 7 /* Pascal */
#define L_MAIL 8 /* Electronic mail */
#define L_NEWS 9 /* Usenet Netnews */
static char *types[] = {
"C program text",
"C++ program text",
"FORTRAN program text",
"make commands text" ,
"PL/1 program text",
@ -45,6 +47,12 @@ static struct names {
} names[] = {
/* These must be sorted by eye for optimal hit rate */
/* Add to this list only after substantial meditation */
{"//", L_CC},
{"template", L_CC},
{"virtual", L_CC},
{"class", L_CC},
{"public:", L_CC},
{"private:", L_CC},
{"/*", L_C}, /* must precede "The", "the", etc. */
{"#include", L_C},
{"char", L_C},

View File

@ -1,16 +1,21 @@
#define FILE_VERSION_MAJOR 3
#define patchlevel 21
#define patchlevel 22
/*
* Patchlevel file for Ian Darwin's MAGIC command.
* $NetBSD: patchlevel.h,v 1.7 1997/01/09 20:18:58 tls Exp $
* $NetBSD: patchlevel.h,v 1.8 1997/01/28 00:49:45 christos Exp $
*
* $Log: patchlevel.h,v $
* Revision 1.7 1997/01/09 20:18:58 tls
* RCS ID police
*
* Revision 1.6 1996/10/05 20:20:29 christos
* Changes from file-3.21
* Log: patchlevel.h,v
* Revision 1.22 1997/01/15 17:23:24 christos
* - add support for elf core files: find the program name under SVR4 [Ken Pizzini]
* - print strings only up to the first carriage return [various]
* - freebsd international ascii support [J Wunsch]
* - magic fixes and additions [Guy Harris]
* - 64 bit fixes [Larry Schwimmer]
* - support for both utime and utimes, but don't restore file access times
* by default [various]
* - \xXX only takes 2 hex digits, not 3.
* - re-implement support for core files [Guy Harris]
*
* Revision 1.21 1996/10/05 18:15:29 christos
* Segregate elf stuff and conditionally enable it with -DBUILTIN_ELF

View File

@ -1,4 +1,4 @@
/* $NetBSD: print.c,v 1.9 1997/01/09 20:18:59 tls Exp $ */
/* $NetBSD: print.c,v 1.10 1997/01/28 00:49:46 christos Exp $ */
/*
* print.c - debugging printout routines
@ -42,7 +42,7 @@
#ifndef lint
static char *moduleid =
"@(#)$NetBSD: print.c,v 1.9 1997/01/09 20:18:59 tls Exp $";
"@(#)$NetBSD: print.c,v 1.10 1997/01/28 00:49:46 christos Exp $";
#endif /* lint */
#define SZOF(a) (sizeof(a) / sizeof(a[0]))
@ -60,7 +60,7 @@ struct magic *m;
m->offset);
if (m->flag & INDIR)
(void) fprintf(stderr, "(%s,%ld),",
(void) fprintf(stderr, "(%s,%d),",
(m->in.type >= 0 && m->in.type < SZOF(typ)) ?
typ[(unsigned char) m->in.type] :
"*bad*",
@ -71,7 +71,7 @@ struct magic *m;
typ[(unsigned char) m->type] :
"*bad*");
if (m->mask != ~0L)
(void) fprintf(stderr, " & %.8lx", m->mask);
(void) fprintf(stderr, " & %.8x", m->mask);
(void) fprintf(stderr, ",%c", m->reln);
@ -84,7 +84,7 @@ struct magic *m;
case LELONG:
case BESHORT:
case BELONG:
(void) fprintf(stderr, "%ld", m->value.l);
(void) fprintf(stderr, "%d", m->value.l);
break;
case STRING:
showstr(stderr, m->value.s, -1);

View File

@ -1,5 +1,7 @@
#ifdef BUILTIN_ELF
#include <sys/types.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
@ -17,7 +19,14 @@ doshn(fd, off, num, size, buf)
size_t size;
char *buf;
{
/* This works for both 32 and 64 bit elf formats */
/*
* This works for both 32-bit and 64-bit ELF formats,
* because it looks only at the "sh_type" field, which is
* always 32 bits, and is preceded only by the "sh_name"
* field which is also always 32 bits, and because it uses
* the shdr size from the ELF header rather than using
* the size of an "Elf32_Shdr".
*/
Elf32_Shdr *sh = (Elf32_Shdr *) buf;
if (lseek(fd, off, SEEK_SET) == -1)
@ -26,45 +35,189 @@ doshn(fd, off, num, size, buf)
for ( ; num; num--) {
if (read(fd, buf, size) == -1)
error("read failed (%s).\n", strerror(errno));
if (sh->sh_type == SHT_SYMTAB)
if (sh->sh_type == SHT_SYMTAB) {
(void) printf (", not stripped");
return;
}
}
(void) printf (", stripped");
}
#ifdef notyet
/*
* Look through the program headers of an executable image, searching
* for a PT_INTERP section; if one is found, it's dynamically linked,
* otherwise it's statically linked.
*/
static void
dophn(fd, off, num, size, buf)
dophn_exec(fd, off, num, size, buf)
int fd;
off_t off;
int num;
size_t size;
char *buf;
{
/* I am not sure if this works for 64 bit elf formats */
Elf32_Phdr *ph = (Elf32_Phdr *) buf;
if (lseek(fd, off, SEEK_SET) == -1)
error("lseek failed (%s).\n", strerror(errno));
for ( ; num; num--) {
if (read(fd, buf, size) == -1)
error("read failed (%s).\n", strerror(errno));
if (ph->p_type == PT_INTERP) {
/*
* Has an interpreter - must be a dynamically-linked
* executable.
*/
printf(", dynamically linked");
return;
}
}
printf(", statically linked");
}
size_t prpsoffsets[] = {
100, /* SunOS 5.x */
32, /* Linux */
};
#define NOFFSETS (sizeof prpsoffsets / sizeof prpsoffsets[0])
/*
* Look through the program headers of an executable image, searching
* for a PT_NOTE section of type NT_PRPSINFO, with a name "CORE"; if one
* is found, try looking in various places in its contents for a 16-character
* string containing only printable characters - if found, that string
* should be the name of the program that dropped core.
* Note: right after that 16-character string is, at least in SunOS 5.x
* (and possibly other SVR4-flavored systems) and Linux, a longer string
* (80 characters, in 5.x, probably other SVR4-flavored systems, and Linux)
* containing the start of the command line for that program.
*/
static void
dophn_core(fd, off, num, size, buf)
int fd;
off_t off;
int num;
size_t size;
char *buf;
{
/*
* This doesn't work for 64-bit ELF, as the "p_offset" field is
* 64 bits in 64-bit ELF.
*/
/*
* This doesn't work for 64-bit ELF, as the "p_offset" field is
* 64 bits in 64-bit ELF.
*/
Elf32_Phdr *ph = (Elf32_Phdr *) buf;
Elf32_Nhdr *nh;
size_t offset, noffset, reloffset;
unsigned char c;
int i, j;
char nbuf[BUFSIZ];
int bufsize;
for ( ; num; num--) {
if (lseek(fd, off, SEEK_SET) == -1)
error("lseek failed (%s).\n", strerror(errno));
if (read(fd, buf, size) == -1)
error("read failed (%s).\n", strerror(errno));
printf("type:%d\n", ph->p_type);
off += size;
if (ph->p_type != PT_NOTE)
continue;
if (lseek(fd, ph->p_offset, SEEK_SET) == -1)
error("lseek failed (%s).\n", strerror(errno));
if (size > BUFSIZ)
size = BUFSIZ;
if (read(fd, buf, size) == -1)
bufsize = read(fd, nbuf, BUFSIZ);
if (bufsize == -1)
error("read failed (%s).\n", strerror(errno));
for (size = 0; size < BUFSIZ; size++)
if (isalnum(buf[size]))
printf("%d %c\n", size, buf[size]);
return;
offset = 0;
for (;;) {
if (offset >= bufsize)
break;
nh = (Elf32_Nhdr *)&nbuf[offset];
offset += sizeof *nh;
/*
* If this note isn't an NT_PRPSINFO note, it's
* not what we're looking for.
*/
if (nh->n_type != NT_PRPSINFO) {
offset += nh->n_namesz;
offset = ((offset + 3)/4)*4;
offset += nh->n_descsz;
offset = ((offset + 3)/4)*4;
continue;
}
/*
* Make sure this note has the name "CORE".
*/
if (offset + nh->n_namesz >= bufsize) {
/*
* We're past the end of the buffer.
*/
break;
}
if (nh->n_namesz != 5
|| strcmp(&nbuf[offset], "CORE") != 0)
continue;
offset += nh->n_namesz;
offset = ((offset + 3)/4)*4;
/*
* Extract the program name. We assume it to be
* 16 characters (that's what it is in SunOS 5.x
* and Linux).
*
* Unfortunately, it's at a different offset in
* SunOS 5.x and Linux, so try multiple offsets.
* If the characters aren't all printable, reject
* it.
*/
for (i = 0; i < NOFFSETS; i++) {
reloffset = prpsoffsets[i];
noffset = offset + reloffset;
for (j = 0; j < 16;
j++, noffset++, reloffset++) {
/*
* Make sure we're not past the end
* of the buffer; if we are, just
* give up.
*/
if (noffset >= bufsize)
return;
/*
* Make sure we're not past the
* end of the contents; if we
* are, this obviously isn't
* the right offset.
*/
if (reloffset >= nh->n_descsz)
goto tryanother;
c = nbuf[noffset];
if (c != '\0' && !isprint(c))
goto tryanother;
}
/*
* Well, that worked.
*/
printf(", from '%.16s'",
&nbuf[offset + prpsoffsets[i]]);
return;
tryanother:
;
}
offset += nh->n_descsz;
offset = ((offset + 3)/4)*4;
}
}
}
#endif
void
tryelf(fd, buf, nbytes)
@ -73,8 +226,8 @@ tryelf(fd, buf, nbytes)
int nbytes;
{
union {
long l;
char c[sizeof (long)];
int32 l;
char c[sizeof (int32)];
} u;
/*
@ -84,7 +237,7 @@ tryelf(fd, buf, nbytes)
* one is found or else the binary is stripped.
*/
if (buf[EI_MAG0] != ELFMAG0 || buf[EI_MAG1] != ELFMAG1
&& buf[EI_MAG2] != ELFMAG2 || buf[EI_MAG3] != ELFMAG3)
|| buf[EI_MAG2] != ELFMAG2 || buf[EI_MAG3] != ELFMAG3)
return;
@ -96,23 +249,27 @@ tryelf(fd, buf, nbytes)
u.l = 1;
(void) memcpy(&elfhdr, buf, sizeof elfhdr);
#ifdef notyet
if (elfhdr.e_type == ET_CORE)
dophn(fd, elfhdr.e_phoff, elfhdr.e_phnum,
elfhdr.e_phentsize, buf);
else
#endif
{
/*
* If the system byteorder does not equal the
* object byteorder then don't test.
*/
if ((u.c[sizeof(long) - 1] + 1) == elfhdr.e_ident[5])
/*
* If the system byteorder does not equal the
* object byteorder then don't test.
* XXX - we could conceivably fix up the "dophn_XXX()" and
* "doshn()" routines to extract stuff in the right
* byte order....
*/
if ((u.c[sizeof(long) - 1] + 1) == elfhdr.e_ident[5]) {
if (elfhdr.e_type == ET_CORE)
dophn_core(fd, elfhdr.e_phoff, elfhdr.e_phnum,
elfhdr.e_phentsize, buf);
else {
if (elfhdr.e_type == ET_EXEC) {
dophn_exec(fd, elfhdr.e_phoff,
elfhdr.e_phnum,
elfhdr.e_phentsize, buf);
}
doshn(fd, elfhdr.e_shoff, elfhdr.e_shnum,
elfhdr.e_shentsize, buf);
}
}
return;
}
@ -125,22 +282,32 @@ tryelf(fd, buf, nbytes)
u.l = 1;
(void) memcpy(&elfhdr, buf, sizeof elfhdr);
/*
* If the system byteorder does not equal the
* object byteorder then don't test.
* XXX - we could conceivably fix up the "dophn_XXX()" and
* "doshn()" routines to extract stuff in the right
* byte order....
*/
if ((u.c[sizeof(long) - 1] + 1) == elfhdr.e_ident[5]) {
#ifdef notyet
if (elfhdr.e_type == ET_CORE)
dophn(fd, elfhdr.e_phoff, elfhdr.e_phnum,
elfhdr.e_phentsize, buf);
else
if (elfhdr.e_type == ET_CORE)
dophn_core(fd, elfhdr.e_phoff, elfhdr.e_phnum,
elfhdr.e_phentsize, buf);
else
#endif
{
#ifdef notyet
if (elfhdr.e_type == ET_EXEC) {
dophn_exec(fd, elfhdr.e_phoff,
elfhdr.e_phnum,
elfhdr.e_phentsize, buf);
}
#endif
{
/*
* If the system byteorder does not equal the
* object byteorder then don't test.
*/
if ((u.c[sizeof(long) - 1] + 1) == elfhdr.e_ident[5])
doshn(fd, elfhdr.e_shoff, elfhdr.e_shnum,
elfhdr.e_shentsize, buf);
}
}
return;
}
}

View File

@ -1,8 +1,8 @@
/* $NetBSD: readelf.h,v 1.2 1997/01/09 20:19:00 tls Exp $ */
/* $NetBSD: readelf.h,v 1.3 1997/01/28 00:49:48 christos Exp $ */
/*
* readelf.h
* @(#)$NetBSD: readelf.h,v 1.2 1997/01/09 20:19:00 tls Exp $
* @(#)$NetBSD: readelf.h,v 1.3 1997/01/28 00:49:48 christos Exp $
*
* Provide elf data structures for non-elf machines, allowing file
* non-elf hosts to determine if an elf binary is stripped.
@ -61,6 +61,7 @@ typedef struct {
} Elf64_Ehdr;
/* e_type */
#define ET_EXEC 2
#define ET_CORE 4
/* sh_type */
@ -139,4 +140,30 @@ typedef struct {
Elf64_Off sh_entsize;
} Elf64_Shdr;
/* Notes used in ET_CORE */
#define NT_PRSTATUS 1
#define NT_PRFPREG 2
#define NT_PRPSINFO 3
#define NT_TASKSTRUCT 4
/* Note header in a PT_NOTE section */
typedef struct elf_note {
Elf32_Word n_namesz; /* Name size */
Elf32_Word n_descsz; /* Content size */
Elf32_Word n_type; /* Content type */
} Elf32_Nhdr;
typedef struct {
Elf64_Word n_namesz;
Elf64_Word n_descsz;
Elf64_Word n_type;
} Elf64_Nhdr;
#define NT_PRSTATUS 1
#define NT_PRFPREG 2
#define NT_PRPSINFO 3
#define NT_PRXREG 4
#define NT_PLATFORM 5
#define NT_AUXV 6
#endif

View File

@ -1,4 +1,4 @@
/* $NetBSD: softmagic.c,v 1.12 1997/01/09 20:19:01 tls Exp $ */
/* $NetBSD: softmagic.c,v 1.13 1997/01/28 00:49:48 christos Exp $ */
/*
* softmagic - interpret variable magic from /etc/magic
@ -28,8 +28,8 @@
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
@ -37,15 +37,15 @@
#ifndef lint
static char *moduleid =
"@(#)$NetBSD: softmagic.c,v 1.12 1997/01/09 20:19:01 tls Exp $";
"@(#)$NetBSD: softmagic.c,v 1.13 1997/01/28 00:49:48 christos Exp $";
#endif /* lint */
static int match __P((unsigned char *, int));
static int mget __P((union VALUETYPE *,
unsigned char *, struct magic *, int));
static int mcheck __P((union VALUETYPE *, struct magic *));
static long mprint __P((union VALUETYPE *, struct magic *));
static void mdebug __P((long, char *, int));
static int32 mprint __P((union VALUETYPE *, struct magic *));
static void mdebug __P((int32, char *, int));
static int mconvert __P((union VALUETYPE *, struct magic *));
/*
@ -101,12 +101,12 @@ int nbytes;
int cont_level = 0;
int need_separator = 0;
union VALUETYPE p;
static long *tmpoff = NULL;
static int32 *tmpoff = NULL;
static size_t tmplen = 0;
long oldoff = 0;
int32 oldoff = 0;
if (tmpoff == NULL)
if ((tmpoff = (long *) malloc(tmplen = 20)) == NULL)
if ((tmpoff = (int32 *) malloc(tmplen = 20)) == NULL)
error("out of memory\n");
for (magindex = 0; magindex < nmagic; magindex++) {
@ -132,7 +132,7 @@ int nbytes;
need_separator = 1;
/* and any continuations that match */
if (++cont_level >= tmplen)
if ((tmpoff = (long *) realloc(tmpoff,
if ((tmpoff = (int32 *) realloc(tmpoff,
tmplen += 20)) == NULL)
error("out of memory\n");
while (magic[magindex+1].cont_level != 0 &&
@ -177,7 +177,7 @@ int nbytes;
*/
if (++cont_level >= tmplen)
if ((tmpoff =
(long *) realloc(tmpoff,
(int32 *) realloc(tmpoff,
tmplen += 20)) == NULL)
error("out of memory\n");
}
@ -191,14 +191,14 @@ int nbytes;
return 0; /* no match at all */
}
static long
static int32
mprint(p, m)
union VALUETYPE *p;
struct magic *m;
{
char *pp, *rt;
unsigned long v;
long t=0 ;
uint32 v;
int32 t=0 ;
switch (m->type) {
@ -223,8 +223,8 @@ struct magic *m;
case LELONG:
v = p->l;
v = signextend(m, v) & m->mask;
(void) printf(m->desc, (unsigned long) v);
t = m->offset + sizeof(long);
(void) printf(m->desc, (uint32) v);
t = m->offset + sizeof(int32);
break;
case STRING:
@ -233,6 +233,11 @@ struct magic *m;
t = m->offset + strlen(m->value.s);
}
else {
if (*m->value.s == '\0') {
char *cp = strchr(p->s,'\n');
if (cp)
*cp = '\0';
}
(void) printf(m->desc, p->s);
t = m->offset + strlen(p->s);
}
@ -246,7 +251,8 @@ struct magic *m;
*rt = '\0';
(void) printf(m->desc, pp);
t = m->offset + sizeof(time_t);
return;
break;
default:
error("invalid m->type (%d) in mprint().\n", m->type);
/*NOTREACHED*/
@ -270,13 +276,12 @@ struct magic *m;
return 1;
case STRING:
{
size_t len;
char *ptr;
/* Null terminate and eat the return */
p->s[sizeof(p->s) - 1] = '\0';
len = strlen(p->s);
if (len > 0 && p->s[len - 1] == '\n')
p->s[len - 1] = '\0';
if ((ptr = strchr(p->s, '\n')) != NULL)
*ptr = '\0';
return 1;
}
case BESHORT:
@ -284,7 +289,7 @@ struct magic *m;
return 1;
case BELONG:
case BEDATE:
p->l = (long)
p->l = (int32)
((p->hl[0]<<24)|(p->hl[1]<<16)|(p->hl[2]<<8)|(p->hl[3]));
return 1;
case LESHORT:
@ -292,7 +297,7 @@ struct magic *m;
return 1;
case LELONG:
case LEDATE:
p->l = (long)
p->l = (int32)
((p->hl[3]<<24)|(p->hl[2]<<16)|(p->hl[1]<<8)|(p->hl[0]));
return 1;
default:
@ -304,11 +309,11 @@ struct magic *m;
static void
mdebug(offset, str, len)
long offset;
int32 offset;
char *str;
int len;
{
(void) fprintf(stderr, "mget @%ld: ", offset);
(void) fprintf(stderr, "mget @%d: ", offset);
showstr(stderr, (char *) str, len);
(void) fputc('\n', stderr);
(void) fputc('\n', stderr);
@ -321,7 +326,7 @@ unsigned char *s;
struct magic *m;
int nbytes;
{
long offset = m->offset;
int32 offset = m->offset;
if (offset + sizeof(union VALUETYPE) <= nbytes)
memcpy(p, s + offset, sizeof(union VALUETYPE));
@ -330,7 +335,7 @@ int nbytes;
* the usefulness of padding with zeroes eludes me, it
* might even cause problems
*/
long have = nbytes - offset;
int32 have = nbytes - offset;
memset(p, 0, sizeof(union VALUETYPE));
if (have > 0)
memcpy(p, s + offset, have);
@ -380,8 +385,8 @@ mcheck(p, m)
union VALUETYPE* p;
struct magic *m;
{
register unsigned long l = m->value.l;
register unsigned long v;
register uint32 l = m->value.l;
register uint32 v;
int matched;
if ( (m->value.s[0] == 'x') && (m->value.s[1] == '\0') ) {
@ -424,7 +429,7 @@ struct magic *m;
register int len = m->vallen;
while (--len >= 0)
if ((v = *b++ - *a++) != 0)
if ((v = *b++ - *a++) != '\0')
break;
}
break;
@ -438,21 +443,21 @@ struct magic *m;
switch (m->reln) {
case 'x':
if (debug)
(void) fprintf(stderr, "%lu == *any* = 1\n", v);
(void) fprintf(stderr, "%u == *any* = 1\n", v);
matched = 1;
break;
case '!':
matched = v != l;
if (debug)
(void) fprintf(stderr, "%lu != %lu = %d\n",
(void) fprintf(stderr, "%u != %u = %d\n",
v, l, matched);
break;
case '=':
matched = v == l;
if (debug)
(void) fprintf(stderr, "%lu == %lu = %d\n",
(void) fprintf(stderr, "%u == %u = %d\n",
v, l, matched);
break;
@ -460,13 +465,13 @@ struct magic *m;
if (m->flag & UNSIGNED) {
matched = v > l;
if (debug)
(void) fprintf(stderr, "%lu > %lu = %d\n",
(void) fprintf(stderr, "%u > %u = %d\n",
v, l, matched);
}
else {
matched = (long) v > (long) l;
matched = (int32) v > (int32) l;
if (debug)
(void) fprintf(stderr, "%ld > %ld = %d\n",
(void) fprintf(stderr, "%d > %d = %d\n",
v, l, matched);
}
break;
@ -475,13 +480,13 @@ struct magic *m;
if (m->flag & UNSIGNED) {
matched = v < l;
if (debug)
(void) fprintf(stderr, "%lu < %lu = %d\n",
(void) fprintf(stderr, "%u < %u = %d\n",
v, l, matched);
}
else {
matched = (long) v < (long) l;
matched = (int32) v < (int32) l;
if (debug)
(void) fprintf(stderr, "%ld < %ld = %d\n",
(void) fprintf(stderr, "%d < %d = %d\n",
v, l, matched);
}
break;
@ -489,14 +494,14 @@ struct magic *m;
case '&':
matched = (v & l) == l;
if (debug)
(void) fprintf(stderr, "((%lx & %lx) == %lx) = %d\n",
(void) fprintf(stderr, "((%x & %x) == %x) = %d\n",
v, l, l, matched);
break;
case '^':
matched = (v & l) != l;
if (debug)
(void) fprintf(stderr, "((%lx & %lx) != %lx) = %d\n",
(void) fprintf(stderr, "((%x & %x) != %x) = %d\n",
v, l, l, matched);
break;