summaryrefslogtreecommitdiff
path: root/lib/xgetcwd.c
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2004-11-17 03:41:16 +0000
committerPaul Eggert <eggert@cs.ucla.edu>2004-11-17 03:41:16 +0000
commit2b88597a04aefb8275f44a2091bcd2285afc665d (patch)
tree23cfeea08b9e53eeb9a657fa6e234f565543aedf /lib/xgetcwd.c
parent6ad1c2cfd708beeb37b7c2fbf9bbb866c26aa2a9 (diff)
downloadcoreutils-2b88597a04aefb8275f44a2091bcd2285afc665d.tar.xz
Include <limits.h>, for PATH_MAX.
(xgetcwd): Set errno correctly when failing. Work around Solaris 9 bug: getcwd sets errno==ERANGE even though the failure is actually due to a PATH_MAX problem.
Diffstat (limited to 'lib/xgetcwd.c')
-rw-r--r--lib/xgetcwd.c20
1 files changed, 18 insertions, 2 deletions
diff --git a/lib/xgetcwd.c b/lib/xgetcwd.c
index 785002e0e..f08580357 100644
--- a/lib/xgetcwd.c
+++ b/lib/xgetcwd.c
@@ -23,6 +23,7 @@
# include <config.h>
#endif
+#include <limits.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
@@ -60,6 +61,8 @@ xgetcwd (void)
return cwd;
#else
+ int saved_errno;
+
/* The initial buffer size for the working directory. A power of 2
detects arithmetic overflow earlier, but is not required. */
# ifndef INITIAL_BUFFER_SIZE
@@ -72,16 +75,29 @@ xgetcwd (void)
{
char *buf = xmalloc (buf_size);
char *cwd = getcwd (buf, buf_size);
- int saved_errno;
if (cwd)
return cwd;
saved_errno = errno;
free (buf);
if (saved_errno != ERANGE)
- return NULL;
+ break;
+
+#ifdef PATH_MAX
+ if (PATH_MAX / 2 < buf_size)
+ {
+ if (PATH_MAX <= buf_size)
+ break;
+ buf_size = PATH_MAX;
+ continue;
+ }
+#endif
+
buf_size *= 2;
if (buf_size == 0)
xalloc_die ();
}
+
+ errno = saved_errno;
+ return NULL;
#endif
}