summaryrefslogtreecommitdiff
path: root/lib/xgetcwd.c
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>2001-08-31 10:36:12 +0000
committerJim Meyering <jim@meyering.net>2001-08-31 10:36:12 +0000
commitee65e66c6cc7833fccec6f2a0d18ea88d5456e01 (patch)
tree7f733509f7c91f37312290e7f9940566a0fced13 /lib/xgetcwd.c
parent48f3734b371b4cd335666ea910cc5a40178a188c (diff)
downloadcoreutils-ee65e66c6cc7833fccec6f2a0d18ea88d5456e01.tar.xz
Don't include pathmax.h.
Include stdlib.h and unistd.h if available. Include xalloc.h. (xmalloc, xstrdup, free): Remove decls. (xgetcwd): Don't assume sizes fit in unsigned. Check for overflow when computing sizes. Simplify reallocation code.
Diffstat (limited to 'lib/xgetcwd.c')
-rw-r--r--lib/xgetcwd.c31
1 files changed, 17 insertions, 14 deletions
diff --git a/lib/xgetcwd.c b/lib/xgetcwd.c
index 95d0ab96c..0af66fb88 100644
--- a/lib/xgetcwd.c
+++ b/lib/xgetcwd.c
@@ -28,7 +28,13 @@ extern int errno;
#endif
#include <sys/types.h>
-#include "pathmax.h"
+
+#if HAVE_STDLIB_H
+# include <stdlib.h>
+#endif
+#if HAVE_UNISTD_H
+# include <unistd.h>
+#endif
#if HAVE_GETCWD
char *getcwd ();
@@ -37,9 +43,7 @@ char *getwd ();
# define getcwd(Buf, Max) getwd (Buf)
#endif
-extern void *xmalloc ();
-extern char *xstrdup ();
-extern void free ();
+#include "xalloc.h"
/* Return the current directory, newly allocated, arbitrarily long.
Return NULL and set errno on error. */
@@ -51,7 +55,7 @@ xgetcwd ()
return getcwd (NULL, 0);
#else
char *ret;
- unsigned path_max;
+ size_t path_max;
char buf[1024];
errno = 0;
@@ -61,29 +65,28 @@ xgetcwd ()
if (errno != ERANGE)
return NULL;
- path_max = 1300;
- path_max += 2; /* The getcwd docs say to do this. */
+ path_max = 1 << 10;
for (;;)
{
char *cwd = (char *) xmalloc (path_max);
+ int save_errno;
errno = 0;
ret = getcwd (cwd, path_max);
if (ret != NULL)
return ret;
- if (errno != ERANGE)
+ save_errno = errno;
+ free (cwd);
+ if (save_errno != ERANGE)
{
- int save_errno = errno;
- free (cwd);
errno = save_errno;
return NULL;
}
- free (cwd);
-
- path_max += path_max / 16;
- path_max += 32;
+ path_max *= 2;
+ if (path_max == 0)
+ xalloc_die ();
}
#endif
}