From f61457b0fee7326ead5b2cd8df8515a814d922e0 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Tue, 31 Mar 2020 01:59:33 -0700 Subject: [PATCH] fileio: add take_fdopendir() variant fdopendir() wrapper analogous to take_fdopen() --- src/basic/fileio.c | 12 ++++++++++++ src/basic/fileio.h | 1 + 2 files changed, 13 insertions(+) diff --git a/src/basic/fileio.c b/src/basic/fileio.c index c82a9d2c01d..4c365ad6fa9 100644 --- a/src/basic/fileio.c +++ b/src/basic/fileio.c @@ -80,6 +80,18 @@ FILE* take_fdopen(int *fd, const char *options) { return f; } +DIR* take_fdopendir(int *dfd) { + assert(dfd); + + DIR *d = fdopendir(*dfd); + if (!d) + return NULL; + + *dfd = -1; + + return d; +} + FILE* open_memstream_unlocked(char **ptr, size_t *sizeloc) { FILE *f = open_memstream(ptr, sizeloc); if (!f) diff --git a/src/basic/fileio.h b/src/basic/fileio.h index 525f6ac814a..58daabaa8ff 100644 --- a/src/basic/fileio.h +++ b/src/basic/fileio.h @@ -41,6 +41,7 @@ int fopen_unlocked(const char *path, const char *options, FILE **ret); int fdopen_unlocked(int fd, const char *options, FILE **ret); int take_fdopen_unlocked(int *fd, const char *options, FILE **ret); FILE* take_fdopen(int *fd, const char *options); +DIR* take_fdopendir(int *dfd); FILE* open_memstream_unlocked(char **ptr, size_t *sizeloc); FILE* fmemopen_unlocked(void *buf, size_t size, const char *mode);