X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Fgetcwd.c;h=f8567bd7392563d00a132d925f9354a3145570e5;hb=44d6eead9ba864662836a9f9d95913b4e0cb2a3b;hp=f13da4dcc69feda0ad56c88cd5651a3c7fcd6b25;hpb=854ebf64dc7dae95a43a4e139e075156d2add805;p=gnulib.git diff --git a/lib/getcwd.c b/lib/getcwd.c index f13da4dcc..f8567bd73 100644 --- a/lib/getcwd.c +++ b/lib/getcwd.c @@ -1,4 +1,4 @@ -/* Copyright (C) 1991,92,93,94,95,96,97,98,99,2004,2005,2006 Free Software +/* Copyright (C) 1991,92,93,94,95,96,97,98,99,2004,2005,2006,2007 Free Software Foundation, Inc. This file is part of the GNU C Library. @@ -18,7 +18,8 @@ #if !_LIBC # include -# include "getcwd.h" +# include +# include "dirfd.h" #endif #include @@ -49,8 +50,6 @@ # ifndef mempcpy # define mempcpy __mempcpy # endif -#else -# include "mempcpy.h" #endif #include @@ -142,13 +141,18 @@ __getcwd (char *buf, size_t size) size_t allocated = size; size_t used; -#if HAVE_PARTLY_WORKING_GETCWD && !defined AT_FDCWD +#if HAVE_PARTLY_WORKING_GETCWD /* The system getcwd works, except it sometimes fails when it shouldn't, setting errno to ERANGE, ENAMETOOLONG, or ENOENT. If AT_FDCWD is not defined, the algorithm below is O(N**2) and this is much slower than the system getcwd (at least on GNU/Linux). So trust the system getcwd's results unless they look - suspicious. */ + suspicious. + + Use the system getcwd even if we have openat support, since the + system getcwd works even when a parent is unreadable, while the + openat-based approach does not. */ + # undef getcwd dir = getcwd (buf, size); if (dir || (errno != ERANGE && !is_ENAMETOOLONG (errno) && errno != ENOENT)) @@ -231,6 +235,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);