Fix the quiet build code so it works with a separate build dir too.
[mono.git] / eglib / src / gpath.c
index 9e8c03d1cb8b62e92fce1a3632d5c1c38c3d4fc8..4ae2f8f5e9e8863f5e7fe704e3275dd8dd084be8 100644 (file)
 gchar *
 g_build_path (const gchar *separator, const gchar *first_element, ...)
 {
-       GString *result;
-       const char *s, *p, *next;
-       size_t slen;
+       const char *elem, *next, *endptr;
+       gboolean trimmed;
+       GString *path;
        va_list args;
-       gboolean found;
-#ifdef G_OS_WIN32
-       const gchar alt_separator = '/';
-#endif
+       size_t slen;
        
        g_return_val_if_fail (separator != NULL, NULL);
-
-       if (first_element == NULL)
-               return g_strdup ("");
-
-       result = g_string_sized_new (48);
-
+       
+       path = g_string_sized_new (48);
        slen = strlen (separator);
        
        va_start (args, first_element);
-       for (s = first_element; s != NULL; s = next){
-               found = FALSE;
-               next = va_arg (args, char *);
-               p = (s + strlen (s));
-
-               /* Strip all but one trailing separator */
-               if (next && p - slen >= s){
-                       for (; strncmp (p-slen, separator, slen) == 0
-#ifdef G_OS_WIN32
-                            || alt_separator == *(p-slen)
-#endif
-                       ;){
-                               found = TRUE;
-                               p -= slen;
-                       }
+       for (elem = first_element; elem != NULL; elem = next) {
+               /* trim any trailing separators from @elem */
+               endptr = elem + strlen (elem);
+               trimmed = FALSE;
+               
+               while (endptr >= elem + slen) {
+                       if (strncmp (endptr - slen, separator, slen) != 0)
+                               break;
+                       
+                       endptr -= slen;
+                       trimmed = TRUE;
                }
-               if (found) p += slen;
-
-               /* Append path token */
-               g_string_append_len (result, s, p - s);
-
-               if (next && *next){
-                       /* Append separator if needed */
-                       if (!found)
-                               g_string_append (result, separator);
-
-                       /* Strip multiple contiguous separators */
-                       for (; strncmp (next, separator, slen) == 0 
-#ifdef G_OS_WIN32
-                       || alt_separator == *next
-#endif
-                       ; )
+               
+               /* append elem, not including any trailing separators */
+               if (endptr > elem)
+                       g_string_append_len (path, elem, endptr - elem);
+               
+               /* get the next element */
+               do {
+                       if (!(next = va_arg (args, char *)))
+                               break;
+                       
+                       /* remove leading separators */
+                       while (!strncmp (next, separator, slen))
                                next += slen;
-               }
+               } while (*next == '\0');
+               
+               if (next || trimmed)
+                       g_string_append_len (path, separator, slen);
        }
-       /* Nul-terminate */
-       g_string_append_c (result, 0);
        va_end (args);
-
-       return g_string_free (result, FALSE);
+       
+       return g_string_free (path, FALSE);
 }
 
 gchar *
@@ -215,7 +201,7 @@ g_find_program_in_path (const gchar *program)
        char *p = g_strdup (g_getenv ("PATH"));
        char *x = p, *l;
        gchar *curdir = NULL;
-       char *save;
+       char *save = NULL;
 #ifdef G_OS_WIN32
        char *program_exe;
        char *suffix_list[5] = {".exe",".cmd",".bat",".com",NULL};