From 49e767ccb9dcfd2161b89f0d7a980eb85f056c5d Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sun, 4 Dec 2016 11:39:55 -0500 Subject: Move portability code into util.h --- bfs.h | 7 ------- bftw.c | 4 ++-- eval.c | 12 ------------ parse.c | 1 + util.c | 18 ++++++++++++++++++ util.h | 30 ++++++++++++++++++++++++++++++ 6 files changed, 51 insertions(+), 21 deletions(-) diff --git a/bfs.h b/bfs.h index f177340..c8b4255 100644 --- a/bfs.h +++ b/bfs.h @@ -27,13 +27,6 @@ # define BFS_HOMEPAGE "https://github.com/tavianator/bfs" #endif -// Some portability concerns -#if __APPLE__ -# define st_atim st_atimespec -# define st_ctim st_ctimespec -# define st_mtim st_mtimespec -#endif - /** * A command line expression. */ diff --git a/bftw.c b/bftw.c index 6205d79..614b418 100644 --- a/bftw.c +++ b/bftw.c @@ -359,10 +359,10 @@ static DIR *dircache_entry_open(struct dircache *cache, struct dircache_entry *e // footprint significantly, while keeping the fd around for future // openat() calls. - fd = fcntl(entry->fd, F_DUPFD_CLOEXEC, 0); + fd = dup_cloexec(entry->fd); if (fd < 0 && dircache_should_retry(cache, entry)) { - fd = fcntl(entry->fd, F_DUPFD_CLOEXEC, 0); + fd = dup_cloexec(entry->fd); } if (fd < 0) { return NULL; diff --git a/eval.c b/eval.c index d3b8f19..ebead3b 100644 --- a/eval.c +++ b/eval.c @@ -27,18 +27,6 @@ #include #include -#ifndef S_ISDOOR -# define S_ISDOOR(mode) false -#endif - -#ifndef S_ISPORT -# define S_ISPORT(mode) false -#endif - -#ifndef S_ISWHT -# define S_ISWHT(mode) false -#endif - struct eval_state { /** Data about the current file. */ struct BFTW *ftwbuf; diff --git a/parse.c b/parse.c index bf3a09a..2a78686 100644 --- a/parse.c +++ b/parse.c @@ -11,6 +11,7 @@ #include "bfs.h" #include "typo.h" +#include "util.h" #include #include #include diff --git a/util.c b/util.c index aef5fbb..bc611a8 100644 --- a/util.c +++ b/util.c @@ -58,3 +58,21 @@ int redirect(int fd, const char *path, int flags, ...) { return ret; } + +int dup_cloexec(int fd) { +#ifdef F_DUPFD_CLOEXEC + return fcntl(fd, F_DUPFD_CLOEXEC, 0); +#else + int ret = dup(fd); + if (ret < 0) { + return -1; + } + + if (fcntl(ret, F_SETFD, FD_CLOEXEC) == -1) { + close(ret); + return -1; + } + + return ret; +#endif +} diff --git a/util.h b/util.h index fbcadbd..e814f23 100644 --- a/util.h +++ b/util.h @@ -14,6 +14,27 @@ #include #include +#include + +// Some portability concerns + +#if __APPLE__ +# define st_atim st_atimespec +# define st_ctim st_ctimespec +# define st_mtim st_mtimespec +#endif + +#ifndef S_ISDOOR +# define S_ISDOOR(mode) false +#endif + +#ifndef S_ISPORT +# define S_ISPORT(mode) false +#endif + +#ifndef S_ISWHT +# define S_ISWHT(mode) false +#endif /** * readdir() wrapper that makes error handling cleaner. @@ -40,4 +61,13 @@ bool isopen(int fd); */ int redirect(int fd, const char *path, int flags, ...); +/** + * Like dup(), but set the FD_CLOEXEC flag. + * + * @param fd + * The file descriptor to duplicate. + * @return A duplicated file descriptor, or -1 on failure. + */ +int dup_cloexec(int fd); + #endif // BFS_UTIL_H -- cgit v1.2.3