From: Christos Zoulas Date: Tue, 14 Oct 2003 19:29:55 +0000 (+0000) Subject: Fix strings, add magic_errno(). X-Git-Tag: FILE5_05~931 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=17026ac69ead2d3425388081dd9622cab440fa5f;p=file Fix strings, add magic_errno(). --- diff --git a/src/apprentice.c b/src/apprentice.c index 6ec89813..f2ed669c 100644 --- a/src/apprentice.c +++ b/src/apprentice.c @@ -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; } diff --git a/src/apptype.c b/src/apptype.c index 35f4fbb3..fdd17ea8 100644 --- a/src/apptype.c +++ b/src/apptype.c @@ -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); diff --git a/src/compress.c b/src/compress.c index 4393b0d8..8c88c4fe 100644 --- a/src/compress.c +++ b/src/compress.c @@ -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 */ diff --git a/src/fsmagic.c b/src/fsmagic.c index 731b3b9d..18bdd60e 100644 --- a/src/fsmagic.c +++ b/src/fsmagic.c @@ -42,7 +42,7 @@ #endif #include #include -/* 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 # 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; diff --git a/src/funcs.c b/src/funcs.c index 54f4c1f7..12aff1d4 100644 --- a/src/funcs.c +++ b/src/funcs.c @@ -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; diff --git a/src/is_tar.c b/src/is_tar.c index 2d673368..8d023529 100644 --- a/src/is_tar.c +++ b/src/is_tar.c @@ -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++; diff --git a/src/magic.c b/src/magic.c index ddd9b0aa..3fe8273a 100644 --- a/src/magic.c +++ b/src/magic.c @@ -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) { diff --git a/src/magic.h b/src/magic.h index 5b37b3b4..eefb8bf5 100644 --- a/src/magic.h +++ b/src/magic.h @@ -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 }; diff --git a/src/names.h b/src/names.h index 75c4d7c4..79013db7 100644 --- a/src/names.h +++ b/src/names.h @@ -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} }; diff --git a/src/readelf.c b/src/readelf.c index 3e69ce21..a4d2ef60 100644 --- a/src/readelf.c +++ b/src/readelf.c @@ -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); diff --git a/src/softmagic.c b/src/softmagic.c index 6631ad87..e70b5243 100644 --- a/src/softmagic.c +++ b/src/softmagic.c @@ -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; } diff --git a/src/tar.h b/src/tar.h index eeca6db6..983ab561 100644 --- a/src/tar.h +++ b/src/tar.h @@ -37,11 +37,11 @@ * * 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.