1
0
mirror of https://git.FreeBSD.org/src.git synced 2024-11-29 08:08:37 +00:00

Add rcsid. RU#. Use err(3) instead of local redefinition. Convert man page to

mdoc format. Add usage(). Remove rcs $ Log $ -> use cvs log instead.
This commit is contained in:
Philippe Charnier 1998-01-28 07:36:25 +00:00
parent 0fc319ac44
commit 76cd8d66fe
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=32846
13 changed files with 337 additions and 499 deletions

View File

@ -25,18 +25,18 @@
* 4. This notice may not be removed or altered.
*/
#include <stdio.h>
#ifndef lint
static const char rcsid[] =
"$Id$";
#endif /* not lint */
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include "file.h"
#ifndef lint
static char *moduleid =
"@(#)$Id: apprentice.c,v 1.1.1.3 1997/03/18 17:58:42 mpp Exp $";
#endif /* lint */
#define EATAB {while (isascii((unsigned char) *l) && \
isspace((unsigned char) *l)) ++l;}
#define LOWCASE(l) (isupper((unsigned char) (l)) ? \
@ -65,7 +65,7 @@ int check; /* non-zero? checking-only run. */
magic = (struct magic *) calloc(sizeof(struct magic), maxmagic);
mfn = malloc(strlen(fn)+1);
if (magic == NULL || mfn == NULL) {
(void) fprintf(stderr, "%s: Out of memory.\n", progname);
warnx("out of memory");
if (check)
return -1;
else
@ -83,8 +83,7 @@ int check; /* non-zero? checking-only run. */
fn = p;
}
if (errs == -1)
(void) fprintf(stderr, "%s: couldn't find any magic files!\n",
progname);
warnx("couldn't find any magic files");
if (!check && errs)
exit(1);
@ -106,9 +105,7 @@ int check; /* non-zero? checking-only run. */
f = fopen(fn, "r");
if (f==NULL) {
if (errno != ENOENT)
(void) fprintf(stderr,
"%s: can't read magic file %s (%s)\n",
progname, fn, strerror(errno));
warn("can't read magic file %s", fn);
return -1;
}
@ -164,8 +161,8 @@ uint32 v;
case STRING:
break;
default:
magwarn("can't happen: m->type=%d\n",
m->type);
warnx("can't happen: m->type=%d in file %s, line %d",
m->type, magicfile, lineno);
return -1;
}
return v;
@ -189,7 +186,7 @@ int *ndx, check;
if ((magic = (struct magic *) realloc(magic,
sizeof(struct magic) *
maxmagic)) == NULL) {
(void) fprintf(stderr, "%s: Out of memory.\n", progname);
warnx("out of memory");
if (check)
return -1;
else
@ -218,7 +215,8 @@ int *ndx, check;
/* get offset, then skip over it */
m->offset = (int) strtoul(l,&t,0);
if (l == t)
magwarn("offset %s invalid", l);
warnx("offset %s invalid in file %s, line %d",
l, magicfile, lineno);
l = t;
if (m->flag & INDIR) {
@ -242,7 +240,9 @@ int *ndx, check;
m->in.type = BYTE;
break;
default:
magwarn("indirect offset type %c invalid", *l);
warnx(
"indirect offset type %c invalid in file %s, line %d",
*l, magicfile, lineno);
break;
}
l++;
@ -256,7 +256,9 @@ int *ndx, check;
else
t = l;
if (*t++ != ')')
magwarn("missing ')' in indirect offset");
warnx(
"missing ')' in indirect offset in file %s, line %d",
magicfile, lineno);
l = t;
}
@ -317,7 +319,8 @@ int *ndx, check;
m->type = LEDATE;
l += NLEDATE;
} else {
magwarn("type %s invalid", l);
warnx("type %s invalid in file %s, line %d", l,
magicfile, lineno);
return -1;
}
/* New-style anding: "0 byte&0x80 =0x80 dynamically linked" */
@ -363,7 +366,8 @@ int *ndx, check;
/*
* TODO finish this macro and start using it!
* #define offsetcheck {if (offset > HOWMANY-1)
* magwarn("offset too big"); }
* warnx("offset too big in file %s, line %d",
* magicfile, lineno); }
*/
/*
@ -434,7 +438,7 @@ int plen, *slen;
if (isspace((unsigned char) c))
break;
if (p >= pmax) {
fprintf(stderr, "String too long: %s\n", origs);
warnx("string too long: %s", origs);
break;
}
if(c == '\\') {

View File

@ -26,19 +26,16 @@
* 4. This notice may not be removed or altered.
*/
#include <stdio.h>
#include <string.h>
#ifndef lint
static const char rcsid[] =
"$Id$";
#endif /* not lint */
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "file.h"
#include "names.h"
#ifndef lint
static char *moduleid =
"@(#)$Id: ascmagic.c,v 1.7 1997/03/18 19:37:17 mpp Exp $";
#endif /* lint */
/* an optimisation over plain strcmp() */
#define STREQ(a, b) (*(a) == *(b) && strcmp((a), (b)) == 0)
@ -123,5 +120,3 @@ int nbytes; /* size actually read */
}
return 1;
}

View File

