From: Paul Eggert Date: Thu, 19 Mar 2015 19:11:07 +0000 (-0700) Subject: fdopendir: port better to MinGW X-Git-Tag: v1.0~7114 X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=a277f536d9b33ebc24893d946fb8be14f23c9d00;p=gnulib.git 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. --- diff --git a/ChangeLog b/ChangeLog index 18d90e2d7e..709dfffe09 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2015-03-19 Paul Eggert + + 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 fdopendir: fix typo in comment 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; diff --git a/modules/fdopendir b/modules/fdopendir index ee9a875396..2d03867ecc 100644 --- a/modules/fdopendir +++ b/modules/fdopendir @@ -9,6 +9,7 @@ Depends-on: 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]