diff options
author | Michael Shalayeff <mickey@cvs.openbsd.org> | 2003-07-08 00:30:13 +0000 |
---|---|---|
committer | Michael Shalayeff <mickey@cvs.openbsd.org> | 2003-07-08 00:30:13 +0000 |
commit | da4970237bd179c05018c9955df252b075c13ad2 (patch) | |
tree | d9011577715e48eeedaf35ec35e2d0097a1b0b0f /usr.bin/compress/main.c | |
parent | cef4eeb2b75e7caa0e5260d19860344c84c1b07d (diff) |
fix fd closing logicand close in gzopen(); found by wilfried@ and millert@ ok
Diffstat (limited to 'usr.bin/compress/main.c')
-rw-r--r-- | usr.bin/compress/main.c | 84 |
1 files changed, 51 insertions, 33 deletions
diff --git a/usr.bin/compress/main.c b/usr.bin/compress/main.c index 3c3c142aebc..3238f738a51 100644 --- a/usr.bin/compress/main.c +++ b/usr.bin/compress/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.31 2003/06/30 03:42:05 millert Exp $ */ +/* $OpenBSD: main.c,v 1.32 2003/07/08 00:30:12 mickey Exp $ */ static const char copyright[] = "@(#) Copyright (c) 1992, 1993\n\ @@ -35,7 +35,7 @@ static const char license[] = #if 0 static char sccsid[] = "@(#)compress.c 8.2 (Berkeley) 1/7/94"; #else -static const char main_rcsid[] = "$OpenBSD: main.c,v 1.31 2003/06/30 03:42:05 millert Exp $"; +static const char main_rcsid[] = "$OpenBSD: main.c,v 1.32 2003/07/08 00:30:12 mickey Exp $"; #endif #endif /* not lint */ @@ -451,6 +451,7 @@ compress(const char *in, const char *out, const struct compressor *method, if ((ofd = open(out, O_WRONLY|O_CREAT, S_IWUSR)) < 0) { if (verbose >= 0) warn("%s", out); + (void) close(ifd); return (-1); } @@ -458,39 +459,45 @@ compress(const char *in, const char *out, const struct compressor *method, if (verbose >= 0) warnx("%s: won't write compressed data to terminal", out); + (void) close(ofd); + (void) close(ifd); return (-1); } - if ((cookie = (*method->open)(ofd, "w", bits)) != NULL) { - - while ((nr = read(ifd, buf, sizeof(buf))) > 0) - if ((method->write)(cookie, buf, nr) != nr) { - if (verbose >= 0) - warn("%s", out); - error++; - break; - } + if ((cookie = (*method->open)(ofd, "w", bits)) == NULL) { + if (verbose >= 0) + warn("%s", in); + (void) close(ofd); + (void) close(ifd); + return (-1); } - if (cookie == NULL || nr < 0) { - if (!error && verbose >= 0) + while ((nr = read(ifd, buf, sizeof(buf))) > 0) + if ((method->write)(cookie, buf, nr) != nr) { + if (verbose >= 0) + warn("%s", out); + error++; + break; + } + + if (!error && nr < 0) { + if (verbose >= 0) warn("%s", in); error++; } - if (cookie == NULL || (method->close)(cookie)) { + if ((method->close)(cookie)) { if (!error && verbose >= 0) warn("%s", out); error++; - (void) close(ofd); } if (close(ifd)) { if (!error && verbose >= 0) - warn("%s", out); + warn("%s", in); error++; } - + return (error); } @@ -543,29 +550,40 @@ decompress(const char *in, const char *out, const struct compressor *method, return -1; } - if ((cookie = (*method->open)(ifd, "r", bits)) != NULL) { - if ((ofd = open(out, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR)) < 0) { + if ((cookie = (*method->open)(ifd, "r", bits)) == NULL) { + if (verbose >= 0) + warn("%s", in); + error++; + close (ifd); + return -1; + } + + if ((ofd = open(out, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR)) < 0) { + if (verbose >= 0) + warn("%s", in); + (method->close)(cookie); + return -1; + } + + while ((nr = (method->read)(cookie, buf, sizeof(buf))) > 0) + if (write(ofd, buf, nr) != nr) { if (verbose >= 0) - warn("%s", in); - (method->close)(cookie); - return -1; + warn("%s", out); + error++; + break; } - while ((nr = (method->read)(cookie, buf, sizeof(buf))) > 0) - if (write(ofd, buf, nr) != nr) { - if (verbose >= 0) - warn("%s", out); - error++; - break; - } + if (!error && nr < 0) { + if (verbose >= 0) + warnx("%s: %s", in, + errno == EINVAL ? "crc error" : strerror(errno)); + error++; } - if (cookie == NULL || (method->close)(cookie) || nr < 0) { + if ((method->close)(cookie)) { if (!error && verbose >= 0) - warnx("%s: %s", in, - errno == EINVAL ? "crc error" : strerror(errno)); + warnx("%s", in); error++; - close (ifd); } if (close(ofd)) { |