diff options
Diffstat (limited to 'file')
-rw-r--r-- | file/apprentice.c | 12 | ||||
-rw-r--r-- | file/ascmagic.c | 52 | ||||
-rw-r--r-- | file/compress.c | 32 | ||||
-rw-r--r-- | file/configure.ac | 3 | ||||
-rw-r--r-- | file/file.c | 2 | ||||
-rw-r--r-- | file/file.h | 36 | ||||
-rw-r--r-- | file/fsmagic.c | 62 | ||||
-rw-r--r-- | file/print.c | 26 | ||||
-rw-r--r-- | file/readelf.c | 60 | ||||
-rw-r--r-- | file/softmagic.c | 15 | ||||
-rw-r--r-- | file/system.h | 27 |
11 files changed, 184 insertions, 143 deletions
diff --git a/file/apprentice.c b/file/apprentice.c index dd2716c01..2d8f4a54a 100644 --- a/file/apprentice.c +++ b/file/apprentice.c @@ -902,7 +902,7 @@ apprentice_map(/*@unused@*/ const fmagic fm, if (fstat(fd, &st) == -1) { (void)fprintf(stderr, "%s: Cannot stat `%s' (%s)\n", progname, dbname, strerror(errno)); - goto error; + goto errxit; } #ifdef HAVE_MMAP @@ -910,14 +910,14 @@ apprentice_map(/*@unused@*/ const fmagic fm, MAP_PRIVATE|MAP_FILE, fd, (off_t)0)) == MAP_FAILED) { (void)fprintf(stderr, "%s: Cannot map `%s' (%s)\n", progname, dbname, strerror(errno)); - goto error; + goto errxit; } #else mm = xmalloc((size_t)st.st_size); if (read(fd, mm, (size_t)st.st_size) != (size_t)st.st_size) { (void) fprintf(stderr, "%s: Read failed (%s).\n", progname, strerror(errno)); - goto error; + goto errxit; } #endif *magicp = mm; @@ -928,7 +928,7 @@ apprentice_map(/*@unused@*/ const fmagic fm, if (swap4(*ptr) != MAGICNO) { (void)fprintf(stderr, "%s: Bad magic in `%s'\n", progname, dbname); - goto error; + goto errxit; } needsbyteswap = 1; } else @@ -941,7 +941,7 @@ apprentice_map(/*@unused@*/ const fmagic fm, (void)fprintf(stderr, "%s: version mismatch (%d != %d) in `%s'\n", progname, version, VERSIONNO, dbname); - goto error; + goto errxit; } *nmagicp = (st.st_size / sizeof(struct magic)) - 1; (*magicp)++; @@ -949,7 +949,7 @@ apprentice_map(/*@unused@*/ const fmagic fm, byteswap(*magicp, *nmagicp); return 0; -error: +errxit: if (fd != -1) (void)close(fd); /*@-branchstate@*/ diff --git a/file/ascmagic.c b/file/ascmagic.c index 48a8bea86..0ab448ccf 100644 --- a/file/ascmagic.c +++ b/file/ascmagic.c @@ -520,11 +520,12 @@ fmagicA(fmagic fm) */ switch (is_tar(fm)) { case 1: - ckfputs((fm->flags & FMAGIC_FLAGS_MIME) ? "application/x-tar" : "tar archive", stdout); + ckfputs(((fm->flags & FMAGIC_FLAGS_MIME) + ? "application/x-tar" : "tar archive"), fm); return 1; case 2: - ckfputs((fm->flags & FMAGIC_FLAGS_MIME) ? "application/x-tar, POSIX" - : "POSIX tar archive", stdout); + ckfputs(((fm->flags & FMAGIC_FLAGS_MIME) + ? "application/x-tar, POSIX" : "POSIX tar archive"), fm); return 1; } @@ -680,27 +681,26 @@ subtype_identified: if ((fm->flags & FMAGIC_FLAGS_MIME)) { if (subtype_mime != NULL) - ckfputs(subtype_mime, stdout); + ckfputs(subtype_mime, fm); else - ckfputs("text/plain", stdout); + ckfputs("text/plain", fm); if (code_mime != NULL) { - ckfputs("; charset=", stdout); - ckfputs(code_mime, stdout); + ckfputs("; charset=", fm); + ckfputs(code_mime, fm); } } else { - ckfputs(code, stdout); + ckfputs(code, fm); if (subtype != NULL) { - ckfputs(" ", stdout); - ckfputs(subtype, stdout); + ckfputs(" ", fm); + ckfputs(subtype, fm); } - - ckfputs(" ", stdout); - ckfputs(type, stdout); + ckfputs(" ", fm); + ckfputs(type, fm); if (has_long_lines) - ckfputs(", with very long lines", stdout); + ckfputs(", with very long lines", fm); /* * Only report line terminators if we find one other than LF, @@ -708,37 +708,37 @@ subtype_identified: */ if ((n_crlf == 0 && n_cr == 0 && n_nel == 0 && n_lf == 0) || (n_crlf != 0 || n_cr != 0 || n_nel != 0)) { - ckfputs(", with", stdout); + ckfputs(", with", fm); if (n_crlf == 0 && n_cr == 0 && n_nel == 0 && n_lf == 0) - ckfputs(" no", stdout); + ckfputs(" no", fm); else { if (n_crlf) { - ckfputs(" CRLF", stdout); + ckfputs(" CRLF", fm); if (n_cr || n_lf || n_nel) - ckfputs(",", stdout); + ckfputs(",", fm); } if (n_cr) { - ckfputs(" CR", stdout); + ckfputs(" CR", fm); if (n_lf || n_nel) - ckfputs(",", stdout); + ckfputs(",", fm); } if (n_lf) { - ckfputs(" LF", stdout); + ckfputs(" LF", fm); if (n_nel) - ckfputs(",", stdout); + ckfputs(",", fm); } if (n_nel) - ckfputs(" NEL", stdout); + ckfputs(" NEL", fm); } - ckfputs(" line terminators", stdout); + ckfputs(" line terminators", fm); } if (has_escapes) - ckfputs(", with escape sequences", stdout); + ckfputs(", with escape sequences", fm); if (has_backspace) - ckfputs(", with overstriking", stdout); + ckfputs(", with overstriking", fm); } return 1; diff --git a/file/compress.c b/file/compress.c index 7433edc7e..6427ca97f 100644 --- a/file/compress.c +++ b/file/compress.c @@ -106,7 +106,7 @@ pipe2file(int fd, void *startbuf, size_t nbytes) errno = r; #endif if (tfd == -1) { - error("Can't create temporary file for pipe copy (%s)\n", + error(EXIT_FAILURE, 0, "Can't create temporary file for pipe copy (%s)\n", strerror(errno)); /*@notreached@*/ } @@ -121,13 +121,13 @@ pipe2file(int fd, void *startbuf, size_t nbytes) switch (r) { case -1: - error("Error copying from pipe to temp file (%s)\n", + error(EXIT_FAILURE, 0, "Error copying from pipe to temp file (%s)\n", strerror(errno)); /*@notreached@*/break; case 0: break; default: - error("Error while writing to temp file (%s)\n", + error(EXIT_FAILURE, 0, "Error while writing to temp file (%s)\n", strerror(errno)); /*@notreached@*/ } @@ -138,13 +138,13 @@ pipe2file(int fd, void *startbuf, size_t nbytes) * can still access the phantom inode. */ if ((fd = dup2(tfd, fd)) == -1) { - error("Couldn't dup destcriptor for temp file(%s)\n", + error(EXIT_FAILURE, 0, "Couldn't dup destcriptor for temp file(%s)\n", strerror(errno)); /*@notreached@*/ } (void)close(tfd); if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) { - error("Couldn't seek on temp file (%s)\n", strerror(errno)); + error(EXIT_FAILURE, 0, "Couldn't seek on temp file (%s)\n", strerror(errno)); /*@notreached@*/ } return fd; @@ -228,21 +228,19 @@ uncompressbuf(int method, const unsigned char *old, /*@modifies *newch, fileSystem, internalState @*/ { int fdin[2], fdout[2]; + pid_t pid; - /* The buffer is NUL terminated, and we don't need that. */ - n--; - #ifdef HAVE_LIBZ if (method == 2) return uncompressgzipped(old,newch,n); #endif if (pipe(fdin) == -1 || pipe(fdout) == -1) { - error("cannot create pipe (%s).\n", strerror(errno)); + error(EXIT_FAILURE, 0, "cannot create pipe (%s).\n", strerror(errno)); /*@notreached@*/ } - switch (fork()) { + switch ((pid = fork())) { case 0: /* child */ (void) close(0); (void) dup(fdin[0]); @@ -259,17 +257,19 @@ uncompressbuf(int method, const unsigned char *old, (void) execvp(compr[method].argv[0], (char *const *)compr[method].argv); exit(EXIT_FAILURE); - /*@notreached@*/break; + /*@notreached@*/ break; case -1: - error("could not fork (%s).\n", strerror(errno)); + error(EXIT_FAILURE, 0, "could not fork (%s).\n", strerror(errno)); /*@notreached@*/break; default: /* parent */ (void) close(fdin[0]); (void) close(fdout[1]); + + n--; /* The buffer is NUL terminated, and we don't need that. */ if (swrite(fdin[1], old, n) != n) { n = 0; - goto err; + goto errxit; } (void) close(fdin[1]); fdin[1] = -1; @@ -277,15 +277,15 @@ uncompressbuf(int method, const unsigned char *old, if ((n = sread(fdout[0], *newch, HOWMANY)) <= 0) { free(*newch); n = 0; - goto err; + goto errxit; } /* NUL terminate, as every buffer is handled here. */ (*newch)[n++] = '\0'; -err: +errxit: if (fdin[1] != -1) (void) close(fdin[1]); (void) close(fdout[0]); - (void) wait(NULL); + pid = waitpid(pid, NULL, 0); return n; } /*@notreached@*/ diff --git a/file/configure.ac b/file/configure.ac index 6ed6aac6c..ef6017351 100644 --- a/file/configure.ac +++ b/file/configure.ac @@ -66,6 +66,7 @@ AC_HEADER_STDINT AC_HEADER_MAJOR AC_HEADER_SYS_WAIT AC_CHECK_HEADERS(sys/mman.h sys/stat.h sys/types.h) +AC_CHECK_HEADERS(error.h) AC_CHECK_HEADERS(fcntl.h) AC_CHECK_HEADERS(getopt.h) AC_CHECK_HEADERS(locale.h) @@ -99,7 +100,7 @@ AC_CHECK_SIZEOF_STDC_HEADERS(uint32_t, 0) AC_CHECK_SIZEOF_STDC_HEADERS(uint64_t, 0) dnl Checks for functions -AC_CHECK_FUNCS(mtrace mkstemp mmap strdup strerror strtoul) +AC_CHECK_FUNCS(error mtrace mkstemp mmap strdup strerror strtoul) dnl Checks for libraries AC_CHECK_LIB(z, gzopen) diff --git a/file/file.c b/file/file.c index f37d1b23f..293591794 100644 --- a/file/file.c +++ b/file/file.c @@ -84,7 +84,7 @@ unwrap(fmagic fm, char *fn) wid = 1; } else { if ((f = fopen(fn, "r")) == NULL) { - error("Cannot open `%s' (%s).\n", fn, strerror(errno)); + error(EXIT_FAILURE, 0, "Cannot open `%s' (%s).\n", fn, strerror(errno)); /*@notreached@*/ } diff --git a/file/file.h b/file/file.h index 61adf3a56..fba8e4d77 100644 --- a/file/file.h +++ b/file/file.h @@ -140,11 +140,13 @@ struct fmagic_s { struct mlist * mlist; /*!< list of arrays of magic entries */ /*@null@*/ struct mlist * ml; /*!< current magic array item */ - union VALUETYPE val; /*!< current magic expression value */ const char * fn; /*!< current file name */ + int fd; /*!< current file descriptor */ struct stat sb; /*!< current file stat(2) buffer */ unsigned char * buf; /*!< current file buffer */ int nb; /*!< current no. bytes in file buffer */ + union VALUETYPE val; /*!< current magic expression value */ + FILE * f; /*!< output file handle */ }; typedef /*@abstract@*/ struct fmagic_s * fmagic; @@ -174,16 +176,17 @@ extern int fmagicZ(fmagic fm) /*@globals fileSystem, internalState @*/ /*@modifies fm, fileSystem, internalState @*/; -/*@exits@*/ -extern void error(const char *f, ...) - /*@globals fileSystem @*/ - /*@modifies fileSystem @*/; -extern void ckfputs(const char *str, FILE *fil) - /*@globals fileSystem @*/ - /*@modifies fil, fileSystem @*/; /*@observer@*/ extern char *fmttime(long v, int local) /*@*/; + +extern void ckfputs(const char *str, fmagic fm) + /*@globals fileSystem @*/ + /*@modifies fm, fileSystem @*/; +extern void ckfprintf(fmagic fm, const char *fmt, ...) + /*@globals fileSystem @*/ + /*@modifies fm, fileSystem @*/; + extern void magwarn(const char *f, ...) /*@globals fileSystem @*/ /*@modifies fileSystem @*/; @@ -194,23 +197,6 @@ extern void showstr(FILE *fp, const char *s, int len) /*@globals fileSystem @*/ /*@modifies fp, fileSystem @*/; -/** - */ -/*@unused@*/ /*@exits@*/ /*@only@*/ -static inline void * vmefail(/*@unused@*/ size_t nb) - /*@globals fileSystem @*/ - /*@modifies fileSystem @*/ -{ - error("out of memory"); - /*@notreached@*/ -/*@-nullret@*/ - return NULL; -/*@=nullret@*/ -} - -extern void ckfprintf(FILE *f, const char *fmt, ...) - /*@globals fileSystem @*/ - /*@modifies f, fileSystem @*/; extern uint32_t signextend(struct magic *m, uint32_t v) /*@globals fileSystem @*/ /*@modifies fileSystem @*/; diff --git a/file/fsmagic.c b/file/fsmagic.c index 33db98a94..2d6a3ff7e 100644 --- a/file/fsmagic.c +++ b/file/fsmagic.c @@ -53,34 +53,33 @@ fmagicD(fmagic fm) ret = stat(fn, st); /* don't merge into if; see "ret =" above */ if (ret) { - ckfprintf(stdout, - /* Yes, I do mean stdout. */ - /* No \n, caller will provide. */ - "can't stat `%s' (%s).", fn, strerror(errno)); + /* Yes, I do mean stdout. */ + /* No \n, caller will provide. */ + ckfprintf(fm, "can't stat `%s' (%s).", fn, strerror(errno)); return 1; } if ((fm->flags & FMAGIC_FLAGS_MIME)) { if ((st->st_mode & S_IFMT) != S_IFREG) { - ckfputs("application/x-not-regular-file", stdout); + ckfputs("application/x-not-regular-file", fm); return 1; } } else { #if defined(S_ISUID) || defined(__LCLINT__) - if (st->st_mode & S_ISUID) ckfputs("setuid ", stdout); + if (st->st_mode & S_ISUID) ckfputs("setuid ", fm); #endif #if defined(S_ISGID) || defined(__LCLINT__) - if (st->st_mode & S_ISGID) ckfputs("setgid ", stdout); + if (st->st_mode & S_ISGID) ckfputs("setgid ", fm); #endif #if defined(S_ISVTX) || defined(__LCLINT__) - if (st->st_mode & S_ISVTX) ckfputs("sticky ", stdout); + if (st->st_mode & S_ISVTX) ckfputs("sticky ", fm); #endif } switch (st->st_mode & S_IFMT) { case S_IFDIR: - ckfputs("directory", stdout); + ckfputs("directory", fm); return 1; #if defined(S_IFCHR) || defined(__LCLINT__) case S_IFCHR: @@ -133,12 +132,12 @@ fmagicD(fmagic fm) /* TODO add code to handle V7 MUX and Blit MUX files */ #if defined(S_IFIFO) || defined(__LCLINT__) case S_IFIFO: - ckfputs("fifo (named pipe)", stdout); + ckfputs("fifo (named pipe)", fm); return 1; #endif #if defined(S_IFDOOR) case S_IFDOOR: - ckfputs("door", stdout); + ckfputs("door", fm); return 1; #endif #if defined(S_IFLNK) || defined(__LCLINT__) @@ -150,8 +149,7 @@ fmagicD(fmagic fm) buf[0] = '\0'; if ((nch = readlink(fn, buf, BUFSIZ-1)) <= 0) { - ckfprintf(stdout, "unreadable symlink (%s).", - strerror(errno)); + ckfprintf(fm, "unreadable symlink (%s).", strerror(errno)); return 1; } buf[nch] = '\0'; /* readlink(2) forgets this */ @@ -160,8 +158,7 @@ fmagicD(fmagic fm) /*@-branchstate@*/ if (*buf == '/') { if (stat(buf, &tstatbuf) < 0) { - ckfprintf(stdout, - "broken symbolic link to %s", buf); + ckfprintf(fm, "broken symbolic link to %s", buf); return 1; } } @@ -179,8 +176,7 @@ fmagicD(fmagic fm) tmp = buf2; } if (stat(tmp, &tstatbuf) < 0) { - ckfprintf(stdout, - "broken symbolic link to %s", buf); + ckfprintf(fm, "broken symbolic link to %s", buf); return 1; } } @@ -191,8 +187,8 @@ fmagicD(fmagic fm) xx = fmagicProcess(fm, buf, strlen(buf)); return 1; } else { /* just print what it points to */ - ckfputs("symbolic link to ", stdout); - ckfputs(buf, stdout); + ckfputs("symbolic link to ", fm); + ckfputs(buf, fm); } } return 1; @@ -200,14 +196,14 @@ fmagicD(fmagic fm) #if defined(S_IFSOCK) #ifndef __COHERENT__ case S_IFSOCK: - ckfputs("socket", stdout); + ckfputs("socket", fm); return 1; #endif #endif case S_IFREG: break; default: - error("invalid mode 0%o.\n", st->st_mode); + error(EXIT_FAILURE, 0, "invalid mode 0%o.\n", st->st_mode); /*@notreached@*/ } @@ -224,7 +220,8 @@ fmagicD(fmagic fm) * when we read the file.) */ if (!(fm->flags & FMAGIC_FLAGS_SPECIAL) && st->st_size == 0) { - ckfputs((fm->flags & FMAGIC_FLAGS_MIME) ? "application/x-empty" : "empty", stdout); + ckfputs(((fm->flags & FMAGIC_FLAGS_MIME) + ? "application/x-empty" : "empty"), fm); return 1; } return 0; @@ -259,8 +256,9 @@ fmagicF(fmagic fm, int zfl) return 'a'; /* abandon hope, all ye who remain here */ - ckfputs((fm->flags & FMAGIC_FLAGS_MIME) ? "application/octet-stream" : "data", stdout); - return '\0'; + ckfputs(((fm->flags & FMAGIC_FLAGS_MIME) + ? "application/octet-stream" : "data"), fm); + return '\0'; } /* @@ -281,7 +279,7 @@ fmagicProcess(fmagic fm, const char *fn, int wid) if (strcmp("-", fn) == 0) { if (fstat(0, &fm->sb)<0) { - error("cannot fstat `%s' (%s).\n", stdname, + error(EXIT_FAILURE, 0, "cannot fstat `%s' (%s).\n", stdname, strerror(errno)); /*@notreached@*/ } @@ -302,11 +300,10 @@ fmagicProcess(fmagic fm, const char *fn, int wid) if ((fd = open(fm->fn, O_RDONLY)) < 0) { /* We can't open it, but we were able to stat it. */ if (fm->sb.st_mode & 0002) - ckfputs("writeable, ", stdout); + ckfputs("writeable, ", fm); if (fm->sb.st_mode & 0111) - ckfputs("executable, ", stdout); - ckfprintf(stdout, "can't read `%s' (%s).", - fm->fn, strerror(errno)); + ckfputs("executable, ", fm); + ckfprintf(fm, "can't read `%s' (%s).", fm->fn, strerror(errno)); goto exit; } } @@ -316,12 +313,13 @@ fmagicProcess(fmagic fm, const char *fn, int wid) * try looking at the first HOWMANY bytes */ if ((fm->nb = read(fd, (char *)fm->buf, HOWMANY)) == -1) { - error("read failed (%s).\n", strerror(errno)); + error(EXIT_FAILURE, 0, "read failed (%s).\n", strerror(errno)); /*@notreached@*/ } if (fm->nb == 0) - ckfputs((fm->flags & FMAGIC_FLAGS_MIME) ? "application/x-empty" : "empty", stdout); + ckfputs(((fm->flags & FMAGIC_FLAGS_MIME) + ? "application/x-empty" : "empty"), fm); else { fm->buf[fm->nb++] = '\0'; /* null-terminate it */ match = fmagicF(fm, (fm->flags & FMAGIC_FLAGS_UNCOMPRESS)); @@ -367,7 +365,7 @@ fmagicProcess(fmagic fm, const char *fn, int wid) } exit: - (void) putchar('\n'); + ckfputs("\n", fm); fm->buf = NULL; fm->nb = 0; return ret; diff --git a/file/print.c b/file/print.c index 0b6aafd04..08476a23b 100644 --- a/file/print.c +++ b/file/print.c @@ -134,44 +134,52 @@ mdump(struct magic *m) * ckfprintf - fprintf, but with error checking */ void -ckfputs(const char *str, FILE *fil) +ckfputs(const char *str, fmagic fm) { - if (fputs(str,fil) == EOF) - error("write failed.\n"); + FILE *f = (fm->f ? fm->f : stdout); + if (fputs(str, f) == EOF) + error(EXIT_FAILURE, 0, "ckfputs write failed.\n"); } /*VARARGS*/ void -ckfprintf(FILE *f, const char *fmt, ...) +ckfprintf(fmagic fm, const char *fmt, ...) { + FILE *f = (fm->f ? fm->f : stdout); + va_list va; va_start(va, fmt); (void) vfprintf(f, fmt, va); if (ferror(f)) - error("write failed.\n"); + error(EXIT_FAILURE, 0, "ckfprintf write failed.\n"); va_end(va); } +#if !defined(HAVE_ERROR) /* * error - print best error message possible and exit */ /*VARARGS*/ void -error(const char *f, ...) +error(int status, int errnum, const char *fmt, ...) { va_list va; - va_start(va, f); + va_start(va, fmt); /* cuz we use stdout for most, stderr here */ (void) fflush(stdout); if (progname != NULL) (void) fprintf(stderr, "%s: ", progname); - (void) vfprintf(stderr, f, va); + (void) vfprintf(stderr, fmt, va); va_end(va); - exit(EXIT_FAILURE); +#if NOTYET + if (status) +#endif + exit(status); } +#endif /*VARARGS*/ void diff --git a/file/readelf.c b/file/readelf.c index 4e0fb7464..36c7d4814 100644 --- a/file/readelf.c +++ b/file/readelf.c @@ -114,12 +114,16 @@ doshn(int cls, int swap, int fd, off_t off, int num, size_t size) Elf32_Shdr sh32; Elf64_Shdr sh64; - if (lseek(fd, off, SEEK_SET) == -1) - error("lseek failed (%s).\n", strerror(errno)); + if (lseek(fd, off, SEEK_SET) == -1) { + error(EXIT_FAILURE, 0, "lseek failed (%s).\n", strerror(errno)); + /*@notreached@*/ + } for ( ; num; num--) { - if (read(fd, sh_addr, size) == -1) - error("read failed (%s).\n", strerror(errno)); + if (read(fd, sh_addr, size) == -1) { + error(EXIT_FAILURE, 0, "read failed (%s).\n", strerror(errno)); + /*@notreached@*/ + } if (shs_type == SHT_SYMTAB /* || shs_type == SHT_DYNSYM */) { (void) printf (", not stripped"); return; @@ -148,12 +152,16 @@ dophn_exec(int cls, int swap, int fd, off_t off, int num, size_t size) int bufsize; size_t offset, nameoffset; - if (lseek(fd, off, SEEK_SET) == -1) - error("lseek failed (%s).\n", strerror(errno)); + if (lseek(fd, off, SEEK_SET) == -1) { + error(EXIT_FAILURE, 0, "lseek failed (%s).\n", strerror(errno)); + /*@notreached@*/ + } for ( ; num; num--) { - if (read(fd, ph_addr, size) == -1) - error("read failed (%s).\n", strerror(errno)); + if (read(fd, ph_addr, size) == -1) { + error(EXIT_FAILURE, 0, "read failed (%s).\n", strerror(errno)); + /*@notreached@*/ + } switch (ph_type) { case PT_DYNAMIC: @@ -167,12 +175,16 @@ dophn_exec(int cls, int swap, int fd, off_t off, int num, size_t size) * This is a PT_NOTE section; loop through all the notes * in the section. */ - if (lseek(fd, (off_t) ph_offset, SEEK_SET) == -1) - error("lseek failed (%s).\n", strerror(errno)); + if (lseek(fd, (off_t) ph_offset, SEEK_SET) == -1) { + error(EXIT_FAILURE, 0, "lseek failed (%s).\n", strerror(errno)); + /*@notreached@*/ + } bufsize = read(fd, nbuf, BUFSIZ); - if (bufsize == -1) - error(": " "read failed (%s).\n", + if (bufsize == -1) { + error(EXIT_FAILURE, 0, ": " "read failed (%s).\n", strerror(errno)); + /*@notreached@*/ + } offset = 0; for (;;) { if (offset >= bufsize) @@ -345,10 +357,14 @@ dophn_core(int cls, int swap, int fd, off_t off, int num, size_t size) * Loop through all the program headers. */ for ( ; num; num--) { - if (lseek(fd, off, SEEK_SET) == -1) - error("lseek failed (%s).\n", strerror(errno)); - if (read(fd, ph_addr, size) == -1) - error("read failed (%s).\n", strerror(errno)); + if (lseek(fd, off, SEEK_SET) == -1) { + error(EXIT_FAILURE, 0, "lseek failed (%s).\n", strerror(errno)); + /*@notreached@*/ + } + if (read(fd, ph_addr, size) == -1) { + error(EXIT_FAILURE, 0, "read failed (%s).\n", strerror(errno)); + /*@notreached@*/ + } off += size; if (ph_type != PT_NOTE) continue; @@ -357,11 +373,15 @@ dophn_core(int cls, int swap, int fd, off_t off, int num, size_t size) * This is a PT_NOTE section; loop through all the notes * in the section. */ - if (lseek(fd, (off_t) ph_offset, SEEK_SET) == -1) - error("lseek failed (%s).\n", strerror(errno)); + if (lseek(fd, (off_t) ph_offset, SEEK_SET) == -1) { + error(EXIT_FAILURE, 0, "lseek failed (%s).\n", strerror(errno)); + /*@notreached@*/ + } bufsize = read(fd, nbuf, BUFSIZ); - if (bufsize == -1) - error(": " "read failed (%s).\n", strerror(errno)); + if (bufsize == -1) { + error(EXIT_FAILURE, 0, ": " "read failed (%s).\n", strerror(errno)); + /*@notreached@*/ + } offset = 0; for (;;) { if (offset >= bufsize) diff --git a/file/softmagic.c b/file/softmagic.c index 1c05d2466..56c5bc211 100644 --- a/file/softmagic.c +++ b/file/softmagic.c @@ -101,8 +101,8 @@ fmagicSPrint(const fmagic fm, struct magic *m) break; default: - error("invalid m->type (%d) in fmagicSPrint().\n", m->type); - /*@notreached@*/ + error(EXIT_FAILURE, 0, "invalid m->type (%d) in fmagicSPrint().\n", m->type); + /*@notreached@*/ break; } return(t); } @@ -377,7 +377,7 @@ fmagicSConvert(fmagic fm, struct magic *m) case REGEX: return 1; default: - error("invalid type %d in fmagicSConvert().\n", m->type); + error(EXIT_FAILURE, 0, "invalid type %d in fmagicSConvert().\n", m->type); /*@notreached@*/ return 0; } @@ -871,7 +871,8 @@ fmagicSCheck(const fmagic fm, struct magic *m) rc = regcomp(&rx, m->value.s, REG_EXTENDED|REG_NOSUB); if (rc) { (void) regerror(rc, &rx, errmsg, sizeof(errmsg)); - error("regex error %d, (%s)\n", rc, errmsg); + error(EXIT_FAILURE, 0, "regex error %d, (%s)\n", rc, errmsg); + /*@notreached@*/ } else { rc = regexec(&rx, p->buf, 0, 0, 0); return !rc; @@ -879,7 +880,7 @@ fmagicSCheck(const fmagic fm, struct magic *m) } /*@notreached@*/ break; default: - error("invalid type %d in fmagicSCheck().\n", m->type); + error(EXIT_FAILURE, 0, "invalid type %d in fmagicSCheck().\n", m->type); /*@notreached@*/ return 0; } @@ -954,7 +955,7 @@ fmagicSCheck(const fmagic fm, struct magic *m) default: matched = 0; - error("fmagicSCheck: can't happen: invalid relation %d.\n", m->reln); + error(EXIT_FAILURE, 0, "fmagicSCheck: can't happen: invalid relation %d.\n", m->reln); /*@notreached@*/ break; } @@ -1067,7 +1068,7 @@ fmagicSMatch(const fmagic fm) && (m->nospflag == 0) && (m->desc[0] != '\0') ) { - (void) putchar(' '); + ckfputs(" ", fm); need_separator = 0; } if ((cont_level+1) >= tmplen) diff --git a/file/system.h b/file/system.h index c06a21331..e3880e077 100644 --- a/file/system.h +++ b/file/system.h @@ -41,6 +41,15 @@ extern int errno; /*@=declundef @*/ #endif +#if HAVE_ERROR && HAVE_ERROR_H +#include <error.h> +#else +/*@exits@*/ +extern void error(int status, int errnum, const char *f, ...) + /*@globals fileSystem @*/ + /*@modifies fileSystem @*/; +#endif + #ifdef STDC_HEADERS #include <stdlib.h> #ifdef HAVE_STDINT_H @@ -103,6 +112,10 @@ extern int _tolower(int) __THROW /*@*/; #include <sys/file.h> #endif +#if HAVE_SYS_WAIT_H +#include <sys/wait.h> +#endif + #if HAVE_SYS_MMAN_H && !defined(__LCLINT__) #include <sys/mman.h> #endif @@ -223,6 +236,20 @@ char * xstrdup (const char *str) /*@=fcnuse@*/ /*@=declundef =exportfcn=incondefs @*/ +/** + */ +/*@unused@*/ /*@exits@*/ /*@only@*/ +static inline void * vmefail(/*@unused@*/ size_t nb) + /*@globals fileSystem @*/ + /*@modifies fileSystem @*/ +{ + error(EXIT_FAILURE, 0, "out of memory"); + /*@notreached@*/ +/*@-nullret@*/ + return NULL; +/*@=nullret@*/ +} + #if HAVE_MCHECK_H #include <mcheck.h> #if defined(__LCLINT__) |