]> git.wh0rd.org - fontconfig.git/blobdiff - src/fcstr.c
Eliminate ./ and ../ elements from font directory names when scanning.
[fontconfig.git] / src / fcstr.c
index 5d1961c3af47ee0f74857d6ed7df4e9cea976cfc..37bad6b16379f4cd5cf7128d3b163d5eae5d54e6 100644 (file)
@@ -835,6 +835,63 @@ FcStrBasename (const FcChar8 *file)
     return FcStrCopy (slash + 1);
 }
 
+FcChar8 *
+FcStrCanonFilename (const FcChar8 *s)
+{
+    FcChar8 *file;
+    FcChar8 *f;
+    const FcChar8 *slash;
+    
+    if (*s != '/')
+    {
+       FcChar8 *full;
+       
+       FcChar8 cwd[FC_MAX_FILE_LEN + 2];
+       if (getcwd ((char *) cwd, FC_MAX_FILE_LEN) == NULL)
+           return NULL;
+       strcat ((char *) cwd, "/");
+       full = FcStrPlus (cwd, s);
+       file = FcStrCanonFilename (full);
+       FcStrFree (full);
+       return file;
+    }
+    file = malloc (strlen ((char *) s) + 1);
+    if (!file)
+       return NULL;
+    slash = NULL;
+    f = file;
+    for (;;) {
+       if (*s == '/' || *s == '\0')
+       {
+           if (slash)
+           {
+               switch (s - slash) {
+               case 2:
+                   if (!strncmp ((char *) slash, "/.", 2))
+                   {
+                       f -= 2; /* trim /. from file */
+                   }
+                   break;
+               case 3:
+                   if (!strncmp ((char *) slash, "/..", 3))
+                   {
+                       f -= 3; /* trim /.. from file */
+                       while (f > file) {
+                           if (*--f == '/')
+                               break;
+                       }
+                   }
+                   break;
+               }
+           }
+           slash = s;
+       }
+       if (!(*f++ = *s++))
+           break;
+    }
+    return file;
+}
+
 FcStrSet *
 FcStrSetCreate (void)
 {