Fix bugs with cli flags in gzip.com

This commit is contained in:
Justine Tunney 2023-04-28 05:15:58 -07:00
parent 1c2da3a55a
commit bf6459e324
No known key found for this signature in database
GPG key ID: BE714B4575D6E328

View file

@ -137,13 +137,8 @@ void Compress(const char *inpath) {
int rc, n, errnum;
const char *outpath;
char *p, openflags[5];
outpath = 0;
if (inpath) {
input = fopen(inpath, "rb");
} else if (opt_usestdout && (opt_force || !isatty(1))) {
if ((!inpath || opt_usestdout) && (!isatty(1) || opt_force)) {
opt_usestdout = true;
inpath = "/dev/stdin";
input = stdin;
} else {
fputs(prog, stderr);
fputs(": compressed data not written to a terminal."
@ -151,6 +146,12 @@ void Compress(const char *inpath) {
stderr);
exit(1);
}
if (inpath) {
input = fopen(inpath, "rb");
} else {
inpath = "/dev/stdin";
input = stdin;
}
p = openflags;
*p++ = opt_append ? 'a' : 'w';
*p++ = 'b';
@ -160,7 +161,7 @@ void Compress(const char *inpath) {
*p = 0;
if (opt_usestdout) {
outpath = "/dev/stdout";
output = gzdopen(0, openflags);
output = gzdopen(1, openflags);
} else {
if (strlen(inpath) + 3 + 1 > PATH_MAX) _Exit(2);
stpcpy(stpcpy(pathbuf, inpath), ".gz");