.TH FILE __CSECTION__ "Copyright but distributable"
-.\" $Id: file.man,v 1.25 1995/04/28 17:29:13 christos Exp $
+.\" $Id: file.man,v 1.26 1995/10/27 23:12:01 christos Exp $
.SH NAME
file
\- determine file type
namefile ]
[
.B \-m
-magicfile ]
+magicfiles ]
file ...
.SH DESCRIPTION
.I File
.B \-v
Print the version of the program and exit.
.TP 8
-.B \-m file
-Specify an alternate file of magic numbers.
+.B \-m list
+Specify an alternate list of files containing magic numbers.
+This can be a single file, or a colon-separated list of files.
.TP 8
.B \-z
Try to look inside compressed files.
.SH ENVIRONMENT
The environment variable
.B MAGIC
-can be used to set the default magic number file.
+can be used to set the default magic number files.
.SH SEE ALSO
.IR magic (__FSECTION__)
\- description of magic file format.
#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.22 1995/05/20 22:09:21 christos Exp $";
+ "@(#)$Id: apprentice.c,v 1.23 1995/10/27 23:12:01 christos Exp $";
#endif /* lint */
#define EATAB {while (isascii((unsigned char) *l) && \
static int maxmagic = 0;
+static int apprentice_1 __P((char *, int));
int
apprentice(fn, check)
-char *fn; /* name of magic file */
+char *fn; /* list of magic files */
int check; /* non-zero? checking-only run. */
{
- FILE *f;
- char line[BUFSIZ+1];
- int errs = 0;
-
- f = fopen(fn, "r");
- if (f==NULL) {
- (void) fprintf(stderr, "%s: can't read magic file %s\n",
- progname, fn);
- if (check)
- return -1;
- else
- exit(1);
- }
+ char *p, *mfn;
+ int file_err, errs = -1;
maxmagic = MAXMAGIS;
- if ((magic = (struct magic *) calloc(sizeof(struct magic), maxmagic))
- == NULL) {
+ 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);
if (check)
return -1;
else
exit(1);
}
+ fn = strcpy(mfn, fn);
+ while (fn) {
+ p = strchr(fn, ':');
+ if (p)
+ *p++ = '\0';
+ file_err = apprentice_1(fn, check);
+ if (file_err > errs)
+ errs = file_err;
+ fn = p;
+ }
+ if (errs == -1)
+ (void) fprintf(stderr, "%s: couldn't find any magic files!\n",
+ progname);
+ if (!check && errs)
+ exit(1);
+
+ free(mfn);
+ return errs;
+}
+
+static int
+apprentice_1(fn, check)
+char *fn; /* name of magic file */
+int check; /* non-zero? checking-only run. */
+{
+ static const char hdr[] =
+ "cont\toffset\ttype\topcode\tmask\tvalue\tdesc";
+ FILE *f;
+ char line[BUFSIZ+1];
+ int errs = 0;
+
+ 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));
+ return -1;
+ }
+
/* parse it */
if (check) /* print silly verbose header for USG compat. */
- (void) printf("cont\toffset\ttype\topcode\tmask\tvalue\tdesc\n");
+ (void) printf("%s\n", hdr);
for (lineno = 1;fgets(line, BUFSIZ, f) != NULL; lineno++) {
if (line[0]=='#') /* comment, do not parse */
continue;
line[strlen(line)-1] = '\0'; /* delete newline */
if (parse(line, &nmagic, check) != 0)
- ++errs;
+ errs = 1;
}
(void) fclose(f);
- return errs ? -1 : 0;
+ return errs;
}
/*
*/
#ifndef lint
static char *moduleid =
- "@(#)$Id: file.c,v 1.33 1995/05/20 22:09:21 christos Exp $";
+ "@(#)$Id: file.c,v 1.34 1995/10/27 23:12:01 christos Exp $";
#endif /* lint */
#include <stdio.h>
#include "file.h"
#ifdef S_IFLNK
-# define USAGE "Usage: %s [-vczL] [-f namefile] [-m magicfile] file...\n"
+# define USAGE "Usage: %s [-vczL] [-f namefile] [-m magicfiles] file...\n"
#else
-# define USAGE "Usage: %s [-vcz] [-f namefile] [-m magicfile] file...\n"
+# define USAGE "Usage: %s [-vcz] [-f namefile] [-m magicfiles] file...\n"
#endif
#ifndef MAGIC