NaCl runtime fixes
[mono.git] / mono / utils / mono-path.c
index a5d88bd58cfcaaa75247b6123aae9107c8238c03..0a443aa41f76c1a977ac108b5e0a4a635651ae6e 100644 (file)
 
 /* Resolves '..' and '.' references in a path. If the path provided is relative,
  * it will be relative to the current directory */
+
+/* For Native Client, the above is not true.  Since there is no getcwd we fill */
+/* in the file being passed in relative to '.' and don't resolve it            */
+
+/* There are a couple of tests for this method in mono/test/mono-path.cs */
 gchar *
 mono_path_canonicalize (const char *path)
 {
@@ -44,7 +49,7 @@ mono_path_canonicalize (const char *path)
                g_free (tmpdir);
        }
 
-#ifdef PLATFORM_WIN32
+#ifdef HOST_WIN32
        g_strdelimit (abspath, "/", '\\');
 #endif
        abspath = g_strreverse (abspath);
@@ -73,13 +78,29 @@ mono_path_canonicalize (const char *path)
                pos = strchr (lastpos, G_DIR_SEPARATOR);
        }
 
-#ifdef PLATFORM_WIN32 /* For UNC paths the first '\' is removed. */
-       if (*(lastpos-1) == G_DIR_SEPARATOR && *(lastpos-2) == G_DIR_SEPARATOR)
+#ifdef HOST_WIN32
+       /* Avoid removing the first '\' for UNC paths. We must make sure that it's indeed an UNC path
+       by checking if the \\ pair happens exactly at the end of the string.
+       */
+       if (*(lastpos-1) == G_DIR_SEPARATOR && *(lastpos-2) == G_DIR_SEPARATOR && *lastpos == 0)
                lastpos = lastpos-1;
 #endif
        
        if (dest != lastpos) strcpy (dest, lastpos);
-       return g_strreverse (abspath);
+       
+       g_strreverse (abspath);
+
+       /* We strip away all trailing dir separators. This is not correct for the root directory,
+        * since we'll return an empty string, so re-append a dir separator if there is none in the
+        * result */
+       if (strchr (abspath, G_DIR_SEPARATOR) == NULL) {
+               int len = strlen (abspath);
+               abspath = g_realloc (abspath, len + 2);
+               abspath [len] = G_DIR_SEPARATOR;
+               abspath [len+1] = 0;
+       }
+
+       return abspath;
 }
 
 /*
@@ -129,7 +150,7 @@ mono_path_resolve_symlinks (const char *path)
        return mono_path_canonicalize (path);
 #else
        gchar **split = g_strsplit (path, G_DIR_SEPARATOR_S, -1);
-       gchar *p = g_strdup_printf ("");
+       gchar *p = g_strdup ("");
        int i;
 
        for (i = 0; split [i] != NULL; i++) {
@@ -150,7 +171,7 @@ mono_path_resolve_symlinks (const char *path)
                }
        }
 
-       g_free (split);
+       g_strfreev (split);
        return p;
 #endif
 }