diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2016-11-13 15:08:33 -0500 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2016-11-13 15:44:48 -0500 |
commit | 58b4741d3d8eb50bd265e0aa5603923c1e3c04c4 (patch) | |
tree | 8ac8a78995df61d92359e33f5fcbc9a8299e9546 /main.c | |
parent | 1c5168342e5248983d3a7929abaebe16ca3640a3 (diff) | |
download | bfs-58b4741d3d8eb50bd265e0aa5603923c1e3c04c4.tar.xz |
Redirect stdin from /dev/null for -ok and -okdir.
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 36 |
1 files changed, 4 insertions, 32 deletions
@@ -10,6 +10,7 @@ *********************************************************************/ #include "bfs.h" +#include "util.h" #include <errno.h> #include <fcntl.h> #include <stdio.h> @@ -17,43 +18,14 @@ #include <unistd.h> /** - * 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; } /** |