]> Dogcows Code - chaz/tar/blobdiff - lib/xgetcwd.c
Update
[chaz/tar] / lib / xgetcwd.c
index 95d0ab96cdf05ff0e608fdeb8eee34bdbe6734ee..23fbaeab1dd8b992d8ecd4c32afd65f7fe546c5f 100644 (file)
@@ -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. */
@@ -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
 }
This page took 0.021892 seconds and 4 git commands to generate.