summaryrefslogtreecommitdiffstats
path: root/util.c
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2019-01-31 23:46:56 -0500
committerTavian Barnes <tavianator@tavianator.com>2019-02-01 12:50:29 -0500
commitaa5f0f5745e4e7392c078d1fe28825c941f52f7c (patch)
tree897376cebf759a6e90c4b94f7b3dd5fe3a42025a /util.c
parent02132c2efb6daa213aa07b805ccb0c20241ca2f7 (diff)
downloadbfs-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.c10
1 files changed, 3 insertions, 7 deletions
diff --git a/util.c b/util.c
index b708527..1e5ebdb 100644
--- a/util.c
+++ b/util.c
@@ -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;