X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=lib%2Fxgetcwd.c;h=23fbaeab1dd8b992d8ecd4c32afd65f7fe546c5f;hb=1ffb4377a444fb7150030acfe843bc20cfba5d5a;hp=95d0ab96cdf05ff0e608fdeb8eee34bdbe6734ee;hpb=f17ce7b6aea9a983d18da31462a55192af95adad;p=chaz%2Ftar diff --git a/lib/xgetcwd.c b/lib/xgetcwd.c index 95d0ab9..23fbaea 100644 --- a/lib/xgetcwd.c +++ b/lib/xgetcwd.c @@ -28,7 +28,13 @@ extern int errno; #endif #include -#include "pathmax.h" + +#if HAVE_STDLIB_H +# include +#endif +#if HAVE_UNISTD_H +# include +#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. */ @@ -50,40 +54,26 @@ xgetcwd () #if defined __GLIBC__ && __GLIBC__ >= 2 return getcwd (NULL, 0); #else - char *ret; - unsigned 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 = 1300; - path_max += 2; /* The getcwd docs say to do this. */ - - for (;;) + while (1) { - char *cwd = (char *) xmalloc (path_max); + char *cwd; + buf = (char *) xrealloc (buf, buf_size); - errno = 0; - ret = getcwd (cwd, path_max); - if (ret != NULL) - return ret; + cwd = getcwd (buf, buf_size); + if (cwd != NULL) + return cwd; if (errno != ERANGE) { - int save_errno = errno; - free (cwd); - errno = save_errno; + free (buf); return NULL; } - free (cwd); - - path_max += path_max / 16; - path_max += 32; + buf_size *= 2; + if (buf_size == 0) + xalloc_die (); } #endif }