]> granicus.if.org Git - file/commitdiff
Fix strings, add magic_errno().
authorChristos Zoulas <christos@zoulas.com>
Tue, 14 Oct 2003 19:29:55 +0000 (19:29 +0000)
committerChristos Zoulas <christos@zoulas.com>
Tue, 14 Oct 2003 19:29:55 +0000 (19:29 +0000)
12 files changed:
src/apprentice.c
src/apptype.c
src/compress.c
src/fsmagic.c
src/funcs.c
src/is_tar.c
src/magic.c
src/magic.h
src/names.h
src/readelf.c
src/softmagic.c
src/tar.h

index 6ec898135c85fd74bba36594fdfe650f7b176ef1..f2ed669c774fade44a8093d0d54807d6270a3120 100644 (file)
@@ -50,7 +50,7 @@
 #endif
 
 #ifndef        lint
-FILE_RCSID("@(#)$Id: apprentice.c,v 1.68 2003/10/14 19:17:17 christos Exp $")
+FILE_RCSID("@(#)$Id: apprentice.c,v 1.69 2003/10/14 19:29:55 christos Exp $")
 #endif /* lint */
 
 #define        EATAB {while (isascii((unsigned char) *l) && \
@@ -138,7 +138,7 @@ apprentice_1(struct magic_set *ms, const char *fn, int action,
        int mapped;
 
        if (magicsize != FILE_MAGICSIZE) {
-               file_error(ms, 0, "Magic element size %lu != %lu",
+               file_error(ms, 0, "magic element size %lu != %lu",
                    (unsigned long)sizeof(*magic),
                    (unsigned long)FILE_MAGICSIZE);
                return -1;
@@ -155,7 +155,7 @@ apprentice_1(struct magic_set *ms, const char *fn, int action,
 #ifndef COMPILE_ONLY
        if ((rv = apprentice_map(ms, &magic, &nmagic, fn)) == -1) {
                if (ms->flags & MAGIC_CHECK)
-                       file_magwarn("Using regular magic file `%s'", fn);
+                       file_magwarn("using regular magic file `%s'", fn);
                rv = apprentice_file(ms, &magic, &nmagic, fn, action);
                if (rv != 0)
                        return -1;
@@ -266,7 +266,7 @@ file_apprentice(struct magic_set *ms, const char *fn, int action)
                free(mfn);
                free(mlist);
                mlist = NULL;
-               file_error(ms, 0, "Couldn't find any magic files!");
+               file_error(ms, 0, "could not find any magic files!");
                return NULL;
        }
        return mlist;
@@ -290,7 +290,8 @@ apprentice_file(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp,
        f = fopen(fn, "r");
        if (f == NULL) {
                if (errno != ENOENT)
-                       file_error(ms, errno, "Can't read magic file %s", fn);
+                       file_error(ms, errno, "cannot read magic file `%s'",
+                           fn);
                return -1;
        }
 
@@ -365,7 +366,7 @@ file_signextend(struct magic_set *ms, struct magic *m, uint32_t v)
                        break;
                default:
                        if (ms->flags & MAGIC_CHECK)
-                           file_magwarn("can't happen: m->type=%d\n",
+                           file_magwarn("cannot happen: m->type=%d\n",
                                    m->type);
                        return ~0U;
                }
@@ -724,7 +725,7 @@ getvalue(struct magic_set *ms, struct magic *m, char **p)
                *p = getstr(ms, *p, m->value.s, sizeof(m->value.s), &slen);
                if (*p == NULL) {
                        if (ms->flags & MAGIC_CHECK)
-                               file_magwarn("Cannot get string from `%s'",
+                               file_magwarn("cannot get string from `%s'",
                                    m->value.s);
                        return -1;
                }
@@ -758,7 +759,7 @@ getstr(struct magic_set *ms, char *s, char *p, int plen, int *slen)
                if (isspace((unsigned char) c))
                        break;
                if (p >= pmax) {
-                       file_error(ms, 0, "String too long: `%s'", origs);
+                       file_error(ms, 0, "string too long: `%s'", origs);
                        return NULL;
                }
                if(c == '\\') {
@@ -966,14 +967,14 @@ apprentice_map(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp,
                return -1;
 
        if (fstat(fd, &st) == -1) {
-               file_error(ms, errno, "Cannot stat `%s'", dbname);
+               file_error(ms, errno, "cannot stat `%s'", dbname);
                goto error;
        }
 
 #ifdef QUICK
        if ((mm = mmap(0, (size_t)st.st_size, PROT_READ|PROT_WRITE,
            MAP_PRIVATE|MAP_FILE, fd, (off_t)0)) == MAP_FAILED) {
-               file_error(ms, errno, "Cannot map `%s'", dbname);
+               file_error(ms, errno, "cannot map `%s'", dbname);
                goto error;
        }
 #define RET    2
@@ -983,7 +984,7 @@ apprentice_map(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp,
                goto error;
        }
        if (read(fd, mm, (size_t)st.st_size) != (size_t)st.st_size) {
-               file_error(ms, errno, "Read failed");
+               file_badread(ms);
                goto error;
        }
 #define RET    1
@@ -994,7 +995,7 @@ apprentice_map(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp,
        ptr = (uint32_t *)(void *)*magicp;
        if (*ptr != MAGICNO) {
                if (swap4(*ptr) != MAGICNO) {
-                       file_error(ms, 0, "Bad magic in `%s'");
+                       file_error(ms, 0, "bad magic in `%s'");
                        goto error;
                }
                needsbyteswap = 1;
@@ -1049,24 +1050,24 @@ apprentice_compile(struct magic_set *ms, struct magic **magicp,
                return -1;
 
        if ((fd = open(dbname, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1) {
-               file_error(ms, errno, "Cannot open `%s'", dbname);
+               file_error(ms, errno, "cannot open `%s'", dbname);
                return -1;
        }
 
        if (write(fd, ar, sizeof(ar)) != (ssize_t)sizeof(ar)) {
-               file_error(ms, errno, "Error writing `%s'", dbname);
+               file_error(ms, errno, "error writing `%s'", dbname);
                return -1;
        }
 
        if (lseek(fd, (off_t)sizeof(struct magic), SEEK_SET)
            != sizeof(struct magic)) {
-               file_error(ms, errno, "Error seeking `%s'", dbname);
+               file_error(ms, errno, "error seeking `%s'", dbname);
                return -1;
        }
 
        if (write(fd, *magicp, (sizeof(struct magic) * *nmagicp)) 
            != (ssize_t)(sizeof(struct magic) * *nmagicp)) {
-               file_error(ms, errno, "Error writing `%s'", dbname);
+               file_error(ms, errno, "error writing `%s'", dbname);
                return -1;
        }
 
index 35f4fbb3c40f65bb11e9b66ef8af568bfe39a09c..fdd17ea813453e977d291e18ab7aafe8f908bc29 100644 (file)
@@ -31,7 +31,7 @@
 #include "file.h"
 
 #ifndef        lint
-FILE_RCSID("@(#)$Id: apptype.c,v 1.4 2003/10/14 19:17:17 christos Exp $")
+FILE_RCSID("@(#)$Id: apptype.c,v 1.5 2003/10/14 19:29:55 christos Exp $")
 #endif /* lint */
 
 #ifdef __EMX__
@@ -55,7 +55,7 @@ file_os2_apptype(struct magic_set *ms, const char *fn, const void *buf,
        if (fn)
                filename = strdup(fn);
        else if ((filename = tempnam("./", "tmp")) == NULL) {
-               file_error(ms, errno, "Can't create tempnam");
+               file_error(ms, errno, "cannot create tempnam");
                return -1;
        }
        /* qualify the filename to prevent extraneous searches */
@@ -67,11 +67,12 @@ file_os2_apptype(struct magic_set *ms, const char *fn, const void *buf,
 
        if (fn == NULL) {
                if ((fp = fopen(path, "wb")) == NULL) {
-                       file_error(ms, errno, "Can't open tmp file `%s'", path);
+                       file_error(ms, errno, "cannot open tmp file `%s'", path);
                        return -1;
                }
                if (fwrite(buf, 1, nb, fp) != nb) {
-                       file_error(ms, errno, "Can't write tmp file `%s'", path);
+                       file_error(ms, errno, "cannot write tmp file `%s'",
+                           path);
                        return -1;
                }
                (void)fclose(fp);
index 4393b0d8fcc4463bb1b7174d0b4eea976b395878..8c88c4fe0710609882c6a75365b1d1575e167e71 100644 (file)
@@ -56,7 +56,7 @@
 #endif
 
 #ifndef lint
-FILE_RCSID("@(#)$Id: compress.c,v 1.33 2003/10/14 19:17:17 christos Exp $")
+FILE_RCSID("@(#)$Id: compress.c,v 1.34 2003/10/14 19:29:55 christos Exp $")
 #endif
 
 
@@ -201,7 +201,7 @@ file_pipe2file(struct magic_set *ms, int fd, const void *startbuf,
 #endif
        if (tfd == -1) {
                file_error(ms, errno,
-                   "Can't create temporary file for pipe copy");
+                   "cannot create temporary file for pipe copy");
                return -1;
        }
 
@@ -215,12 +215,12 @@ file_pipe2file(struct magic_set *ms, int fd, const void *startbuf,
 
        switch (r) {
        case -1:
-               file_error(ms, errno, "Error copying from pipe to temp file");
+               file_error(ms, errno, "error copying from pipe to temp file");
                return -1;
        case 0:
                break;
        default:
-               file_error(ms, errno, "Error while writing to temp file");
+               file_error(ms, errno, "error while writing to temp file");
                return -1;
        }
 
@@ -230,7 +230,7 @@ file_pipe2file(struct magic_set *ms, int fd, const void *startbuf,
         * can still access the phantom inode.
         */
        if ((fd = dup2(tfd, fd)) == -1) {
-               file_error(ms, errno, "Couldn't dup destcriptor for temp file");
+               file_error(ms, errno, "could not dup descriptor for temp file");
                return -1;
        }
        (void)close(tfd);
@@ -329,7 +329,7 @@ uncompressbuf(struct magic_set *ms, size_t method, const unsigned char *old,
 #endif
 
        if (pipe(fdin) == -1 || pipe(fdout) == -1) {
-               file_error(ms, errno, "Cannot create pipe");    
+               file_error(ms, errno, "cannot create pipe");    
                return 0;
        }
        switch (fork()) {
@@ -351,7 +351,7 @@ uncompressbuf(struct magic_set *ms, size_t method, const unsigned char *old,
                exit(1);
                /*NOTREACHED*/
        case -1:
-               file_error(ms, errno, "Could not fork");
+               file_error(ms, errno, "could not fork");
                return 0;
 
        default: /* parent */
index 731b3b9ddb106c177bbf4119130eb037865ae8d4..18bdd60e3d9e4bb7468e3fe5bbbb8c9d4a7213c1 100644 (file)
@@ -42,7 +42,7 @@
 #endif
 #include <stdlib.h>
 #include <sys/stat.h>
-/* Since major is a function on SVR4, we can't use `ifndef major'.  */
+/* Since major is a function on SVR4, we cannot use `ifndef major'.  */
 #ifdef MAJOR_IN_MKDEV
 # include <sys/mkdev.h>
 # define HAVE_MAJOR
@@ -62,7 +62,7 @@
 #undef HAVE_MAJOR
 
 #ifndef        lint
-FILE_RCSID("@(#)$Id: fsmagic.c,v 1.42 2003/10/14 19:17:17 christos Exp $")
+FILE_RCSID("@(#)$Id: fsmagic.c,v 1.43 2003/10/14 19:29:55 christos Exp $")
 #endif /* lint */
 
 protected int
@@ -91,10 +91,10 @@ file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb)
 
        if (ret) {
                if (ms->flags & MAGIC_ERROR) {
-                       file_error(ms, errno, "Can't stat `%s'", fn);
+                       file_error(ms, errno, "cannot stat `%s'", fn);
                        return -1;
                }
-               if (file_printf(ms, "Can't stat `%s' (%s)",
+               if (file_printf(ms, "cannot open (%s)",
                    fn, strerror(errno)) == -1)
                        return -1;
                return 1;
index 54f4c1f73d00b8fdb64522b50975e9924c91f0dd..12aff1d4271ceb2ec17c511b9be2157facd662a3 100644 (file)
@@ -75,31 +75,36 @@ file_error(struct magic_set *ms, int error, const char *f, ...)
        va_list va;
        /* Only the first error is ok */
        if (ms->haderr)
-           return;
+               return;
        va_start(va, f);
-       (void) vsnprintf(ms->o.buf, ms->o.size, f, va);
+       (void)vsnprintf(ms->o.buf, ms->o.size, f, va);
+       va_end(va);
+       if (error > 0) {
+               size_t len = strlen(ms->o.buf);
+               (void)snprintf(ms->o.buf + len, ms->o.size - len, " (%s)",
+                   strerror(error));
+       }
        ms->haderr++;
        ms->error = error;
-       va_end(va);
 }
 
 
 protected void
 file_oomem(struct magic_set *ms)
 {
-       file_error(ms, errno, "Cannot allocate memory");
+       file_error(ms, errno, "cannot allocate memory");
 }
 
 protected void
 file_badseek(struct magic_set *ms)
 {
-       file_error(ms, errno, "Error seeking");
+       file_error(ms, errno, "error seeking");
 }
 
 protected void
 file_badread(struct magic_set *ms)
 {
-       file_error(ms, errno, "Error reading");
+       file_error(ms, errno, "error reading");
 }
 
 protected int
@@ -130,7 +135,7 @@ protected int
 file_reset(struct magic_set *ms)
 {
        if (ms->mlist == NULL) {
-               file_error(ms, 0, "No magic files loaded");
+               file_error(ms, 0, "no magic files loaded");
                return -1;
        }
        ms->o.ptr = ms->o.buf;
index 2d673368f8fb6651f53e40fd8260a7c359586128..8d023529156f7b7d685b450528660252977f088d 100644 (file)
@@ -50,7 +50,7 @@
 #include "tar.h"
 
 #ifndef lint
-FILE_RCSID("@(#)$Id: is_tar.c,v 1.22 2003/03/27 19:09:45 christos Exp $")
+FILE_RCSID("@(#)$Id: is_tar.c,v 1.23 2003/10/14 19:29:55 christos Exp $")
 #endif
 
 #define        isodigit(c)     ( ((c) >= '0') && ((c) <= '7') )
@@ -104,7 +104,7 @@ is_tar(const unsigned char *buf, size_t nbytes)
        p = header->charptr;
        for (i = sizeof(union record); --i >= 0;) {
                /*
-                * We can't use unsigned char here because of old compilers,
+                * We cannot use unsigned char here because of old compilers,
                 * e.g. V7.
                 */
                sum += 0xFF & *p++;
index ddd9b0aa675aec0bf87bff1a78caadfc37746bf6..3fe8273aaf9d3bcf8bc966d61b50cfdbb41b9732 100644 (file)
@@ -65,7 +65,7 @@
 #include "patchlevel.h"
 
 #ifndef        lint
-FILE_RCSID("@(#)$Id: magic.c,v 1.13 2003/10/14 19:17:17 christos Exp $")
+FILE_RCSID("@(#)$Id: magic.c,v 1.14 2003/10/14 19:29:55 christos Exp $")
 #endif /* lint */
 
 #ifdef __EMX__
@@ -233,7 +233,7 @@ magic_file(struct magic_set *ms, const char *inname)
        if (inname == NULL)
                fd = STDIN_FILENO;
        else if ((fd = open(inname, O_RDONLY)) < 0) {
-               /* We can't open it, but we were able to stat it. */
+               /* We cannot open it, but we were able to stat it. */
                if (sb.st_mode & 0002)
                        if (file_printf(ms, "writable, ") == -1)
                                return NULL;
@@ -247,7 +247,7 @@ magic_file(struct magic_set *ms, const char *inname)
         * try looking at the first HOWMANY bytes
         */
        if ((nbytes = read(fd, (char *)buf, HOWMANY)) == -1) {
-               file_error(ms, errno, "Cannot read `%s'", inname);
+               file_error(ms, errno, "cannot read `%s'", inname);
                goto done;
        }
 
@@ -278,7 +278,7 @@ magic_file(struct magic_set *ms, const char *inname)
                         * be an ELF file, and the file is at least 5 bytes
                         * long, so if it's an ELF file it has at least one
                         * byte past the ELF magic number - try extracting
-                        * information from the ELF headers that can't easily
+                        * information from the ELF headers that cannot easily
                         * be extracted with rules in the magic file.
                         */
                        file_tryelf(ms, fd, buf, (size_t)nbytes);
@@ -315,6 +315,12 @@ magic_error(struct magic_set *ms)
        return ms->haderr ? ms->o.buf : NULL;
 }
 
+public int
+magic_errno(struct magic_set *ms)
+{
+       return ms->haderr ? ms->error : 0;
+}
+
 public int
 magic_setflags(struct magic_set *ms, int flags)
 {
index 5b37b3b4467e5d2374220058df4f4c6c05468201..eefb8bf540ab726997305c7d23e885a8eaefb93d 100644 (file)
@@ -60,6 +60,7 @@ int magic_setflags(magic_t, int);
 int magic_load(magic_t, const char *);
 int magic_compile(magic_t, const char *);
 int magic_check(magic_t, const char *);
+int magic_errno(magic_t);
 
 #ifdef __cplusplus
 };
index 75c4d7c4012131ee3475ac2c172c163211a1ef29..79013db7886269be851f71a3814a5ba945bc9339 100644 (file)
@@ -37,7 +37,7 @@
  * appear at fixed offsets into the file. Don't make HOWMANY
  * too high unless you have a very fast CPU.
  *
- * $Id: names.h,v 1.22 2003/09/25 18:30:26 christos Exp $
+ * $Id: names.h,v 1.23 2003/10/14 19:29:55 christos Exp $
  */
 
 /*
@@ -79,7 +79,7 @@ static const struct {
        { "HTML document",                              "text/html", },
        { "BCPL program",                               "text/x-bcpl" },
        { "M4 macro language pre-processor",            "text/x-m4" },
-       { "can't happen error on names.h/types",        "error/x-error" },
+       { "cannot happen error on names.h/types",       "error/x-error" },
        { 0, 0}
 };
 
index 3e69ce21eff08c5e2997d137a2bcfa6b72353cdb..a4d2ef60de5e07adfa0c0899f3203e8429323b1a 100644 (file)
@@ -39,7 +39,7 @@
 #include "readelf.h"
 
 #ifndef lint
-FILE_RCSID("@(#)$Id: readelf.c,v 1.34 2003/10/14 19:17:17 christos Exp $")
+FILE_RCSID("@(#)$Id: readelf.c,v 1.35 2003/10/14 19:29:55 christos Exp $")
 #endif
 
 #ifdef ELFCORE
@@ -222,7 +222,7 @@ dophn_core(struct magic_set *ms, int class, int swap, int fd, off_t off,
        ssize_t bufsize;
 
        if (size != ph_size) {
-               file_error(ms, 0, "Corrupted program header size");
+               file_error(ms, 0, "corrupted program header size");
                return -1;
        }
        /*
@@ -543,7 +543,7 @@ doshn(struct magic_set *ms, int class, int swap, int fd, off_t off, int num,
        Elf64_Shdr sh64;
 
        if (size != sh_size) {
-               file_error(ms, 0, "Corrupted section header size");
+               file_error(ms, 0, "corrupted section header size");
                return -1;
        }
 
@@ -587,7 +587,7 @@ dophn_exec(struct magic_set *ms, int class, int swap, int fd, off_t off,
        off_t savedoffset;
 
        if (size != ph_size) {
-               file_error(ms, 0, "Corrupted program header size");
+               file_error(ms, 0, "corrupted program header size");
                return -1;
        }
        if (lseek(fd, off, SEEK_SET) == (off_t)-1) {
@@ -661,7 +661,7 @@ file_tryelf(struct magic_set *ms, int fd, const unsigned char *buf,
        int swap;
 
        /*
-        * If we can't seek, it must be a pipe, socket or fifo.
+        * If we cannot seek, it must be a pipe, socket or fifo.
         */
        if((lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) && (errno == ESPIPE))
                fd = file_pipe2file(ms, fd, buf, nbytes);
index 6631ad87c54cb48e45f0b1eeb52578e79129e4ca..e70b5243ed9235cca8a4e5671cffaecd4976a80b 100644 (file)
@@ -44,7 +44,7 @@
 
 
 #ifndef        lint
-FILE_RCSID("@(#)$Id: softmagic.c,v 1.61 2003/10/14 19:17:17 christos Exp $")
+FILE_RCSID("@(#)$Id: softmagic.c,v 1.62 2003/10/14 19:29:55 christos Exp $")
 #endif /* lint */
 
 private int match(struct magic_set *, struct magic *, uint32_t,
@@ -1157,7 +1157,7 @@ mcheck(struct magic_set *ms, union VALUETYPE *p, struct magic *m)
 
        default:
                matched = 0;
-               file_error(ms, 0, "can't happen: invalid relation `%c'",
+               file_error(ms, 0, "cannot happen: invalid relation `%c'",
                    m->reln);
                return -1;
        }
index eeca6db6a20b04770096a79a3d672b0beaf5ec97..983ab561b2874990f03261cd645e3c099b3d28d2 100644 (file)
--- a/src/tar.h
+++ b/src/tar.h
  *
  * Created 25 August 1985 by John Gilmore, ihnp4!hoptoad!gnu.
  *
- * $Id: tar.h,v 1.6 2003/03/23 21:16:26 christos Exp $ # checkin only
+ * $Id: tar.h,v 1.7 2003/10/14 19:29:56 christos Exp $ # checkin only
  */
 
 /*
- * Kludge for handling systems that can't cope with multiple
+ * Kludge for handling systems that cannot cope with multiple
  * external definitions of a variable.  In ONE routine (tar.c),
  * we #define TAR_EXTERN to null; here, we set it to "extern" if
  * it is not already set.