diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2020-10-01 10:09:05 -0400 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2020-10-01 10:09:05 -0400 |
commit | 80ac731c907c04f60148a696162cb95d7cedc90a (patch) | |
tree | 7fe33a8ec3ec7d27db1beffa393cedc2a21d8e6a /main.c | |
parent | 04445b6bff02da758a87a48c19ee4963aba62f15 (diff) | |
download | bfs-80ac731c907c04f60148a696162cb95d7cedc90a.tar.xz |
util: Move redirect() and isopen() to main.c
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 22 |
1 files changed, 22 insertions, 0 deletions
@@ -61,6 +61,28 @@ #include <unistd.h> /** + * Check if a file descriptor is open. + */ +static bool isopen(int fd) { + return fcntl(fd, F_GETFD) >= 0 || errno != EBADF; +} + +/** + * Open a file and redirect it to a particular descriptor. + */ +static int redirect(int fd, const char *path, int flags) { + int ret = open(path, flags); + + if (ret >= 0 && ret != fd) { + int orig = ret; + ret = dup2(orig, fd); + close(orig); + } + + return ret; +} + +/** * Make sure the standard streams std{in,out,err} are open. If they are not, * future open() calls may use those file descriptors, and std{in,out,err} will * use them unintentionally. |