summaryrefslogtreecommitdiff
path: root/lib/fdopendir.c
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2015-03-19 12:11:07 -0700
committerPaul Eggert <eggert@cs.ucla.edu>2015-03-19 12:13:27 -0700
commita277f536d9b33ebc24893d946fb8be14f23c9d00 (patch)
tree0a0d1338ae8b86b854aeda3fcbdb44a31b649c14 /lib/fdopendir.c
parentb89aa0edea32ca947722bb845b41606f171c9895 (diff)
downloadgnulib-a277f536d9b33ebc24893d946fb8be14f23c9d00.tar.gz
fdopendir: port better to MinGW
* lib/fdopendir.c (fd_clone_opendir) [REPLACE_FCHDIR]: Use 'dup' if dirfd fails. Suggested by Eli Zaretskii in: http://lists.gnu.org/archive/html/bug-gnulib/2015-03/msg00033.html * modules/fdopendir (Depends-on): Add dirfd.
Diffstat (limited to 'lib/fdopendir.c')
-rw-r--r--lib/fdopendir.c11
1 files changed, 10 insertions, 1 deletions
diff --git a/lib/fdopendir.c b/lib/fdopendir.c
index 117baa9993..837a8219b3 100644
--- a/lib/fdopendir.c
+++ b/lib/fdopendir.c
@@ -156,7 +156,16 @@ fd_clone_opendir (int fd, struct saved_cwd const *cwd)
if (! dir && EXPECTED_ERRNO (saved_errno))
{
char const *name = _gl_directory_name (fd);
- return (name ? opendir (name) : NULL);
+ DIR *dp = name ? opendir (name) : NULL;
+
+ /* The caller has done an elaborate dance to arrange for opendir to
+ consume just the right file descriptor. If dirfd returns -1,
+ though, we're on a system like mingw where opendir does not
+ consume a file descriptor. Consume it via 'dup' instead. */
+ if (dp && dirfd (dp) < 0)
+ dup (fd);
+
+ return dp;
}
# endif
errno = saved_errno;