Use %ld and %lx where appropriate.
Remove unused variables
ELF support for both big and little endian
Fixes for small files again.
#ifndef lint
static char *moduleid =
- "@(#)$Id: apprentice.c,v 1.21 1995/04/28 17:29:13 christos Exp $";
+ "@(#)$Id: apprentice.c,v 1.22 1995/05/20 22:09:21 christos Exp $";
#endif /* lint */
#define EATAB {while (isascii((unsigned char) *l) && \
struct magic *m;
char *t, *s;
+#define ALLOC_INCR 20
if (nd+1 >= maxmagic){
- maxmagic += 20;
+ maxmagic += ALLOC_INCR;
if ((magic = (struct magic *) realloc(magic,
sizeof(struct magic) *
maxmagic)) == NULL) {
else
exit(1);
}
+ memset(&magic[*ndx], 0, sizeof(struct magic) * ALLOC_INCR);
}
m = &magic[*ndx];
m->flag = 0;
} else
if (m->reln != 'x') {
m->value.l = signextend(m, strtoul(*p, p, 0));
- eatsize(&p);
+ eatsize(p);
}
return 0;
}
/*
- * Ascii magic -- file types that we know based on keywords
+ * ASCII magic -- file types that we know based on keywords
* that can appear anywhere in the file.
*
* Copyright (c) Ian F. Darwin, 1987.
#ifndef lint
static char *moduleid =
- "@(#)$Id: ascmagic.c,v 1.19 1995/04/28 17:29:13 christos Exp $";
+ "@(#)$Id: ascmagic.c,v 1.20 1995/05/20 22:09:21 christos Exp $";
#endif /* lint */
/* an optimisation over plain strcmp() */
unsigned char *buf;
int nbytes; /* size actually read */
{
- int i, isblock, has_escapes = 0;
+ int i, has_escapes = 0;
unsigned char *s;
char nbuf[HOWMANY+1]; /* one extra for terminating '\0' */
char *token;
for (i = 0; i < nbytes; i++) {
if (!isascii(buf[i]))
- return 0; /* not all ascii */
+ return 0; /* not all ASCII */
}
- /* all else fails, but it is ascii... */
- ckfputs("ascii text", stdout);
+ /* all else fails, but it is ASCII... */
+ ckfputs("ASCII text", stdout);
if (has_escapes) {
ckfputs(" (with escape sequences)", stdout);
}
* information if recognized
* uncompress(method, old, n, newch) - uncompress old into new,
* using method, return sizeof new
- * $Id: compress.c,v 1.8 1994/01/21 01:38:24 christos Exp $
+ * $Id: compress.c,v 1.9 1995/05/20 22:09:21 christos Exp $
*/
#include <stdio.h>
#include <stdlib.h>
char *argv[3];
int silent;
} compr[] = {
- { "\037\235", 2, { "uncompress", "-c", NULL }, 0 },
- { "\037\213", 2, { "gzip", "-dq", NULL }, 1 },
- /*
- * XXX pcat does not work, cause I don't know how to make it read stdin,
- * so we use gzip
- */
- { "\037\036", 2, { "gzip", "-dq", NULL }, 0 },
+ { "\037\235", 2, { "uncompress", "-c", NULL }, 0 }, /* compressed */
+ { "\037\213", 2, { "gzip", "-cdq", NULL }, 1 }, /* gzipped */
+ { "\037\236", 2, { "gzip", "-cdq", NULL }, 1 }, /* frozen */
+ { "\037\240", 2, { "gzip", "-cdq", NULL }, 1 }, /* SCO LZH */
+ /* the standard pack utilities do not accept standard input */
+ { "\037\036", 2, { "gzip", "-cdq", NULL }, 0 }, /* packed */
};
static int ncompr = sizeof(compr) / sizeof(compr[0]);
return n;
}
}
-
-
*/
#ifndef lint
static char *moduleid =
- "@(#)$Id: file.c,v 1.32 1995/04/28 17:29:13 christos Exp $";
+ "@(#)$Id: file.c,v 1.33 1995/05/20 22:09:21 christos Exp $";
#endif /* lint */
#include <stdio.h>
#endif
#include <unistd.h> /* for read() */
-#ifdef HAVE_ELF
+#ifdef __ELF__
#include <elf.h>
#endif
struct utimbuf utbuf;
struct stat sb;
int nbytes = 0; /* number of bytes read from a datafile */
+ char match = '\0';
if (strcmp("-", inname) == 0) {
if (fstat(0, &sb)<0) {
- error("cannot fstat `%s' (%s).\n", stdname,
+ error("cannot fstat `%s' (%s).\n", stdname,
strerror(errno));
/*NOTREACHED*/
}
putchar('\n');
return;
}
-
+
if ((fd = open(inname, O_RDONLY)) < 0) {
/* We can't open it, but we were able to stat it. */
if (sb.st_mode & 0002) ckfputs("writeable, ", stdout);
/*NOTREACHED*/
}
- if (nbytes == 0)
+ if (nbytes == 0)
ckfputs("empty", stdout);
else {
- buf[nbytes++] = '\0'; /* NULL terminate */
- tryit(buf, nbytes, zflag);
+ buf[nbytes++] = '\0'; /* null-terminate it */
+ match = tryit(buf, nbytes, zflag);
}
-
-#ifdef HAVE_ELF
+#ifdef __ELF__
/*
* ELF executables have multiple section headers in arbitrary
* file locations and thus file(1) cannot determine it from easily.
* Should come up with a better fix.
*/
- if (nbytes > sizeof (Elf32_Ehdr) &&
+ if (match == 's' && nbytes > sizeof (Elf32_Ehdr) &&
buf[EI_MAG0] == ELFMAG0 &&
buf[EI_MAG1] == ELFMAG1 &&
buf[EI_MAG2] == ELFMAG2 &&
- buf[EI_MAG3] == ELFMAG3 ) {
+ buf[EI_MAG3] == ELFMAG3) {
+ union {
+ long l;
+ char c[sizeof (long)];
+ } u;
Elf32_Ehdr elfhdr;
int stripped = 1;
-
+
+ u.l = 1;
(void) memcpy(&elfhdr, buf, sizeof elfhdr);
-
- if (lseek(fd, elfhdr.e_shoff, SEEK_SET)<0)
- error("lseek failed (%s).\n", strerror(errno));
-
- for ( ; elfhdr.e_shnum ; elfhdr.e_shnum--) {
+
+ /*
+ * 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 (lseek(fd, elfhdr.e_shoff, SEEK_SET)<0)
+ error("lseek failed (%s).\n", strerror(errno));
+
+ for ( ; elfhdr.e_shnum ; elfhdr.e_shnum--) {
if (read(fd, buf, elfhdr.e_shentsize)<0)
- error("read failed (%s).\n", strerror(errno));
+ error("read failed (%s).\n", strerror(errno));
if (((Elf32_Shdr *)&buf)->sh_type == SHT_SYMTAB) {
- stripped = 0;
- break;
+ stripped = 0;
+ break;
}
+ }
+ if (stripped)
+ (void) printf (", stripped");
}
- if (stripped)
- (void) printf (" - stripped");
}
#endif
}
-void
+int
tryit(buf, nb, zflag)
unsigned char *buf;
int nb, zflag;
{
- /*
- * Try compression stuff
- */
- if (!zflag || zmagic(buf, nb) != 1)
- /*
- * try tests in /etc/magic (or surrogate magic file)
- */
- if (softmagic(buf, nb) != 1)
- /*
- * try known keywords, check for ascii-ness too.
- */
- if (ascmagic(buf, nb) != 1)
- /*
- * abandon hope, all ye who remain here
- */
- ckfputs("data", stdout);
+ /* try compression stuff */
+ if (zflag && zmagic(buf, nb))
+ return 'z';
+
+ /* try tests in /etc/magic (or surrogate magic file) */
+ if (softmagic(buf, nb))
+ return 's';
+
+ /* try known keywords, check whether it is ASCII */
+ if (ascmagic(buf, nb))
+ return 'a';
+
+ /* abandon hope, all ye who remain here */
+ ckfputs("data", stdout);
+ return '\0';
}
/*
* file.h - definitions for file(1) program
- * @(#)$Id: file.h,v 1.21 1995/04/28 17:29:13 christos Exp $
+ * @(#)$Id: file.h,v 1.22 1995/05/20 22:09:21 christos Exp $
*
* Copyright (c) Ian F. Darwin, 1987.
* Written by Ian F. Darwin.
extern void process __P((const char *, int));
extern void showstr __P((FILE *, const char *, int));
extern int softmagic __P((unsigned char *, int));
-extern void tryit __P((unsigned char *, int, 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));
#define FILE_VERSION_MAJOR 3
-#define patchlevel 17
+#define patchlevel 18
/*
* Patchlevel file for Ian Darwin's MAGIC command.
- * $Id: patchlevel.h,v 1.17 1995/04/28 17:29:13 christos Exp $
+ * $Id: patchlevel.h,v 1.18 1995/05/20 22:09:21 christos Exp $
*
* $Log: patchlevel.h,v $
- * Revision 1.17 1995/04/28 17:29:13 christos
+ * 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]
#ifndef lint
static char *moduleid =
- "@(#)$Id: print.c,v 1.21 1994/05/03 17:58:23 christos Exp $";
+ "@(#)$Id: print.c,v 1.22 1995/05/20 22:09:21 christos Exp $";
#endif /* lint */
#define SZOF(a) (sizeof(a) / sizeof(a[0]))
m->offset);
if (m->flag & INDIR)
- (void) fprintf(stderr, "(%s,%d),",
+ (void) fprintf(stderr, "(%s,%ld),",
(m->in.type >= 0 && m->in.type < SZOF(typ)) ?
typ[(unsigned char) m->in.type] :
"*bad*",
typ[(unsigned char) m->type] :
"*bad*");
if (m->mask != ~0L)
- (void) fprintf(stderr, " & %.8x", m->mask);
+ (void) fprintf(stderr, " & %.8lx", m->mask);
(void) fprintf(stderr, ",%c", m->reln);
case LELONG:
case BESHORT:
case BELONG:
- (void) fprintf(stderr, "%d", m->value.l);
+ (void) fprintf(stderr, "%ld", m->value.l);
break;
case STRING:
showstr(stderr, m->value.s, -1);
#ifndef lint
static char *moduleid =
- "@(#)$Id: softmagic.c,v 1.29 1995/04/28 19:13:08 christos Exp $";
+ "@(#)$Id: softmagic.c,v 1.30 1995/05/20 22:09:21 christos Exp $";
#endif /* lint */
static int match __P((unsigned char *, int));
char *str;
int len;
{
- (void) fprintf(stderr, "mget @%d: ", offset);
+ (void) fprintf(stderr, "mget @%ld: ", offset);
showstr(stderr, (char *) str, len);
(void) fputc('\n', stderr);
(void) fputc('\n', stderr);
int nbytes;
{
long offset = m->offset;
- long diff = nbytes - (offset + sizeof(union VALUETYPE));
- if (diff >= 0)
+
+ if (offset + sizeof(union VALUETYPE) <= nbytes)
memcpy(p, s + offset, sizeof(union VALUETYPE));
else {
- /* Not enough space; zeropad */
- long have = sizeof(union VALUETYPE) + diff;
+ /*
+ * the usefulness of padding with zeroes eludes me, it
+ * might even cause problems
+ */
+ long have = nbytes - offset;
+ memset(p, 0, sizeof(union VALUETYPE));
if (have > 0)
memcpy(p, s + offset, have);
- else
- have = 0;
-
- memset(p + have, 0, sizeof(union VALUETYPE) - have);
}