]> Dogcows Code - chaz/tar/blobdiff - src/misc.c
Revamp tar_getcwd/normalize_filename stuff.
[chaz/tar] / src / misc.c
index bd396a307d2e37b0091262cd3e73092f54ef1899..5264952e95c0b3e8fc812b8f9b698878b2d5442b 100644 (file)
@@ -229,11 +229,12 @@ zap_slashes (char *name)
 }
 
 /* Normalize FILE_NAME by removing redundant slashes and "."
-   components, including redundant trailing slashes.  Leave ".."
-   alone, as it may be significant in the presence of symlinks and on
-   platforms where "/.." != "/".  Destructive version: modifies its
-   argument. */
-static void
+   components, including redundant trailing slashes.
+   Leave ".." alone, as it may be significant in the presence 
+   of symlinks and on platforms where "/.." != "/".
+
+   Destructive version: modifies its argument. */
+void
 normalize_filename_x (char *file_name)
 {
   char *name = file_name + FILE_SYSTEM_PREFIX_LEN (file_name);
@@ -267,11 +268,12 @@ normalize_filename_x (char *file_name)
 }
 
 /* Normalize NAME by removing redundant slashes and "." components,
-   including redundant trailing slashes.  Return a normalized
-   newly-allocated copy.  */
+   including redundant trailing slashes.
+
+   Return a normalized newly-allocated copy.  */
 
 char *
-normalize_filename (const char *name)
+normalize_filename (int cdidx, const char *name)
 {
   char *copy = NULL;
 
@@ -283,7 +285,7 @@ normalize_filename (const char *name)
          getcwd is slow, it might fail, and it does not necessarily
          return a canonical name even when it succeeds.  Perhaps we
          can use dev+ino pairs instead of names?  */
-      const char *cwd = tar_getcwd ();
+      const char *cwd = tar_getcdpath (cdidx);
       size_t copylen;
       bool need_separator;
       
@@ -886,7 +888,7 @@ chdir_arg (char const *dir)
       if (! wd_count)
        {
          wd[wd_count].name = ".";
-         wd[wd_count].cwd = NULL;
+         wd[wd_count].cwd = xgetcwd ();
          wd[wd_count].fd = AT_FDCWD;
          wd_count++;
        }
@@ -904,7 +906,14 @@ chdir_arg (char const *dir)
     }
 
   wd[wd_count].name = dir;
-  wd[wd_count].cwd = NULL;
+  if (IS_ABSOLUTE_FILE_NAME (wd[wd_count].name))
+    wd[wd_count].cwd = xstrdup (wd[wd_count].name);
+  else
+    {
+      namebuf_t nbuf = namebuf_create (wd[wd_count - 1].cwd);
+      namebuf_add_dir (nbuf, wd[wd_count].name);
+      wd[wd_count].cwd = namebuf_finish (nbuf);
+    }
   wd[wd_count].fd = 0;
   return wd_count++;
 }
@@ -979,35 +988,22 @@ chdir_do (int i)
 }
 \f
 const char *
-tar_getcwd (void)
+tar_dirname (void)
 {
-  static char *cwd;
-  namebuf_t nbuf;
-  int i;
+  return wd[chdir_current].name;
+}
 
-  if (!cwd)
-    cwd = xgetcwd ();
+const char *
+tar_getcdpath (int idx)
+{
   if (!wd)
-    return cwd;
-  
-  if (0 == chdir_current || !wd[chdir_current].cwd)
     {
-      if (IS_ABSOLUTE_FILE_NAME (wd[chdir_current].name))
-       return wd[chdir_current].name;
-      
-      if (!wd[0].cwd)
-       wd[0].cwd = cwd;
-
-      for (i = chdir_current - 1; i > 0; i--)
-       if (wd[i].cwd)
-         break;
-      
-      nbuf = namebuf_create (wd[i].cwd);
-      for (i++; i <= chdir_current; i++)
-       namebuf_add_dir (nbuf, wd[i].name);
-      wd[chdir_current].cwd = namebuf_finish (nbuf);
+      static char *cwd;
+      if (!cwd)
+       cwd = xgetcwd ();
+      return cwd;
     }
-  return wd[chdir_current].cwd;
+  return wd[idx].cwd;
 }
 \f
 void
This page took 0.025886 seconds and 4 git commands to generate.