From 58b4741d3d8eb50bd265e0aa5603923c1e3c04c4 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sun, 13 Nov 2016 15:08:33 -0500 Subject: Redirect stdin from /dev/null for -ok and -okdir. --- main.c | 36 ++++-------------------------------- 1 file changed, 4 insertions(+), 32 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index 03c5020..7471156 100644 --- a/main.c +++ b/main.c @@ -10,50 +10,22 @@ *********************************************************************/ #include "bfs.h" +#include "util.h" #include #include #include #include #include -/** - * Check if a file descriptor is open. - */ -static bool is_fd_open(int fd) { - return fcntl(fd, F_GETFD) >= 0 || errno != EBADF; -} - /** * Ensure that a file descriptor is open. */ static int ensure_fd_open(int fd, int flags) { - if (is_fd_open(fd)) { + if (isopen(fd)) { return 0; + } else { + return redirect(fd, "/dev/null", flags); } - - int devnull = open("/dev/null", flags); - if (devnull < 0) { - perror("open()"); - return -1; - } - - int ret = 0; - - if (devnull != fd) { - // Probably not reachable - - if (dup2(devnull, fd) < 0) { - perror("dup2()"); - ret = -1; - } - - if (close(devnull) != 0) { - perror("close()"); - ret = -1; - } - } - - return ret; } /** -- cgit v1.2.3