summaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2023-02-03 01:19:44 -0800
committerPaul Eggert <eggert@cs.ucla.edu>2023-02-03 01:26:07 -0800
commit75dac03adcdf79b8d38a87bf29f50bcde9fa46a5 (patch)
tree37821cee94a2e66f3833feb43ab7725aa6490601 /util.c
parent54d039eb3665aedf46fc2f84052162724b7e5aa7 (diff)
downloadgzip-75dac03adcdf79b8d38a87bf29f50bcde9fa46a5.tar.gz
gzip: fix exit status on broken pipe
Fix gzip to behave like cat etc. when outputting to a broken pipe: i.e., exit with nonzero status if SIGPIPE is ignored, and be terminated by SIGPIPE otherwise. * NEWS: Mention this. * gzip.c: Do not install signal handlers unless creating an output file, for which signal handlers are needed. This avoids gzip having to deal with signal handlers when outputting to stdout. (exiting_signal): Remove. All uses removed. (main): Do not install signal handlers at first. (create_outfile): Instead, install them only when needed. (finish_up_gzip): New function, which generalizes abort_gzip. (abort_gzip): Use it. * tests/pipe-output: New test. * tests/Makefile.am (TESTS): Add it. * util.c (EPIPE): Default to 0. (write_error): Just warn if it is a pipe error, and suppress that warning if quiet. In any event exit with status 2 (warning), not status 1 (error). * zgrep.in: Treat gzip status 141 like status 2; it is a broken pipe either way.
Diffstat (limited to 'util.c')
-rw-r--r--util.c8
1 files changed, 7 insertions, 1 deletions
diff --git a/util.c b/util.c
index 642e620..71a937f 100644
--- a/util.c
+++ b/util.c
@@ -36,6 +36,10 @@
# define CHAR_BIT 8
#endif
+#ifndef EPIPE
+# define EPIPE 0
+#endif
+
static int write_buffer (int, voidp, unsigned int);
/* ========================================================================
@@ -452,8 +456,10 @@ void read_error()
void write_error()
{
+ int exitcode = errno == EPIPE ? WARNING : ERROR;
+ if (! (exitcode == WARNING && quiet))
fprintf (stderr, "\n%s: %s: %s\n", program_name, ofname, strerror (errno));
- abort_gzip();
+ finish_up_gzip (exitcode);
}
/* ========================================================================