@ -4,13 +4,17 @@
* information if recognized
* uncompress(method, old, n, newch) - uncompress old into new,
* using method, return sizeof new
* $Id$
*/
#include <stdio.h>
#ifndef lint
static const char rcsid[] =
"$Id$";
#endif /* not lint */
#include <err.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#include "file.h"
@ -73,7 +77,7 @@ int n;
int fdin[2], fdout[2];
if (pipe(fdin) == -1 || pipe(fdout) == -1) {
error("cannot create pipe (%s).\n", strerror(errno));
err(1, "cannot create pipe");
/*NOTREACHED*/
}
switch (fork()) {
@ -91,28 +95,27 @@ int n;
(void) close(2);
execvp(compr[method].argv[0], compr[method].argv);
error("could not execute `%s' (%s).\n",
compr[method].argv[0], strerror(errno));
err(1, "could not execute `%s'", compr[method].argv[0]);
/*NOTREACHED*/
case -1:
error("could not fork (%s).\n", strerror(errno));
err(1, "could not fork");
/*NOTREACHED*/
default: /* parent */
(void) close(fdin[0]);
(void) close(fdout[1]);
if (write(fdin[1], old, n) != n) {
error("write failed (%s).\n", strerror(errno));
err(1, "write failed");
/*NOTREACHED*/
}
(void) close(fdin[1]);
if ((*newch = (unsigned char *) malloc(n)) == NULL) {
error("out of memory.\n");
errx(1, "out of memory");
/*NOTREACHED*/
}
if ((n = read(fdout[0], *newch, n)) <= 0) {
free(*newch);
error("read failed (%s).\n", strerror(errno));
err(1, "read failed");
/*NOTREACHED*/
}
(void) close(fdout[0]);

View File

@ -1,57 +1,57 @@
.TH FILE 1 "Copyright but distributable"
.\" $Id: file.1,v 1.9 1997/03/18 19:37:17 mpp Exp $
.SH NAME
file \- determine file type
.SH SYNOPSIS
.B file
[
.B \-vczL
]
[
.B \-f
namefile ]
[
.B \-m
magicfiles ]
file ...
.SH DESCRIPTION
.\" $Id: file.1,v 1.10 1997/04/04 02:41:01 mpp Exp $
.Dd July 30, 1997
.Dt FILE 1 "Copyright but distributable"
.Os
.Sh NAME
.Nm file
.Nd determine file type
.Sh SYNOPSIS
.Nm file
.Op Fl vczL
.Op Fl f Ar namefile
.Op Fl m Ar magicfiles
.Ar
.Sh DESCRIPTION
This manual page documents version 3.22 of the
.B file
.Nm
command.
.B File
.Nm File
tests each argument in an attempt to classify it.
There are three sets of tests, performed in this order:
filesystem tests, magic number tests, and language tests.
The
.I first
.Em first
test that succeeds causes the file type to be printed.
.PP
.Pp
The type printed will usually contain one of the words
.B text
.Em text
(the file contains only
.SM ASCII
.Tn ASCII
characters and is probably safe to read on an
.SM ASCII
.Tn ASCII
terminal),
.B executable
.Em executable
(the file contains the result of compiling a program
in a form understandable to some \s-1UNIX\s0 kernel or another),
in a form understandable to some
.Ux
kernel or another),
or
.B data
.Em data
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/share/misc/magic
.Pa /usr/share/misc/magic
or the program itself,
.B "preserve these keywords" .
.Em "preserve these keywords" .
.Pp
People depend on knowing that all the readable files in a directory
have the word ``text'' printed.
Don't do as Berkeley did \- change ``shell commands text''
to ``shell script''.
.PP
.Pp
The filesystem tests are based on examining the return from a
.BR stat (2)
.Xr stat 2
system call.
The program checks to see if the file is empty,
or if it's some sort of special file.
@ -60,103 +60,104 @@ Any known file types appropriate to the system you are running on
implement them)
are intuited if they are defined in
the system header file
.IR sys/stat.h .
.PP
.Pa sys/stat.h .
.Pp
The magic number tests are used to check for files with data in
particular fixed formats.
The canonical example of this is a binary executable (compiled program)
.I a.out
.Pa a.out
file, whose format is defined in
.I a.out.h
.Pa a.out.h
and possibly
.I exec.h
.Pa exec.h
in the standard include directory.
These files have a `magic number' stored in a particular place
near the beginning of the file that tells the \s-1UNIX\s0 operating system
near the beginning of the file that tells the
.Ux
operating system
that the file is a binary executable, and which of several types thereof.
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/share/misc/magic.
.PP
.Pa /usr/share/misc/magic .
.Pp
If an argument appears to be an
.SM ASCII
.Tn ASCII
file,
.B file
.Nm
attempts to guess its language.
The language tests look for particular strings (cf
.IR names.h )
.Pa names.h )
that can appear anywhere in the first few blocks of a file.
For example, the keyword
.B .br
.Em .br
indicates that the file is most likely a
.BR troff (1)
.Xr troff 1
input file, just as the keyword
.B struct
.Em struct
indicates a C program.
These tests are less reliable than the previous
two groups, so they are performed last.
The language test routines also test for some miscellany
(such as
.BR tar (1)
.Xr tar 1
archives) and determine whether an unknown file should be
labelled as `ascii text' or `data'.
.SH OPTIONS
.TP 8
.B \-v
.Sh OPTIONS
.Bl -tag -width indent
.It Fl v
Print the version of the program and exit.
.TP 8
.B \-m list
Specify an alternate list of files containing magic numbers.
.It Fl m Ar list
Specify an alternate
.Ar list
of files containing magic numbers.
This can be a single file, or a colon-separated list of files.
.TP 8
.B \-z
.It Fl z
Try to look inside compressed files.
.TP 8
.B \-c
.It Fl c
Cause a checking printout of the parsed form of the magic file.
This is usually used in conjunction with
.B \-m
.Fl m
to debug a new magic file before installing it.
.TP 8
.B \-f namefile
.It Fl f Ar namefile
Read the names of the files to be examined from
.I namefile
.Ar namefile
(one per line)
before the argument list.
Either
.I namefile
.Ar namefile
or at least one filename argument must be present;
to test the standard input, use ``-'' as a filename argument.
.TP 8
.B \-L
option causes symlinks to be followed, as the like-named option in
.BR ls (1).
.It Fl L
Cause symlinks to be followed, as the like-named option in
.Xr ls 1 .
(on systems that support symbolic links).
.SH FILES
.I /usr/share/misc/magic
\- default list of magic numbers (used to be
.I /etc/magic
in previous versions of FreeBSD)
.SH ENVIRONMENT
.El
.Sh FILES
.Bl -tag -width /usr/share/misc/magic -compact
.It Pa /usr/share/misc/magic
default list of magic numbers (used to be
.Pa /etc/magic
in previous versions of
.Bx Free )
.El
.Sh ENVIRONMENT
The environment variable
.B MAGIC
.Em MAGIC
can be used to set the default magic number files.
.SH SEE ALSO
.BR magic (5)
\- description of magic file format.
.br
.BR strings (1), " od" (1)
\- tools for examining non-textfiles.
.SH STANDARDS CONFORMANCE
.Sh SEE ALSO
.Xr od 1 ,
.Xr strings 1 ,
.Xr magic 5
.Sh STANDARDS CONFORMANCE
This program is believed to exceed the System V Interface Definition
of FILE(CMD), as near as one can determine from the vague language
contained therein.
Its behavior is mostly compatible with the System V program of the same name.
Its behaviour is mostly compatible with the System V program of the same name.
This version knows more magic, however, so it will produce
different (albeit more accurate) output in many cases.
.PP
.Pp
The one significant difference
between this version and System V
is that this version treats any white space
@ -178,189 +179,225 @@ in an existing magic file would have to be changed to
.br
0 string \e\ebegindata Andrew Toolkit document
.br
.PP
.Pp
SunOS releases 3.2 and later from Sun Microsystems include a
.BR file (1)
.Xr file 1
command derived from the System V one, but with some extensions.
My version differs from Sun's only in minor ways.
It includes the extension of the `&' operator, used as,
for example,
.br
>16 long&0x7fffffff >0 not stripped
.SH MAGIC DIRECTORY
.Sh MAGIC DIRECTORY
The magic file entries have been collected from various sources,
mainly USENET, and contributed by various authors.
Christos Zoulas (address below) will collect additional
.An Christos Zoulas
(address below) will collect additional
or corrected magic file entries.
A consolidation of magic file entries
will be distributed periodically.
.PP
.Pp
The order of entries in the magic file is significant.
Depending on what system you are using, the order that
they are put together may be incorrect.
If your old
.B file
.Nm
command uses a magic file,
keep the old magic file around for comparison purposes
(rename it to
.IR /usr/share/misc/magic.orig ).
.SH HISTORY
.Pa /usr/share/misc/magic.orig Ns ).
.Sh HISTORY
There has been a
.B file
command in every \s-1UNIX\s0 since at least Research Version 6
.Nm
command in every
.Ux
since at least Research Version 6
(man page dated January, 1975).
The System V version introduced one significant major change:
the external list of magic number types.
This slowed the program down slightly but made it a lot more flexible.
.PP
.Pp
This program, based on the System V version,
was written by Ian Darwin without looking at anybody else's source code.
.PP
John Gilmore revised the code extensively, making it better than
was written by
.An Ian Darwin
without looking at anybody else's source code.
.Pp
.An John Gilmore
revised the code extensively, making it better than
the first version.
Geoff Collyer found several inadequacies
.An Geoff Collyer
found several inadequacies
and provided some magic file entries.
The program has undergone continued evolution since.
.SH AUTHOR
Written by Ian F. Darwin, UUCP address {utzoo | ihnp4}!darwin!ian,
Internet address ian@sq.com,
.Sh AUTHORS
Written by
.An Ian F. Darwin ,
UUCP address {utzoo | ihnp4}!darwin!ian,
Internet address
.Aq ian@sq.com ,
postal address: P.O. Box 603, Station F, Toronto, Ontario, CANADA M4Y 2L8.
.PP
Altered by Rob McMahon, cudcv@warwick.ac.uk, 1989, to extend the `&' operator
.Pp
Altered by
.An Rob McMahon Aq cudcv@warwick.ac.uk ,
1989, to extend the `&' operator
from simple `x&y != 0' to `x&y op z'.
.PP
Altered by Guy Harris, guy@auspex.com, 1993, to:
.RS
.PP
.Pp
Altered by
.An Guy Harris Aq guy@auspex.com ,
1993, to:
.Bl -item -offset indent
.It
put the ``old-style'' `&'
operator back the way it was, because 1) Rob McMahon's change broke the
previous style of usage, 2) the SunOS ``new-style'' `&' operator,
which this version of
.B file
supports, also handles `x&y op z', and 3) Rob's change wasn't documented
in any case;
.PP
operator back the way it was, because
.Bl -enum -offset indent
.It
Rob McMahon's change broke the
previous style of usage,
.It
The SunOS ``new-style'' `&' operator, which this version of
.Nm
supports, also handles `x&y op z',
.It
Rob's change wasn't documented in any case;
.El
.It
put in multiple levels of `>';
.PP
.It
put in ``beshort'', ``leshort'', etc. keywords to look at numbers in the
file in a specific byte order, rather than in the native byte order of
the process running
.BR file .
.RE
.PP
Changes by Ian Darwin and various authors including
Christos Zoulas (christos@deshaw.com), 1990-1992.
.SH LEGAL NOTICE
.Nm file .
.El
.Pp
Changes by
.An Ian Darwin
and various authors including
.An Christos Zoulas Aq christos@deshaw.com ,
1990-1992.
.Sh LEGAL NOTICE
Copyright (c) Ian F. Darwin, Toronto, Canada,
1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993.
.PP
.Pp
This software is not subject to and may not be made subject to any
license of the American Telephone and Telegraph Company, Sun
Microsystems Inc., Digital Equipment Inc., Lotus Development Inc., the
Regents of the University of California, The X Consortium or MIT, or
The Free Software Foundation.
.PP
.Pp
This software is not subject to any export provision of the United States
Department of Commerce, and may be exported to any country or planet.
.PP
.Pp
Permission is granted to anyone to use this software for any purpose on
any computer system, and to alter it and redistribute it freely, subject
to the following restrictions:
.PP
1. The author is not responsible for the consequences of use of this
software, no matter how awful, even if they arise from flaws in it.
.PP
2. The origin of this software must not be misrepresented, either by
.Pp
.Bl -enum -offset indent
.It
The author is not responsible for the consequences of use of this
software, no matter how awful, even if they arise from flaws in it;
.It
The origin of this software must not be misrepresented, either by
explicit claim or by omission. Since few users ever read sources,
credits must appear in the documentation.
.PP
3. Altered versions must be plainly marked as such, and must not be
credits must appear in the documentation;
.It
Altered versions must be plainly marked as such, and must not be
misrepresented as being the original software. Since few users
ever read sources, credits must appear in the documentation.
.PP
4. This notice may not be removed or altered.
.PP
A few support files (\fIgetopt\fP, \fIstrtok\fP)
ever read sources, credits must appear in the documentation;
.It
This notice may not be removed or altered.
.El
.Pp
A few support files (
.Fn getopt ,
.Fn strtok )
distributed with this package
are by Henry Spencer and are subject to the same terms as above.
.PP
A few simple support files (\fIstrtol\fP, \fIstrchr\fP)
are by
.An Henry Spencer
and are subject to the same terms as above.
.Pp
A few simple support files (
.Fn strtol ,
.Fn strchr )
distributed with this package
are in the public domain; they are so marked.
.PP
.Pp
The files
.I tar.h
.Pa tar.h
and
.I is_tar.c
were written by John Gilmore from his public-domain
.B tar
.Pa is_tar.c
were written by
.An John Gilmore
from his public-domain
.Nm tar
program, and are not covered by the above restrictions.
.SH BUGS
.Sh BUGS
There must be a better way to automate the construction of the Magic
file from all the glop in Magdir. What is it?
Better yet, the magic file should be compiled into binary (say,
.BR ndbm (3)
.Xr ndbm 3
or, better yet, fixed-length
.SM ASCII
.Tn ASCII
strings for use in heterogenous network environments) for faster startup.
Then the program would run as fast as the Version 7 program of the same name,
with the flexibility of the System V version.
.PP
.B File
.Pp
.Nm File
uses several algorithms that favor speed over accuracy,
thus it can be misled about the contents of
.SM ASCII
.Tn ASCII
files.
.PP
.Pp
The support for
.SM ASCII
.Tn ASCII
files (primarily for programming languages)
is simplistic, inefficient and requires recompilation to update.
.PP
.Pp
There should be an ``else'' clause to follow a series of continuation lines.
.PP
.Pp
The magic file and keywords should have regular expression support.
Their use of
.SM "ASCII TAB"
.Tn ASCII TAB
as a field delimiter is ugly and makes
it hard to edit the files, but is entrenched.
.PP
.Pp
It might be advisable to allow upper-case letters in keywords
for e.g.,
.BR troff (1)
.Xr troff 1
commands vs man page macros.
Regular expression support would make this easy.
.PP
.Pp
The program doesn't grok \s-2FORTRAN\s0.
It should be able to figure \s-2FORTRAN\s0 by seeing some keywords which
appear indented at the start of line.
Regular expression support would make this easy.
.PP
.Pp
The list of keywords in
.I ascmagic
.Em ascmagic
probably belongs in the Magic file.
This could be done by using some keyword like `*' for the offset value.
.PP
.Pp
Another optimization would be to sort
the magic file so that we can just run down all the
tests for the first byte, first word, first long, etc, once we
have fetched it. Complain about conflicts in the magic file entries.
Make a rule that the magic entries sort based on file offset rather
than position within the magic file?
.PP
.Pp
The program should provide a way to give an estimate
of ``how good'' a guess is.
We end up removing guesses (e.g. ``From '' as first 5 chars of file) because
they are not as good as other guesses (e.g. ``Newsgroups:'' versus
"Return-Path:"). Still, if the others don't pan out, it should be
possible to use the first guess.
.PP
This program is slower than some vendors' file commands.
.PP
.Pp
This program is slower than some vendors'
.Nm
commands.
.Pp
This manual page, and particularly this section, is too long.
.SH AVAILABILITY
.Sh AVAILABILITY
You can obtain the original author's latest version by anonymous FTP
on
.B ftp.deshaw.com
.Em ftp.deshaw.com
in the directory
.I /pub/file/file-X.YY.tar.gz
.Pa /pub/file/file-X.YY.tar.gz

View File

@ -24,18 +24,18 @@
*
* 4. This notice may not be removed or altered.
*/
#ifndef lint
static char *moduleid =
"@(#)$Id: file.c,v 1.7 1997/03/18 19:37:18 mpp Exp $";
#endif /* lint */
#include <stdio.h>
#ifndef lint
static const char rcsid[] =
"$Id$";
#endif /* not lint */
#include <err.h>
#include <fcntl.h> /* for open() */
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/param.h> /* for MAXPATHLEN */
#include <sys/stat.h>
#include <fcntl.h> /* for open() */
#if (__COHERENT__ >= 0x420)
# include <sys/utime.h>
#else
@ -47,15 +47,13 @@ static char *moduleid =
#endif
#include <unistd.h> /* for read() */
#include <netinet/in.h> /* for byte swapping */
#include "patchlevel.h"
#include "file.h"
#ifdef S_IFLNK
# define USAGE "Usage: %s [-vczL] [-f namefile] [-m magicfiles] file...\n"
# define USAGE "usage: file [-vczL] [-f namefile] [-m magicfiles] file...\n"
#else
# define USAGE "Usage: %s [-vcz] [-f namefile] [-m magicfiles] file...\n"
# define USAGE "usage: file [-vcz] [-f namefile] [-m magicfiles] file...\n"
#endif
#ifndef MAGIC
@ -74,11 +72,11 @@ struct magic *magic; /* array of magic entries */
char *magicfile; /* where magic be found */
char *progname; /* used throughout */
int lineno; /* line number in the magic file */
static void unwrap __P((char *fn));
static void usage __P((void));
#if 0
static int byteconv4 __P((int, int, int));
static short byteconv2 __P((int, int, int));
@ -95,18 +93,13 @@ 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,
(void) fprintf(stdout, "file-%d.%d\n",
FILE_VERSION_MAJOR, patchlevel);
return 1;
case 'c':
@ -142,10 +135,8 @@ char *argv[];
break;
}
if (errflg) {
(void) fprintf(stderr, USAGE, progname);
exit(2);
}
if (errflg)
usage();
if (!app) {
ret = apprentice(magicfile, check);
@ -155,10 +146,8 @@ char *argv[];
}
if (optind == argc) {
if (!didsomefiles) {
(void)fprintf(stderr, USAGE, progname);
exit(2);
}
if (!didsomefiles)
usage();
}
else {
int i, wid, nw;
@ -174,6 +163,12 @@ char *argv[];
return 0;
}
static void
usage()
{
fprintf(stderr, USAGE);
exit(2);
}
/*
* unwrap -- read a file of filenames, do each one.
@ -191,7 +186,7 @@ char *fn;
wid = 1;
} else {
if ((f = fopen(fn, "r")) == NULL) {
error("Cannot open `%s' (%s).\n", fn, strerror(errno));
err(1, "cannot open `%s'", fn);
/*NOTREACHED*/
}
@ -295,8 +290,7 @@ int wid;
if (strcmp("-", inname) == 0) {
if (fstat(0, &sb)<0) {
error("cannot fstat `%s' (%s).\n", stdname,
strerror(errno));
err(1, "cannot fstat `%s'", stdname);
/*NOTREACHED*/
}
inname = stdname;
@ -319,7 +313,7 @@ int wid;
/* We can't open it, but we were able to stat it. */
if (sb.st_mode & 0002) ckfputs("writeable, ", stdout);
if (sb.st_mode & 0111) ckfputs("executable, ", stdout);
ckfprintf(stdout, "can't read `%s' (%s).\n",
ckfprintf(stdout, "can't read `%s': %s.\n",
inname, strerror(errno));
return;
}
@ -330,7 +324,7 @@ int wid;
* try looking at the first HOWMANY bytes
*/
if ((nbytes = read(fd, (char *)buf, HOWMANY)) == -1) {
error("read failed (%s).\n", strerror(errno));
err(1, "read failed");
/*NOTREACHED*/
}
@ -372,12 +366,12 @@ int wid;
int
tryit(buf, nb, zflag)
tryit(buf, nb, lzflag)
unsigned char *buf;
int nb, zflag;
int nb, lzflag;
{
/* try compression stuff */
if (zflag && zmagic(buf, nb))
if (lzflag && zmagic(buf, nb))
return 'z';
/* try tests in /etc/magic (or surrogate magic file) */

View File

@ -1,6 +1,6 @@
/*
* file.h - definitions for file(1) program
* @(#)$Id: file.h,v 1.1.1.3 1997/03/18 17:58:51 mpp Exp $
* @(#)$Id: file.h,v 1.7 1997/03/18 19:37:18 mpp Exp $
*
* Copyright (c) Ian F. Darwin, 1987.
* Written by Ian F. Darwin.
@ -106,13 +106,11 @@ extern int tryit __P((unsigned char *, int, int));
extern int zmagic __P((unsigned char *, int));
extern void ckfprintf __P((FILE *, const char *, ...));
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.. */
extern char *progname; /* the program name */
extern char *magicfile; /* name of the magic file */
extern int lineno; /* current line number in magic file */
@ -124,9 +122,6 @@ extern int debug; /* enable debugging? */
extern int zflag; /* process compressed files? */
extern int lflag; /* follow symbolic links? */
extern int optind; /* From getopt(3) */
extern char *optarg;
#if defined(sun) || defined(__sun__) || defined (__sun)
# if defined(__svr4) || defined (__SVR4) || defined(__svr4__)
# define SOLARIS

View File

@ -25,12 +25,15 @@
* 4. This notice may not be removed or altered.
*/
#include <stdio.h>
#ifndef lint
static const char rcsid[] =
"$Id$";
#endif /* not lint */
#include <err.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#ifndef major
# if defined(__SVR4) || defined(_SVR4_SOURCE)
# include <sys/mkdev.h>
@ -49,11 +52,6 @@
#include "file.h"
#ifndef lint
static char *moduleid =
"@(#)$Id: fsmagic.c,v 1.1.1.3 1997/03/18 17:58:44 mpp Exp $";
#endif /* lint */
int
fsmagic(fn, sb)
const char *fn;
@ -76,7 +74,7 @@ struct stat *sb;
ckfprintf(stdout,
/* Yes, I do mean stdout. */
/* No \n, caller will provide. */
"can't stat `%s' (%s).", fn, strerror(errno));
"can't stat `%s': %s.", fn, strerror(errno));
return 1;
}
@ -110,7 +108,7 @@ struct stat *sb;
struct stat tstatbuf;
if ((nch = readlink(fn, buf, BUFSIZ-1)) <= 0) {
ckfprintf(stdout, "unreadable symlink (%s).",
ckfprintf(stdout, "unreadable symlink: %s.",
strerror(errno));
return 1;
}
@ -165,7 +163,7 @@ struct stat *sb;
case S_IFREG:
break;
default:
error("invalid mode 0%o.\n", sb->st_mode);
err(1, "invalid mode 0%o", sb->st_mode);
/*NOTREACHED*/
}
@ -178,4 +176,3 @@ struct stat *sb;
}
return 0;
}

