From 80ac731c907c04f60148a696162cb95d7cedc90a Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 1 Oct 2020 10:09:05 -0400 Subject: util: Move redirect() and isopen() to main.c --- main.c | 22 ++++++++++++++++++++++ util.c | 28 ---------------------------- util.h | 20 -------------------- 3 files changed, 22 insertions(+), 48 deletions(-) diff --git a/main.c b/main.c index 70f4d8b..f77d787 100644 --- a/main.c +++ b/main.c @@ -60,6 +60,28 @@ #include #include +/** + * 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 diff --git a/util.c b/util.c index b66dd18..468d664 100644 --- a/util.c +++ b/util.c @@ -92,34 +92,6 @@ error: return NULL; } -bool isopen(int fd) { - return fcntl(fd, F_GETFD) >= 0 || errno != EBADF; -} - -int redirect(int fd, const char *path, int flags, ...) { - mode_t mode = 0; - if (flags & O_CREAT) { - va_list args; - va_start(args, flags); - - // Use int rather than mode_t, because va_arg must receive a - // fully-promoted type - mode = va_arg(args, int); - - va_end(args); - } - - int ret = open(path, flags, mode); - - if (ret >= 0 && ret != fd) { - int orig = ret; - ret = dup2(orig, fd); - close(orig); - } - - return ret; -} - int dup_cloexec(int fd) { #ifdef F_DUPFD_CLOEXEC return fcntl(fd, F_DUPFD_CLOEXEC, 0); diff --git a/util.h b/util.h index b10e0e4..a9bdecf 100644 --- a/util.h +++ b/util.h @@ -109,26 +109,6 @@ int xreaddir(DIR *dir, struct dirent **de); */ char *xreadlinkat(int fd, const char *path, size_t size); -/** - * Check if a file descriptor is open. - */ -bool isopen(int fd); - -/** - * Open a file and redirect it to a particular descriptor. - * - * @param fd - * The file descriptor to redirect. - * @param path - * The path to open. - * @param flags - * The flags passed to open(). - * @param mode - * The mode passed to open() (optional). - * @return fd on success, -1 on failure. - */ -int redirect(int fd, const char *path, int flags, ...); - /** * Like dup(), but set the FD_CLOEXEC flag. * -- cgit v1.2.3