]> granicus.if.org Git - file/commitdiff
Passed incorrect argument to eatsize().
authorChristos Zoulas <christos@zoulas.com>
Sat, 20 May 1995 22:09:21 +0000 (22:09 +0000)
committerChristos Zoulas <christos@zoulas.com>
Sat, 20 May 1995 22:09:21 +0000 (22:09 +0000)
Use %ld and %lx where appropriate.
Remove unused variables
ELF support for both big and little endian
Fixes for small files again.

src/apprentice.c
src/ascmagic.c
src/compress.c
src/file.c
src/file.h
src/patchlevel.h
src/print.c
src/softmagic.c

index 76e90d46ac43a35c2604c1872c935636957e6e71..08b5055e260725291d1a4003e1a951d07b2879cc 100644 (file)
@@ -33,7 +33,7 @@
 
 #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) && \
@@ -151,8 +151,9 @@ int *ndx, check;
        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) {
@@ -162,6 +163,7 @@ int *ndx, check;
                else
                        exit(1);
            }
+           memset(&magic[*ndx], 0, sizeof(struct magic) * ALLOC_INCR);
        }
        m = &magic[*ndx];
        m->flag = 0;
@@ -370,7 +372,7 @@ char **p;
        } else
                if (m->reln != 'x') {
                        m->value.l = signextend(m, strtoul(*p, p, 0));
-                       eatsize(&p);
+                       eatsize(p);
                }
        return 0;
 }
index 9f41a76a1d97a4ebd057d5591c0b1e533cc4082d..600d468b7da706cf32d2f66a20ef812e9d5820bc 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * 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.
@@ -36,7 +36,7 @@
 
 #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() */
@@ -47,7 +47,7 @@ ascmagic(buf, nbytes)
 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;
@@ -109,11 +109,11 @@ int nbytes;       /* size actually read */
 
        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);
        }
index a665306ac9510ecf5441160b368d870a944b3b50..096abb61e4b21291bf08580996d4e52161e66767 100644 (file)
@@ -4,7 +4,7 @@
  *                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>
@@ -20,13 +20,12 @@ static struct {
    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]);
@@ -121,5 +120,3 @@ int n;
                return n;
        }
 }
-
-
index 409959cdf555374a7dc3048a0f17462d5e70d1f1..8e480f974f07ae1202d0ec47cdf8b11ce4b630dc 100644 (file)
@@ -26,7 +26,7 @@
  */
 #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>
@@ -43,7 +43,7 @@ static char *moduleid =
 #endif
 #include <unistd.h>    /* for read() */
 
-#ifdef HAVE_ELF
+#ifdef __ELF__
 #include <elf.h>
 #endif
 
@@ -216,10 +216,11 @@ int wid;
        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*/
                }
@@ -238,7 +239,7 @@ int wid;
                    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);
@@ -258,14 +259,13 @@ int wid;
                /*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.
@@ -275,30 +275,41 @@ int wid;
         *      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
 
@@ -315,25 +326,24 @@ int wid;
 }
 
 
-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';
 }
index 772bd8b44195c970cb5fb1439253a817ba5b2273..c0bbf0e00b81009389d427a5cf34011be96c3933 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * 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.
@@ -94,7 +94,7 @@ extern void  mdump            __P((struct magic *));
 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));
index 4845454038db0430792e0471a16c90cd4f62e544..2d22adf2d10fd70758fcf9a52098cda5df9c1f05 100644 (file)
@@ -1,12 +1,19 @@
 #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]
index a91f4297e61596f6ce2415a4139a464c96ec2615..17e1136a05596b2863f5199963dd03f89b293a30 100644 (file)
@@ -40,7 +40,7 @@
 
 #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]))
@@ -58,7 +58,7 @@ struct magic *m;
                       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*",
@@ -69,7 +69,7 @@ struct magic *m;
                                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);
 
@@ -82,7 +82,7 @@ struct magic *m;
            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);
index 92d4d773a0938541967c9694d9e82306dd3c2a5e..76fd6d81154132b9ea62e29c77322b7038a64002 100644 (file)
@@ -34,7 +34,7 @@
 
 #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));
@@ -273,7 +273,7 @@ long offset;
 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);
@@ -287,18 +287,18 @@ struct magic *m;
 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);
        }