no need for g_strdup_printf here
[mono.git] / mono / utils / mono-path.c
index 99e82ce71ae337ebee7efad7fead01fc6d7ec23d..591c5dc39c4f241b5c998b1ebd89ef24e05655fd 100644 (file)
@@ -86,12 +86,10 @@ mono_path_canonicalize (const char *path)
  * This ensures that the path that we store points to the final file
  * not a path to a symlink.
  */
-gchar *
-mono_path_resolve_symlinks (const char *path)
+#if !defined(PLATFORM_NO_SYMLINKS)
+static gchar *
+resolve_symlink (const char *path)
 {
-#if defined(PLATFORM_NO_SYMLINKS)
-       return mono_path_canonicalize (path);
-#else
        char *p, *concat, *dir;
        char buffer [PATH_MAX+1];
        int n, iterations = 0;
@@ -120,6 +118,39 @@ mono_path_resolve_symlinks (const char *path)
                g_free (concat);
        } while (iterations < MAXSYMLINKS);
 
+       return p;
+}
+#endif
+
+gchar *
+mono_path_resolve_symlinks (const char *path)
+{
+#if defined(PLATFORM_NO_SYMLINKS)
+       return mono_path_canonicalize (path);
+#else
+       gchar **split = g_strsplit (path, G_DIR_SEPARATOR_S, -1);
+       gchar *p = g_strdup ("");
+       int i;
+
+       for (i = 0; split [i] != NULL; i++) {
+               gchar *tmp = NULL;
+
+               // resolve_symlink of "" goes into canonicalize which resolves to cwd
+               if (strcmp (split [i], "") != 0) {
+                       tmp = g_strdup_printf ("%s%s", p, split [i]);
+                       g_free (p);
+                       p = resolve_symlink (tmp);
+                       g_free (tmp);
+               }
+
+               if (split [i+1] != NULL) {
+                       tmp = g_strdup_printf ("%s%s", p, G_DIR_SEPARATOR_S);
+                       g_free (p);
+                       p = tmp;
+               }
+       }
+
+       g_free (split);
        return p;
 #endif
 }