summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>2001-08-31 11:39:16 +0000
committerJim Meyering <jim@meyering.net>2001-08-31 11:39:16 +0000
commit72ee797c4477d0e09efbba7c3bcc1d9d621be8f7 (patch)
treefe48b7fc157cf46d27ec1ffafbc3a91602be37d8 /lib
parent73caa616836e114e7ba1efcbaa2271fe6cfeb8b5 (diff)
downloadcoreutils-72ee797c4477d0e09efbba7c3bcc1d9d621be8f7.tar.xz
(xgetcwd): Reorganize to avoid some duplication.
Use an initial, malloc'd, buffer of length 128 rather than a statically allocated one of length 1024.
Diffstat (limited to 'lib')
-rw-r--r--lib/xgetcwd.c37
1 files changed, 12 insertions, 25 deletions
diff --git a/lib/xgetcwd.c b/lib/xgetcwd.c
index 0af66fb88..55e12afcd 100644
--- a/lib/xgetcwd.c
+++ b/lib/xgetcwd.c
@@ -54,38 +54,25 @@ xgetcwd ()
#if defined __GLIBC__ && __GLIBC__ >= 2
return getcwd (NULL, 0);
#else
- char *ret;
- size_t path_max;
- char buf[1024];
+ size_t buf_size = 128; /* must be a power of 2 */
+ char *buf = NULL;
- errno = 0;
- ret = getcwd (buf, sizeof (buf));
- if (ret != NULL)
- return xstrdup (buf);
- if (errno != ERANGE)
- return NULL;
-
- path_max = 1 << 10;
-
- for (;;)
+ while (1)
{
- char *cwd = (char *) xmalloc (path_max);
- int save_errno;
+ char *ret;
+ buf = (char *) xrealloc (buf, buf_size);
- errno = 0;
- ret = getcwd (cwd, path_max);
- if (ret != NULL)
- return ret;
- save_errno = errno;
- free (cwd);
- if (save_errno != ERANGE)
+ cwd = getcwd (buf, buf_size);
+ if (cwd != NULL)
+ return cwd;
+ if (errno != ERANGE)
{
- errno = save_errno;
+ free (buf);
return NULL;
}
- path_max *= 2;
- if (path_max == 0)
+ buf_size *= 2;
+ if (buf_size == 0)
xalloc_die ();
}
#endif