I don't know of a system on which this bug can be exercised.
I found it only by manually building getcwd.o, even though
this system already has a mostly-working one.
The symptom is that getcwd would always fail with ENOENT.
Don't use FD after a successful "fdopendir (fd)".
* lib/getcwd.c (__getcwd) [AT_FDCWD]: fdopendir (fd) usually closes fd.
Reset it by calling dirfd on the just-obtained DIR*.
Index: lib/getcwd.c
===================================================================
RCS file: /sources/gnulib/gnulib/lib/getcwd.c,v
retrieving revision 1.19
diff -u -p -r1.19 getcwd.c
--- lib/getcwd.c 19 Feb 2007 02:24:42 -0000 1.19
+++ lib/getcwd.c 19 Feb 2007 20:03:17 -0000
@@ -234,6 +234,8 @@ __getcwd (char *buf, size_t size)
dirstream = fdopendir (fd);
if (dirstream == NULL)
goto lose;
+ /* Reset fd. It may have been closed by fdopendir. */
+ fd = dirfd (dirstream);
fd_needs_closing = false;
#else
dirstream = __opendir (dotlist);