summaryrefslogtreecommitdiff
path: root/usr.bin/compress
diff options
context:
space:
mode:
authorTodd C. Miller <millert@cvs.openbsd.org>2003-01-07 18:48:07 +0000
committerTodd C. Miller <millert@cvs.openbsd.org>2003-01-07 18:48:07 +0000
commit89aa00e87f4af2c6f9a31ccea2724cafc2f018db (patch)
tree0f9fcbd8597f576fb15519ab44acb47f13a3b93f /usr.bin/compress
parent4156a01435689ca691b5b2d0ec3d57468ca6e5e2 (diff)
Avoid trashing an existing output file when the input cannot be read.
Changes open order slightly and makes a distiction between errors < 0 and > 0. Prompted by a discussion on the freebsd-audit list.
Diffstat (limited to 'usr.bin/compress')
-rw-r--r--usr.bin/compress/main.c34
1 files changed, 17 insertions, 17 deletions
diff --git a/usr.bin/compress/main.c b/usr.bin/compress/main.c
index 713f411cfec..ecc1d01daac 100644
--- a/usr.bin/compress/main.c
+++ b/usr.bin/compress/main.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: main.c,v 1.20 2003/01/06 18:09:22 mickey Exp $ */
+/* $OpenBSD: main.c,v 1.21 2003/01/07 18:48:06 millert Exp $ */
static const char copyright[] =
"@(#) Copyright (c) 1992, 1993\n\
@@ -36,7 +36,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.20 2003/01/06 18:09:22 mickey Exp $";
+static const char main_rcsid[] = "$OpenBSD: main.c,v 1.21 2003/01/07 18:48:06 millert Exp $";
#endif
#endif /* not lint */
@@ -407,7 +407,7 @@ main(argc, argv)
}
}
- if (error && oreg && unlink(outfile) && errno != ENOENT &&
+ if (error > 0 && oreg && unlink(outfile) && errno != ENOENT &&
verbose >= 0) {
if (force) {
warn("output: %s", outfile);
@@ -437,22 +437,22 @@ compress(in, out, method, bits, sb)
error = 0;
cookie = NULL;
- if ((ofd = open(out, O_WRONLY|O_CREAT, S_IWUSR)) < 0) {
+ if ((ifd = open(in, O_RDONLY)) < 0) {
if (verbose >= 0)
warn("%s", out);
return (-1);
}
- if (method != M_COMPRESS && !force && isatty(ofd)) {
+ if ((ofd = open(out, O_WRONLY|O_CREAT, S_IWUSR)) < 0) {
if (verbose >= 0)
- warnx("%s: won't write compressed data to terminal",
- out);
+ warn("%s", out);
return (-1);
}
- if ((ifd = open(in, O_RDONLY)) < 0) {
+ if (method != M_COMPRESS && !force && isatty(ofd)) {
if (verbose >= 0)
- warn("%s", out);
+ warnx("%s: won't write compressed data to terminal",
+ out);
return (-1);
}
@@ -486,7 +486,7 @@ compress(in, out, method, bits, sb)
error++;
}
- return (error ? -1 : 0);
+ return (error);
}
const struct compressor *
@@ -545,13 +545,13 @@ decompress(in, out, method, bits, sb)
return -1;
}
- if ((ofd = open(out, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR)) < 0) {
- if (verbose >= 0)
- warn("%s", in);
- return -1;
- }
-
if ((cookie = (*method->open)(ifd, "r", bits)) != NULL) {
+ 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) {
@@ -575,7 +575,7 @@ decompress(in, out, method, bits, sb)
error++;
}
- return error;
+ return (error);
}
void