diff --git a/builtin.c b/builtin.c index 2acd992..fe0e2f1 100644 --- a/builtin.c +++ b/builtin.c @@ -139,7 +139,7 @@ wrerror: update_ERRNO_int(errno); else fatal(_("%s to \"%s\" failed (%s)"), from, - rp ? rp->value : _("standard output"), + rp ? rp->value : ((fp == stdout) ? _("standard output") : _("standard error")), errno ? strerror(errno) : _("reason unknown")); } @@ -245,13 +245,35 @@ do_fflush(int nargs) return make_number((AWKNUM) status); } fp = rp->output.fp; - if (fp != NULL) + if (fp != NULL) { status = rp->output.gawk_fflush(fp, rp->output.opaque); + /* do we need to call rp->output.gawk_ferror here? */ + if (status) { + if (is_non_fatal_redirect(rp->value, strlen(rp->value))) + update_ERRNO_int(errno); + else + fatal(_("%s to \"%s\" failed (%s)"), "fflush", + rp->value, + errno ? strerror(errno) : _("reason unknown")); + } + } else if ((rp->flag & RED_TWOWAY) != 0) warning(_("fflush: cannot flush: two-way pipe `%.*s' has closed write end"), len, file); } else if ((fp = stdfile(tmp->stptr, tmp->stlen)) != NULL) { status = fflush(fp); + if (status) { + if (is_non_fatal_std(fp)) + update_ERRNO_int(errno); + else { + /* die silently on EPIPE to stdout */ + if (fp == stdout && errno == EPIPE) + gawk_exit(EXIT_FATAL); + fatal(_("%s to \"%s\" failed (%s)"), "fflush", + (fp == stdout) ? _("standard output") : _("standard error"), + errno ? strerror(errno) : _("reason unknown")); + } + } } else { status = -1; warning(_("fflush: `%.*s' is not an open file, pipe or co-process"), len, file); diff --git a/io.c b/io.c index cced126..10e4cd9 100644 --- a/io.c +++ b/io.c @@ -1403,11 +1403,27 @@ flush_io() errno = 0; /* we don't warn about stdout/stderr if EPIPE, but we do error exit */ if (fflush(stdout)) { + if (is_non_fatal_std(stdout)) + update_ERRNO_int(errno); + else { + if (errno == EPIPE) + /* die silently on EPIPE to stdout */ + gawk_exit(EXIT_FATAL); + fatal(_("%s to \"%s\" failed (%s)"), "fflush", + _("standard output"), + errno ? strerror(errno) : _("reason unknown")); + } if (errno != EPIPE) warning(_("error writing standard output (%s)"), strerror(errno)); status++; } if (fflush(stderr)) { + if (is_non_fatal_std(stderr)) + update_ERRNO_int(errno); + else + fatal(_("%s to \"%s\" failed (%s)"), "fflush", + _("standard error"), + errno ? strerror(errno) : _("reason unknown")); if (errno != EPIPE) warning(_("error writing standard error (%s)"), strerror(errno)); status++; @@ -1416,6 +1432,11 @@ flush_io() /* flush both files and pipes, what the heck */ if ((rp->flag & RED_WRITE) != 0 && rp->output.fp != NULL) { if (rp->output.gawk_fflush(rp->output.fp, rp->output.opaque)) { + if (!is_non_fatal_redirect(rp->value, strlen(rp->value))) + fatal(_("%s to \"%s\" failed (%s)"), "fflush", + rp->value, + errno ? strerror(errno) : _("reason unknown")); + update_ERRNO_int(errno); if ((rp->flag & RED_PIPE) != 0) warning(_("pipe flush of `%s' failed (%s)."), rp->value, strerror(errno));