diff options
author | Arnold D. Robbins <arnold@skeeve.com> | 2013-10-24 22:03:09 +0300 |
---|---|---|
committer | Arnold D. Robbins <arnold@skeeve.com> | 2013-10-24 22:03:09 +0300 |
commit | 25520aab6144927a20d501c0396e9597f36fc871 (patch) | |
tree | 1a8b237e152873b5a789f10865c11a5ed1e0c3cc /main.c | |
parent | eb152bbe507aef92ece4a301863263818fb50a04 (diff) | |
download | egawk-25520aab6144927a20d501c0396e9597f36fc871.tar.gz egawk-25520aab6144927a20d501c0396e9597f36fc871.tar.bz2 egawk-25520aab6144927a20d501c0396e9597f36fc871.zip |
Improve handling of writes to dead pipes.
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 23 |
1 files changed, 19 insertions, 4 deletions
@@ -157,7 +157,7 @@ GETGROUPS_T *groupset; /* current group set */ int ngroups; /* size of said set */ #endif -void (*lintfunc)(const char *mesg, ...) = warning; +void (*lintfunc)(const char *mesg, ...) = r_warning; static const struct option optab[] = { { "traditional", no_argument, NULL, 'c' }, @@ -289,6 +289,14 @@ main(int argc, char **argv) * Ignore SIGPIPE so that writes to pipes that fail don't * kill the process but instead return -1 and set errno. * That lets us print a fatal message instead of dieing suddenly. + * + * Note that this requires ignoring EPIPE when writing and + * flushing stdout/stderr in other parts of the program. E.g., + * + * gawk 'BEGIN { print "hi" }' | exit + * + * should not give us "broken pipe" messages --- mainly because + * it did not do so in the past and people would complain. */ signal(SIGPIPE, SIG_IGN); #endif @@ -849,8 +857,13 @@ By default it reads standard input and writes standard output.\n\n"), fp); fflush(fp); if (ferror(fp)) { - if (fp == stdout) - warning(_("error writing standard output (%s)"), strerror(errno)); + /* don't warn about stdout/stderr if EPIPE, but do error exit */ + if (errno != EPIPE) { + if (fp == stdout) + warning(_("error writing standard output (%s)"), strerror(errno)); + else if (fp == stderr) + warning(_("error writing standard error (%s)"), strerror(errno)); + } exit(EXIT_FAILURE); } @@ -887,7 +900,9 @@ along with this program. If not, see http://www.gnu.org/licenses/.\n"); fflush(stdout); if (ferror(stdout)) { - warning(_("error writing standard output (%s)"), strerror(errno)); + /* don't warn about stdout if EPIPE, but do error exit */ + if (errno != EPIPE) + warning(_("error writing standard output (%s)"), strerror(errno)); exit(EXIT_FAILURE); } |