X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=gnu%2Ffdopendir.c;h=837a8219b3399f7431c70169e0694ee7cb523f1d;hb=d30babc23b4f25be970ada2e63a50220a3672281;hp=cd08665ae777a45867c1e8a1df4c58414ffa7ebb;hpb=4aa85f09e755fc827cd5ab6225f20c83cd42245d;p=debian%2Ftar diff --git a/gnu/fdopendir.c b/gnu/fdopendir.c index cd08665a..837a8219 100644 --- a/gnu/fdopendir.c +++ b/gnu/fdopendir.c @@ -1,7 +1,5 @@ -/* -*- buffer-read-only: t -*- vi: set ro: */ -/* DO NOT EDIT! GENERATED AUTOMATICALLY! */ /* provide a replacement fdopendir function - Copyright (C) 2004-2014 Free Software Foundation, Inc. + Copyright (C) 2004-2015 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -95,7 +93,7 @@ fdopendir (int fd) That way, barring race conditions, fd_clone_opendir returns a stream whose file descriptor is FD. - If REPLACE_CHDIR or CWD is null, use opendir ("/proc/self/fd/...", + If REPLACE_FCHDIR or CWD is null, use opendir ("/proc/self/fd/...", falling back on fchdir metadata. Otherwise, CWD is a saved version of the working directory; use fchdir/opendir(".")/restore_cwd(CWD). */ static DIR * @@ -158,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;