changeset 8216:a45de5ccf3a6

* lib/getcwd.c (__getcwd): Don't assume getcwd (NULL, 0) works like glibc; on Solaris 10, it fails with errno == EINVAL. POSIX says the behavior is unspecified if the first argument is NULL, so play it safe and never pass NULL to the system getcwd.
author Paul Eggert <eggert@cs.ucla.edu>
date Wed, 21 Feb 2007 08:36:35 +0000
parents c3429d032540
children f43bac5db7d2
files ChangeLog lib/getcwd.c
diffstat 2 files changed, 31 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Wed Feb 21 07:57:33 2007 +0000
+++ b/ChangeLog	Wed Feb 21 08:36:35 2007 +0000
@@ -1,3 +1,10 @@
+2007-02-21  Paul Eggert  <eggert@cs.ucla.edu>
+
+	* lib/getcwd.c (__getcwd): Don't assume getcwd (NULL, 0) works
+	like glibc; on Solaris 10, it fails with errno == EINVAL.
+	POSIX says the behavior is unspecified if the first argument is NULL,
+	so play it safe and never pass NULL to the system getcwd.
+
 2007-02-21  Jim Meyering  <jim@meyering.net>
 
 	* lib/gettimeofday.c (rpl_gettimeofday): Remove declaration
--- a/lib/getcwd.c	Wed Feb 21 07:57:33 2007 +0000
+++ b/lib/getcwd.c	Wed Feb 21 08:36:35 2007 +0000
@@ -141,24 +141,6 @@
   size_t allocated = size;
   size_t used;
 
-#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.
-
-     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))
-    return dir;
-#endif
-
   if (size == 0)
     {
       if (buf != NULL)
@@ -179,6 +161,30 @@
   else
     dir = buf;
 
+#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.
+
+     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
+  if (getcwd (dir, allocated))
+    {
+      if (buf == NULL && size == 0)
+	buf = realloc (dir, strlen (dir) + 1);
+      return (buf ? buf : dir);
+    }
+
+  if (! (errno == ERANGE || is_ENAMETOOLONG (errno) || errno == ENOENT))
+    return NULL;
+#endif
+
   dirp = dir + allocated;
   *--dirp = '\0';