diff options
-rw-r--r-- | bfs.h | 7 | ||||
-rw-r--r-- | bftw.c | 4 | ||||
-rw-r--r-- | eval.c | 12 | ||||
-rw-r--r-- | parse.c | 1 | ||||
-rw-r--r-- | util.c | 18 | ||||
-rw-r--r-- | util.h | 30 |
6 files changed, 51 insertions, 21 deletions
@@ -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. */ @@ -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; @@ -27,18 +27,6 @@ #include <time.h> #include <unistd.h> -#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; @@ -11,6 +11,7 @@ #include "bfs.h" #include "typo.h" +#include "util.h" #include <ctype.h> #include <errno.h> #include <fcntl.h> @@ -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 +} @@ -14,6 +14,27 @@ #include <dirent.h> #include <stdbool.h> +#include <sys/stat.h> + +// 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 |