diff options
author | Arnold D. Robbins <arnold@skeeve.com> | 2011-01-27 21:23:09 +0200 |
---|---|---|
committer | Arnold D. Robbins <arnold@skeeve.com> | 2011-01-27 21:23:09 +0200 |
commit | fcdb37e7e7c6bbfc8726d57af4a0e1cb6dd01f6f (patch) | |
tree | 7b36ecae4be3281497a1a6299740d3ae96a7528c /io.c | |
parent | 35b49bd68a818ff9a1720e2e8ef05f723b15e531 (diff) | |
download | egawk-fcdb37e7e7c6bbfc8726d57af4a0e1cb6dd01f6f.tar.gz egawk-fcdb37e7e7c6bbfc8726d57af4a0e1cb6dd01f6f.tar.bz2 egawk-fcdb37e7e7c6bbfc8726d57af4a0e1cb6dd01f6f.zip |
Bug fixes and cleanup.
Diffstat (limited to 'io.c')
-rw-r--r-- | io.c | 28 |
1 files changed, 25 insertions, 3 deletions
@@ -438,6 +438,25 @@ inrec(IOBUF *iop) return retval; } +/* remap_std_file --- reopen a standard descriptor on /dev/null */ + +static int +remap_std_file(int oldfd) +{ + int newfd; + int ret = -1; + + newfd = open("/dev/null", O_RDWR); + if (newfd >= 0) { + /* dup2() will close fileno(fp) for us first. */ + ret = dup2(newfd, oldfd); + if (ret == 0) + close(newfd); + } + + return ret; +} + /* iop_close --- close an open IOP */ static int @@ -458,12 +477,15 @@ iop_close(IOBUF *iop) iop->flag &= ~IOP_AT_EOF; iop->flag |= IOP_CLOSED; /* there may be dangling pointers */ iop->dataend = NULL; - /* Don't close standard files or else crufty code elsewhere will lose */ - /* FIXME: *DO* close it. Just reopen on an invalid handle. */ + /* + * Closing standard files can cause crufty code elsewhere to lose. + * So we remap the standard file to /dev/null. + * Thanks to Jim Meyering for the suggestion. + */ if (iop->fd == fileno(stdin) || iop->fd == fileno(stdout) || iop->fd == fileno(stderr)) - ret = 0; + ret = remap_std_file(iop->fd); else ret = close(iop->fd); |