diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2019-01-31 23:46:56 -0500 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2019-02-01 12:50:29 -0500 |
commit | aa5f0f5745e4e7392c078d1fe28825c941f52f7c (patch) | |
tree | 897376cebf759a6e90c4b94f7b3dd5fe3a42025a /util.c | |
parent | 02132c2efb6daa213aa07b805ccb0c20241ca2f7 (diff) | |
download | bfs-aa5f0f5745e4e7392c078d1fe28825c941f52f7c.tar.xz |
main: Fix closed standard stream handling
bfs >&- should complain about a missing file descriptor, rather than
silently succeeding.
Diffstat (limited to 'util.c')
-rw-r--r-- | util.c | 10 |
1 files changed, 3 insertions, 7 deletions
@@ -85,8 +85,6 @@ bool isopen(int fd) { } int redirect(int fd, const char *path, int flags, ...) { - close(fd); - mode_t mode = 0; if (flags & O_CREAT) { va_list args; @@ -102,11 +100,9 @@ int redirect(int fd, const char *path, int flags, ...) { int ret = open(path, flags, mode); if (ret >= 0 && ret != fd) { - int other = ret; - ret = dup2(other, fd); - if (close(other) != 0) { - ret = -1; - } + int orig = ret; + ret = dup2(orig, fd); + close(orig); } return ret; |