diff options
author | Arnold D. Robbins <arnold@skeeve.com> | 2021-12-01 22:46:18 +0200 |
---|---|---|
committer | Arnold D. Robbins <arnold@skeeve.com> | 2021-12-01 22:46:18 +0200 |
commit | 0521e672efdbfdaedabfacca03c7a535b4f8526c (patch) | |
tree | 96c5db398195d2b81d66a891fae0848b97bfe87d /builtin.c | |
parent | a22095c81a677b82accccb53cab91052cdc0cbfe (diff) | |
parent | f77e1318c515d495ac9c08bdfdf2dadf79a9649f (diff) | |
download | egawk-0521e672efdbfdaedabfacca03c7a535b4f8526c.tar.gz egawk-0521e672efdbfdaedabfacca03c7a535b4f8526c.tar.bz2 egawk-0521e672efdbfdaedabfacca03c7a535b4f8526c.zip |
Merge branch 'gawk-5.1-stable'
Diffstat (limited to 'builtin.c')
-rw-r--r-- | builtin.c | 77 |
1 files changed, 44 insertions, 33 deletions
@@ -96,43 +96,14 @@ fatal(_("attempt to use array `%s' in a scalar context"), array_vname(s1)); \ */ #define GAWK_RANDOM_MAX 0x7fffffffL -/* efwrite --- like fwrite, but with error checking */ + +/* wrerror --- handle a write or flush error */ static void -efwrite(const void *ptr, - size_t size, - size_t count, - FILE *fp, - const char *from, - struct redirect *rp, - bool flush) +wrerror(FILE *fp, const char *from, struct redirect *rp) { - errno = 0; - if (rp != NULL) { - if (rp->output.gawk_fwrite(ptr, size, count, fp, rp->output.opaque) != count) - goto wrerror; - } else if (fwrite(ptr, size, count, fp) != count) - goto wrerror; - if (flush - && ((fp == stdout && output_is_tty) - || (rp != NULL && (rp->flag & RED_NOBUF) != 0))) { - if (rp != NULL) { - rp->output.gawk_fflush(fp, rp->output.opaque); - if (rp->output.gawk_ferror(fp, rp->output.opaque)) - goto wrerror; - } else { - fflush(fp); - if (ferror(fp)) - goto wrerror; - } - } - return; + os_maybe_set_errno(); -wrerror: -#ifdef __MINGW32__ - if (errno == 0 || errno == EINVAL) - w32_maybe_set_errno(); -#endif /* for stdout, die with a real SIGPIPE, like other awks */ if (fp == stdout && errno == EPIPE) die_via_sigpipe(); @@ -150,6 +121,46 @@ wrerror: errno ? strerror(errno) : _("reason unknown")); } +/* efflush --- flush output with proper error handling */ + +void +efflush(FILE *fp, const char *from, struct redirect *rp) +{ + errno = 0; + if (rp != NULL) { + rp->output.gawk_fflush(fp, rp->output.opaque); + if (rp->output.gawk_ferror(fp, rp->output.opaque)) + wrerror(fp, from, rp); + } else { + fflush(fp); + if (ferror(fp)) + wrerror(fp, from, rp); + } +} + +/* efwrite --- like fwrite, but with error checking */ + +static void +efwrite(const void *ptr, + size_t size, + size_t count, + FILE *fp, + const char *from, + struct redirect *rp, + bool flush) +{ + errno = 0; + if (rp != NULL) { + if (rp->output.gawk_fwrite(ptr, size, count, fp, rp->output.opaque) != count) + return wrerror(fp, from, rp); + } else if (fwrite(ptr, size, count, fp) != count) + return wrerror(fp, from, rp); + if (flush + && ((fp == stdout && output_is_tty) + || (rp != NULL && (rp->flag & RED_NOBUF) != 0))) + efflush(fp, from, rp); +} + /* do_exp --- exponential function */ NODE * |