#endif
#ifndef lint
-FILE_RCSID("@(#)$Id: apprentice.c,v 1.67 2003/10/08 17:18:28 christos Exp $")
+FILE_RCSID("@(#)$Id: apprentice.c,v 1.68 2003/10/14 19:17:17 christos Exp $")
#endif /* lint */
#define EATAB {while (isascii((unsigned char) *l) && \
int mapped;
if (magicsize != FILE_MAGICSIZE) {
- file_error(ms, "Magic element size %lu != %lu",
+ file_error(ms, 0, "Magic element size %lu != %lu",
(unsigned long)sizeof(*magic),
(unsigned long)FILE_MAGICSIZE);
return -1;
free(mfn);
free(mlist);
mlist = NULL;
- file_error(ms, "Couldn't find any magic files!");
+ file_error(ms, 0, "Couldn't find any magic files!");
return NULL;
}
return mlist;
f = fopen(fn, "r");
if (f == NULL) {
if (errno != ENOENT)
- file_error(ms, "Can't read magic file %s (%s)",
- fn, strerror(errno));
+ file_error(ms, errno, "Can't read magic file %s", fn);
return -1;
}
if (isspace((unsigned char) c))
break;
if (p >= pmax) {
- file_error(ms, "String too long: `%s'", origs);
+ file_error(ms, 0, "String too long: `%s'", origs);
return NULL;
}
if(c == '\\') {
return -1;
if (fstat(fd, &st) == -1) {
- file_error(ms, "Cannot stat `%s' (%s)", dbname,
- strerror(errno));
+ 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, "Cannot map `%s' (%s)", dbname, strerror(errno));
+ file_error(ms, errno, "Cannot map `%s'", dbname);
goto error;
}
#define RET 2
goto error;
}
if (read(fd, mm, (size_t)st.st_size) != (size_t)st.st_size) {
- file_error(ms, "Read failed (%s)", strerror(errno));
+ file_error(ms, errno, "Read failed");
goto error;
}
#define RET 1
ptr = (uint32_t *)(void *)*magicp;
if (*ptr != MAGICNO) {
if (swap4(*ptr) != MAGICNO) {
- file_error(ms, "Bad magic in `%s'", dbname);
+ file_error(ms, 0, "Bad magic in `%s'");
goto error;
}
needsbyteswap = 1;
else
version = ptr[1];
if (version != VERSIONNO) {
- file_error(ms, "version mismatch (%d != %d) in `%s'",
+ file_error(ms, 0, "version mismatch (%d != %d) in `%s'",
version, VERSIONNO, dbname);
goto error;
}
return -1;
if ((fd = open(dbname, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1) {
- file_error(ms, "Cannot open `%s' (%s)", dbname, strerror(errno));
+ file_error(ms, errno, "Cannot open `%s'", dbname);
return -1;
}
if (write(fd, ar, sizeof(ar)) != (ssize_t)sizeof(ar)) {
- file_error(ms, "Error writing `%s' (%s)", dbname,
- strerror(errno));
+ 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, "Error seeking `%s' (%s)", dbname,
- strerror(errno));
+ 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, "Error writing `%s' (%s)", dbname,
- strerror(errno));
+ file_error(ms, errno, "Error writing `%s'", dbname);
return -1;
}
#include "file.h"
#ifndef lint
-FILE_RCSID("@(#)$Id: apptype.c,v 1.3 2003/06/10 18:28:37 christos Exp $")
+FILE_RCSID("@(#)$Id: apptype.c,v 1.4 2003/10/14 19:17:17 christos Exp $")
#endif /* lint */
#ifdef __EMX__
size_t nb)
{
APPTYPE rc, type;
- char path[_MAX_PATH], drive[_MAX_DRIVE], dir[_MAX_DIR], fname[_MAX_FNAME],
- ext[_MAX_EXT];
+ char path[_MAX_PATH], drive[_MAX_DRIVE], dir[_MAX_DIR],
+ fname[_MAX_FNAME], ext[_MAX_EXT];
char *filename;
FILE *fp;
if (fn)
filename = strdup(fn);
else if ((filename = tempnam("./", "tmp")) == NULL) {
- error("can't create tempnam (%s).\n", strerror(errno));
+ file_error(ms, errno, "Can't create tempnam");
+ return -1;
}
/* qualify the filename to prevent extraneous searches */
_splitpath(filename, drive, dir, fname, ext);
- sprintf(path, "%s%s%s%s", drive,
+ (void)sprintf(path, "%s%s%s%s", drive,
(*dir == '\0') ? "./" : dir,
fname,
(*ext == '\0') ? "." : ext);
if (fn == NULL) {
if ((fp = fopen(path, "wb")) == NULL) {
- file_error("Can't open tmp file `%s' (%s)", path,
- strerror(errno));
+ file_error(ms, errno, "Can't open tmp file `%s'", path);
return -1;
}
if (fwrite(buf, 1, nb, fp) != nb) {
- file_error("Can't write tmp file `%s' (%s)", path,
- strerror(errno));
+ file_error(ms, errno, "Can't write tmp file `%s'", path);
return -1;
}
(void)fclose(fp);
#endif
#ifndef lint
-FILE_RCSID("@(#)$Id: compress.c,v 1.32 2003/05/23 21:31:58 christos Exp $")
+FILE_RCSID("@(#)$Id: compress.c,v 1.33 2003/10/14 19:17:17 christos Exp $")
#endif
errno = r;
#endif
if (tfd == -1) {
- file_error(ms, "Can't create temporary file for pipe copy (%s)",
- strerror(errno));
+ file_error(ms, errno,
+ "Can't create temporary file for pipe copy");
return -1;
}
switch (r) {
case -1:
- file_error(ms, "Error copying from pipe to temp file (%s)",
- strerror(errno));
+ file_error(ms, errno, "Error copying from pipe to temp file");
return -1;
case 0:
break;
default:
- file_error(ms, "Error while writing to temp file (%s)",
- strerror(errno));
+ file_error(ms, errno, "Error while writing to temp file");
return -1;
}
* can still access the phantom inode.
*/
if ((fd = dup2(tfd, fd)) == -1) {
- file_error(ms, "Couldn't dup destcriptor for temp file (%s)",
- strerror(errno));
+ file_error(ms, errno, "Couldn't dup destcriptor for temp file");
return -1;
}
(void)close(tfd);
rc = inflateInit2(&z, -15);
if (rc != Z_OK) {
- file_error(ms, "zlib: %s", z.msg);
+ file_error(ms, 0, "zlib: %s", z.msg);
return 0;
}
rc = inflate(&z, Z_SYNC_FLUSH);
if (rc != Z_OK && rc != Z_STREAM_END) {
- file_error(ms, "zlib: %s", z.msg);
+ file_error(ms, 0, "zlib: %s", z.msg);
return 0;
}
#endif
if (pipe(fdin) == -1 || pipe(fdout) == -1) {
- file_error(ms, "Cannot create pipe (%s)", strerror(errno));
+ file_error(ms, errno, "Cannot create pipe");
return 0;
}
switch (fork()) {
exit(1);
/*NOTREACHED*/
case -1:
- file_error(ms, "Could not fork (%s)", strerror(errno));
+ file_error(ms, errno, "Could not fork");
return 0;
default: /* parent */
*/
/*
* file.h - definitions for file(1) program
- * @(#)$Id: file.h,v 1.58 2003/10/09 15:15:23 christos Exp $
+ * @(#)$Id: file.h,v 1.59 2003/10/14 19:17:17 christos Exp $
*/
#ifndef __file_h__
char *pbuf;
size_t psize;
} o;
+ int error;
int flags;
int haderr;
};
protected void file_badread(struct magic_set *);
protected void file_badseek(struct magic_set *);
protected void file_oomem(struct magic_set *);
-protected void file_error(struct magic_set *, const char *, ...);
+protected void file_error(struct magic_set *, int, const char *, ...);
protected void file_magwarn(const char *, ...);
protected void file_mdump(struct magic *);
protected void file_showstr(FILE *, const char *, size_t);
#undef HAVE_MAJOR
#ifndef lint
-FILE_RCSID("@(#)$Id: fsmagic.c,v 1.41 2003/05/23 21:31:58 christos Exp $")
+FILE_RCSID("@(#)$Id: fsmagic.c,v 1.42 2003/10/14 19:17:17 christos Exp $")
#endif /* lint */
protected int
file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb)
{
int ret = 0;
+#ifdef S_IFLNK
+ char buf[BUFSIZ+4];
+ int nch;
+ struct stat tstatbuf;
+#endif
if (fn == NULL)
return 0;
ret = stat(fn, sb); /* don't merge into if; see "ret =" above */
if (ret) {
+ if (ms->flags & MAGIC_ERROR) {
+ file_error(ms, errno, "Can't stat `%s'", fn);
+ return -1;
+ }
if (file_printf(ms, "Can't stat `%s' (%s)",
fn, strerror(errno)) == -1)
return -1;
#endif
#ifdef S_IFLNK
case S_IFLNK:
- {
- char buf[BUFSIZ+4];
- int nch;
- struct stat tstatbuf;
-
- if ((nch = readlink(fn, buf, BUFSIZ-1)) <= 0) {
- if (file_printf(ms,
- "unreadable symlink `%s' (%s)",
- strerror(errno)) == -1)
- return -1;
- return 1;
+ if ((nch = readlink(fn, buf, BUFSIZ-1)) <= 0) {
+ if (ms->flags & MAGIC_ERROR) {
+ file_error(ms, errno, "unreadable symlink `%s'",
+ fn);
+ return -1;
}
- buf[nch] = '\0'; /* readlink(2) forgets this */
+ if (file_printf(ms,
+ "unreadable symlink `%s' (%s)", fn,
+ strerror(errno)) == -1)
+ return -1;
+ return 1;
+ }
+ buf[nch] = '\0'; /* readlink(2) forgets this */
- /* If broken symlink, say so and quit early. */
- if (*buf == '/') {
- if (stat(buf, &tstatbuf) < 0) {
- if (file_printf(ms,
- "broken symbolic link to `%s'", buf) == -1)
- return -1;
- return 1;
- }
- }
- else {
- char *tmp;
- char buf2[BUFSIZ+BUFSIZ+4];
+ /* If broken symlink, say so and quit early. */
+ if (*buf == '/') {
+ if (stat(buf, &tstatbuf) < 0) {
+ if (ms->flags & MAGIC_ERROR) {
+ file_error(ms, errno,
+ "broken symbolic link to `%s'", buf);
+ return -1;
+ }
+ if (file_printf(ms, "broken symbolic link to `%s'",
+ buf) == -1)
+ return -1;
+ return 1;
+ }
+ }
+ else {
+ char *tmp;
+ char buf2[BUFSIZ+BUFSIZ+4];
- if ((tmp = strrchr(fn, '/')) == NULL) {
+ if ((tmp = strrchr(fn, '/')) == NULL) {
tmp = buf; /* in current directory anyway */
- }
- else {
+ } else {
if (tmp - fn + 1 > BUFSIZ) {
- file_printf(ms, "path too long: `%s'", fn);
- return -1;
+ if (ms->flags & MAGIC_ERROR) {
+ file_error(ms, 0,
+ "path too long: `%s'", buf);
+ return -1;
+ }
+ if (file_printf(ms,
+ "path too long: `%s'", fn) == -1)
+ return -1;
+ return 1;
}
- strcpy(buf2, fn); /* take directory part */
- buf2[tmp-fn+1] = '\0';
- strcat(buf2, buf); /* plus (relative) symlink */
+ (void)strcpy(buf2, fn); /* take dir part */
+ buf2[tmp - fn + 1] = '\0';
+ (void)strcat(buf2, buf); /* plus (rel) link */
tmp = buf2;
- }
- if (stat(tmp, &tstatbuf) < 0) {
+ }
+ if (stat(tmp, &tstatbuf) < 0) {
+ if (ms->flags & MAGIC_ERROR) {
+ file_error(ms, errno,
+ "broken symbolic link to `%s'",
+ buf);
+ return -1;
+ }
if (file_printf(ms,
- "broken symbolic link to `%s'",
- buf) == -1)
+ "broken symbolic link to `%s'", buf) == -1)
return -1;
return 1;
- }
- }
-
- /* Otherwise, handle it. */
- if ((ms->flags & MAGIC_SYMLINK) != 0) {
- const char *p;
- ms->flags &= MAGIC_SYMLINK;
- p = magic_file(ms, buf);
- ms->flags |= MAGIC_SYMLINK;
- return p != NULL ? 1 : -1;
- } else { /* just print what it points to */
- if (file_printf(ms, "symbolic link to `%s'",
- buf) == -1)
- return -1;
}
}
- return 1;
+
+ /* Otherwise, handle it. */
+ if ((ms->flags & MAGIC_SYMLINK) != 0) {
+ const char *p;
+ ms->flags &= MAGIC_SYMLINK;
+ p = magic_file(ms, buf);
+ ms->flags |= MAGIC_SYMLINK;
+ return p != NULL ? 1 : -1;
+ } else { /* just print what it points to */
+ if (file_printf(ms, "symbolic link to `%s'",
+ buf) == -1)
+ return -1;
+ }
+ return 1;
#endif
#ifdef S_IFSOCK
#ifndef __COHERENT__
case S_IFREG:
break;
default:
- file_error(ms, "invalid mode 0%o", sb->st_mode);
+ file_error(ms, 0, "invalid mode 0%o", sb->st_mode);
return -1;
/*NOTREACHED*/
}
*/
/*VARARGS*/
protected void
-file_error(struct magic_set *ms, const char *f, ...)
+file_error(struct magic_set *ms, int error, const char *f, ...)
{
va_list va;
/* Only the first error is ok */
va_start(va, f);
(void) vsnprintf(ms->o.buf, ms->o.size, f, va);
ms->haderr++;
+ ms->error = error;
va_end(va);
}
protected void
file_oomem(struct magic_set *ms)
{
- file_error(ms, "%s", strerror(errno));
+ file_error(ms, errno, "Cannot allocate memory");
}
protected void
file_badseek(struct magic_set *ms)
{
- file_error(ms, "Error seeking (%s)", strerror(errno));
+ file_error(ms, errno, "Error seeking");
}
protected void
file_badread(struct magic_set *ms)
{
- file_error(ms, "Error reading (%s)", strerror(errno));
+ file_error(ms, errno, "Error reading");
}
protected int
file_reset(struct magic_set *ms)
{
if (ms->mlist == NULL) {
- file_error(ms, "No magic files loaded");
+ file_error(ms, 0, "No magic files loaded");
return -1;
}
ms->o.ptr = ms->o.buf;
ms->haderr = 0;
+ ms->error = -1;
return 0;
}
#include "patchlevel.h"
#ifndef lint
-FILE_RCSID("@(#)$Id: magic.c,v 1.12 2003/10/09 15:15:23 christos Exp $")
+FILE_RCSID("@(#)$Id: magic.c,v 1.13 2003/10/14 19:17:17 christos Exp $")
#endif /* lint */
#ifdef __EMX__
return NULL;
}
ms->haderr = 0;
+ ms->error = -1;
ms->mlist = NULL;
return ms;
}
* try looking at the first HOWMANY bytes
*/
if ((nbytes = read(fd, (char *)buf, HOWMANY)) == -1) {
- file_error(ms, "Cannot read `%s' %s", inname, strerror(errno));
+ file_error(ms, errno, "Cannot read `%s'", inname);
goto done;
}
#define MAGIC_CHECK 0x040 /* Print warnings to stderr */
#define MAGIC_PRESERVE_ATIME 0x080 /* Restore access time on exit */
#define MAGIC_RAW 0x100 /* Don't translate unprintable chars */
+#define MAGIC_ERROR 0x200 /* Handle ENOENT etc as real errors */
#ifdef __cplusplus
extern "C" {
#include "readelf.h"
#ifndef lint
-FILE_RCSID("@(#)$Id: readelf.c,v 1.33 2003/09/16 15:38:46 christos Exp $")
+FILE_RCSID("@(#)$Id: readelf.c,v 1.34 2003/10/14 19:17:17 christos Exp $")
#endif
#ifdef ELFCORE
ssize_t bufsize;
if (size != ph_size) {
- file_error(ms, "Corrupted program header size");
+ file_error(ms, 0, "Corrupted program header size");
return -1;
}
/*
Elf64_Shdr sh64;
if (size != sh_size) {
- file_error(ms, "Corrupted section header size");
+ file_error(ms, 0, "Corrupted section header size");
return -1;
}
off_t savedoffset;
if (size != ph_size) {
- file_error(ms, "Corrupted program header size");
+ file_error(ms, 0, "Corrupted program header size");
return -1;
}
if (lseek(fd, off, SEEK_SET) == (off_t)-1) {
#ifndef lint
-FILE_RCSID("@(#)$Id: softmagic.c,v 1.60 2003/06/10 18:28:37 christos Exp $")
+FILE_RCSID("@(#)$Id: softmagic.c,v 1.61 2003/10/14 19:17:17 christos Exp $")
#endif /* lint */
private int match(struct magic_set *, struct magic *, uint32_t,
break;
default:
- file_error(ms, "invalid m->type (%d) in mprint()", m->type);
+ file_error(ms, 0, "invalid m->type (%d) in mprint()", m->type);
return -1;
}
return(t);
case FILE_REGEX:
return 1;
default:
- file_error(ms, "invalid type %d in mconvert()", m->type);
+ file_error(ms, 0, "invalid type %d in mconvert()", m->type);
return 0;
}
}
if (rc) {
free(p->buf);
regerror(rc, &rx, errmsg, sizeof(errmsg));
- file_error(ms, "regex error %d, (%s)", rc, errmsg);
+ file_error(ms, 0, "regex error %d, (%s)", rc, errmsg);
return -1;
} else {
rc = regexec(&rx, p->buf, 0, 0, 0);
}
}
default:
- file_error(ms, "invalid type %d in mcheck()", m->type);
+ file_error(ms, 0, "invalid type %d in mcheck()", m->type);
return -1;
}
default:
matched = 0;
- file_error(ms, "can't happen: invalid relation `%c'", m->reln);
+ file_error(ms, 0, "can't happen: invalid relation `%c'",
+ m->reln);
return -1;
}