View File

@ -1,3 +1,9 @@
#ifndef lint
static const char rcsid[] =
"$Id$";
#endif /* not lint */
#include "file.h"
#include <string.h>

View File

@ -5,13 +5,16 @@
* Pubic Domain version written 26 Aug 1985 John Gilmore (ihnp4!hoptoad!gnu).
*
* @(#)list.c 1.18 9/23/86 Public Domain - gnu
* $Id: is_tar.c,v 1.1.1.2 1997/03/18 17:58:48 mpp Exp $
*
* Comments changed and some code/comments reformatted
* for file command by Ian Darwin.
*/
#include <string.h>
#ifndef lint
static const char rcsid[] =
"$Id$";
#endif /* not lint */
#include <ctype.h>
#include <sys/types.h>
#include "tar.h"

View File

@ -3,144 +3,7 @@
/*
* Patchlevel file for Ian Darwin's MAGIC command.
* $Id: patchlevel.h,v 1.1.1.3 1997/03/18 17:58:54 mpp Exp $
*
* $Log: patchlevel.h,v $
* Revision 1.6 1997/02/22 19:54:59 peter
* Revert $FreeBSD$ to $Id$
*
* Revision 1.5 1997/02/22 19:29:15 peter
* Revert $Id: patchlevel.h,v 1.6 1997/02/22 19:54:59 peter Exp $ to $Id: patchlevel.h,v 1.6 1997/02/22 19:54:59 peter Exp $
*
* Revision 1.4 1997/01/14 06:59:48 jkh
* Make the long-awaited change from $Id: patchlevel.h,v 1.6 1997/02/22 19:54:59 peter Exp $ to $Id: patchlevel.h,v 1.6 1997/02/22 19:54:59 peter Exp $
*
* This will make a number of things easier in the future, as well as (finally!)
* avoiding the Id-smashing problem which has plagued developers for so long.
*
* Boy, I'm glad we're not using sup anymore. This update would have been
* insane otherwise.
*
* Revision 1.3 1996/01/23 12:40:20 mpp
* Merged changes to resolve conflicts with file 3.19 import.
*
* Revision 1.2 1995/05/30 06:30:06 rgrimes
* Remove trailing whitespace.
*
* Revision 1.1.1.2 1996/01/22 22:31:44 mpp
* Upgrade to file version 3.19.
*
* Revision 1.1.1.1 1994/09/03 19:16:23 csgr
* Bring in file 3.14 by Ian Darwin (and Christos Zoulas)
*
* The following files were moved to different names:
* - file.man -> file.1
* - magic.man -> magic.5
*
* The following file was removed:
* - Magdir/Makefile
*
* Revision 1.1.1.3 1997/03/18 17:58:54 mpp
* Upgrade to file version 3.22.
*
* Obtained from: ftp://ftp.deshaw.com/pub/file/file-3.22.tar.gz
*
* 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
* More magic fixes
*
* Revision 1.20 1996/06/22 22:15:52 christos
* - support relative offsets of the form >&
* - fix bug with truncating magic strings that contain \n
* - file -f - did not read from stdin as documented
* - support elf file parsing using our own elf support.
* - as always magdir fixes and additions.
*
* Revision 1.19 1995/10/27 23:14:46 christos
* Ability to parse colon separated list of magic files
* New LEGAL.NOTICE
* Various magic file changes
*
* Revision 1.18 1995/05/20 22:09:21 christos
* Passed incorrect argument to eatsize().
* Use %ld and %lx where appropriate.
* Remove unused variables
* ELF support for both big and little endian
* Fixes for small files again.
*
* Revision 1.17 1995/04/28 17:29:13 christos
* - Incorrect nroff detection fix from der Mouse
* - Lost and incorrect magic entries.
* - Added ELF stripped binary detection [in C; ugh]
* - Look for $MAGIC to find the magic file.
* - Eat trailing size specifications from numbers i.e. ignore 10L
* - More fixes for very short files
*
* Revision 1.16 1995/03/25 22:06:45 christos
* - use strtoul() where it exists.
* - fix sign-extend bug
* - try to detect tar archives before nroff files, otherwise
* tar files where the first file starts with a . will not work
*
* Revision 1.15 1995/01/21 21:03:35 christos
* Added CSECTION for the file man page
* Added version flag -v
* Fixed bug with -f input flag (from iorio@violet.berkeley.edu)
* Lots of magic fixes and reorganization...
*
* Revision 1.14 1994/05/03 17:58:23 christos
* changes from mycroft@gnu.ai.mit.edu (Charles Hannum) for unsigned
*
* Revision 1.13 1994/01/21 01:27:01 christos
* Fixed null termination bug from Don Seeley at BSDI in ascmagic.c
*
* Revision 1.12 1993/10/27 20:59:05 christos
* Changed -z flag to understand gzip format too.
* Moved builtin compression detection to a table, and move
* the compress magic entry out of the source.
* Made printing of numbers unsigned, and added the mask to it.
* Changed the buffer size to 8k, because gzip will refuse to
* unzip just a few bytes.
*
* Revision 1.11 1993/09/24 18:49:06 christos
* Fixed small bug in softmagic.c introduced by
* copying the data to be examined out of the input
* buffer. Changed the Makefile to use sed to create
* the correct man pages.
*
* Revision 1.10 1993/09/23 21:56:23 christos
* Passed purify. Fixed indirections. Fixed byte order printing.
* Fixed segmentation faults caused by referencing past the end
* of the magic buffer. Fixed bus errors caused by referencing
* unaligned shorts or longs.
*
* Revision 1.9 1993/03/24 14:23:40 ian
* Batch of minor changes from several contributors.
*
* Revision 1.8 93/02/19 15:01:26 ian
* Numerous changes from Guy Harris too numerous to mention but including
* byte-order independance, fixing "old-style masking", etc. etc. A bugfix
* for broken symlinks from martin@@d255s004.zfe.siemens.de.
*
* Revision 1.7 93/01/05 14:57:27 ian
* Couple of nits picked by Christos (again, thanks).
*
* Revision 1.6 93/01/05 13:51:09 ian
* Lotsa work on the Magic directory.
*
* Revision 1.5 92/09/14 14:54:51 ian
* Fix a tiny null-pointer bug in previous fix for tar archive + uncompress.
* $Id: patchlevel.h,v 1.7 1997/03/18 19:37:21 mpp Exp $
*
*/

View File

@ -25,7 +25,12 @@
* 4. This notice may not be removed or altered.
*/
#include <stdio.h>
#ifndef lint
static const char rcsid[] =
"$Id$";
#endif /* not lint */
#include <err.h>
#include <errno.h>
#include <string.h>
#if __STDC__
@ -33,16 +38,9 @@
#else
# include <varargs.h>
#endif
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include "file.h"
#ifndef lint
static char *moduleid =
"@(#)$Id: print.c,v 1.1.1.3 1997/03/18 17:58:49 mpp Exp $";
#endif /* lint */
#define SZOF(a) (sizeof(a) / sizeof(a[0]))
void
@ -117,7 +115,7 @@ ckfputs(str, fil)
FILE *fil;
{
if (fputs(str,fil) == EOF)
error("write failed.\n");
errx(1, "write failed");
}
/*VARARGS*/
@ -141,64 +139,6 @@ ckfprintf(va_alist)
#endif
(void) vfprintf(f, fmt, va);
if (ferror(f))
error("write failed.\n");
errx(1, "write failed");
va_end(va);
}
/*
* error - print best error message possible and exit
*/
/*VARARGS*/
void
#if __STDC__
error(const char *f, ...)
#else
error(va_alist)
va_dcl
#endif
{
va_list va;
#if __STDC__
va_start(va, f);
#else
const char *f;
va_start(va);
f = va_arg(va, const char *);
#endif
/* cuz we use stdout for most, stderr here */
(void) fflush(stdout);
if (progname != NULL)
(void) fprintf(stderr, "%s: ", progname);
(void) vfprintf(stderr, f, va);
va_end(va);
exit(1);
}
/*VARARGS*/
void
#if __STDC__
magwarn(const char *f, ...)
#else
magwarn(va_alist)
va_dcl
#endif
{
va_list va;
#if __STDC__
va_start(va, f);
#else
const char *f;
va_start(va);
f = va_arg(va, const char *);
#endif
/* cuz we use stdout for most, stderr here */
(void) fflush(stdout);
if (progname != NULL)
(void) fprintf(stderr, "%s: %s, %d: ",
progname, magicfile, lineno);
(void) vfprintf(stderr, f, va);
va_end(va);
fputc('\n', stderr);
}

