changeset 17944:84bef418f1a1

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.
author Paul Eggert <eggert@cs.ucla.edu>
date Thu, 19 Mar 2015 12:11:07 -0700
parents 66691469e6b9
children 00c7e00808f7
files ChangeLog lib/fdopendir.c modules/fdopendir
diffstat 3 files changed, 19 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2015-03-19  Paul Eggert  <eggert@cs.ucla.edu>
+
+	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.
+
 2015-03-18  Paul Eggert  <eggert@cs.ucla.edu>
 
 	fdopendir: fix typo in comment
--- a/lib/fdopendir.c
+++ b/lib/fdopendir.c
@@ -156,7 +156,16 @@
       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;
--- a/modules/fdopendir
+++ b/modules/fdopendir
@@ -9,6 +9,7 @@
 dirent
 extensions
 at-internal     [test $HAVE_FDOPENDIR = 0]
+dirfd           [test $HAVE_FDOPENDIR = 0]
 dup             [test $HAVE_FDOPENDIR = 0]
 errno           [test $HAVE_FDOPENDIR = 0]
 fchdir          [test $HAVE_FDOPENDIR = 0]