View File

@ -1,12 +1,14 @@
#ifndef lint
static const char rcsid[] =
"$Id$";
#endif /* not lint */
#ifdef BUILTIN_ELF
#include <sys/types.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <err.h>
#include <errno.h>
#include <unistd.h>
#include "readelf.h"
#include "file.h"
@ -34,11 +36,11 @@ doshn(fd, off, num, size, buf)
Elf32_Shdr *sh = (Elf32_Shdr *) buf;
if (lseek(fd, off, SEEK_SET) == -1)
error("lseek failed (%s).\n", strerror(errno));
err(1, "lseek failed");
for ( ; num; num--) {
if (read(fd, buf, size) == -1)
error("read failed (%s).\n", strerror(errno));
err(1, "read failed");
if (sh->sh_type == SHT_SYMTAB) {
(void) printf (", not stripped");
return;
@ -64,11 +66,11 @@ dophn_exec(fd, off, num, size, buf)
Elf32_Phdr *ph = (Elf32_Phdr *) buf;
if (lseek(fd, off, SEEK_SET) == -1)
error("lseek failed (%s).\n", strerror(errno));
err(1, "lseek failed");
for ( ; num; num--) {
if (read(fd, buf, size) == -1)
error("read failed (%s).\n", strerror(errno));
err(1, "read failed");
if (ph->p_type == PT_INTERP) {
/*
* Has an interpreter - must be a dynamically-linked
@ -125,17 +127,17 @@ dophn_core(fd, off, num, size, buf)
for ( ; num; num--) {
if (lseek(fd, off, SEEK_SET) == -1)
error("lseek failed (%s).\n", strerror(errno));
err(1, "lseek failed");
if (read(fd, buf, size) == -1)
error("read failed (%s).\n", strerror(errno));
err(1, "read failed");
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));
err(1, "lseek failed");
bufsize = read(fd, nbuf, BUFSIZ);
if (bufsize == -1)
error("read failed (%s).\n", strerror(errno));
err(1, "read failed");
offset = 0;
for (;;) {
if (offset >= bufsize)

View File

@ -25,19 +25,18 @@
* 4. This notice may not be removed or altered.
*/
#include <stdio.h>
#ifndef lint
static const char rcsid[] =
"$Id$";
#endif /* not lint */
#include <err.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include "file.h"
#ifndef lint
static char *moduleid =
"@(#)$Id: softmagic.c,v 1.1.1.3 1997/03/18 17:58:45 mpp Exp $";
#endif /* lint */
static int match __P((unsigned char *, int));
static int mget __P((union VALUETYPE *,
unsigned char *, struct magic *, int));
@ -105,7 +104,7 @@ int nbytes;
if (tmpoff == NULL)
if ((tmpoff = (int32 *) malloc(tmplen = 20)) == NULL)
error("out of memory\n");
errx(1, "out of memory");
for (magindex = 0; magindex < nmagic; magindex++) {
/* if main entry matches, print it... */
@ -132,7 +131,7 @@ int nbytes;
if (++cont_level >= tmplen)
if ((tmpoff = (int32 *) realloc(tmpoff,
tmplen += 20)) == NULL)
error("out of memory\n");
errx(1, "out of memory");
while (magic[magindex+1].cont_level != 0 &&
++magindex < nmagic) {
if (cont_level >= magic[magindex].cont_level) {
@ -177,7 +176,7 @@ int nbytes;
if ((tmpoff =
(int32 *) realloc(tmpoff,
tmplen += 20)) == NULL)
error("out of memory\n");
errx(1, "out of memory");
}
if (magic[magindex].flag & ADD) {
magic[magindex].offset = oldoff;
@ -252,7 +251,7 @@ struct magic *m;
break;
default:
error("invalid m->type (%d) in mprint().\n", m->type);
errx(1, "invalid m->type (%d) in mprint()", m->type);
/*NOTREACHED*/
}
return(t);
@ -299,7 +298,7 @@ struct magic *m;
((p->hl[3]<<24)|(p->hl[2]<<16)|(p->hl[1]<<8)|(p->hl[0]));
return 1;
default:
error("invalid type %d in mconvert().\n", m->type);
errx(1, "invalid type %d in mconvert()", m->type);
return 0;
}
}
@ -432,7 +431,7 @@ struct magic *m;
}
break;
default:
error("invalid type %d in mcheck().\n", m->type);
errx(1, "invalid type %d in mcheck()", m->type);
return 0;/*NOTREACHED*/
}
@ -505,7 +504,7 @@ struct magic *m;
default:
matched = 0;
error("mcheck: can't happen: invalid relation %d.\n", m->reln);
errx(1, "mcheck: can't happen: invalid relation %d", m->reln);
break;/*NOTREACHED*/
}