(PSA) GLib can now canonicalize file paths

Quick announcement: if you have a relative file path, and want to resolve it against another (absolute) path, GLib can do that now.

An example in C:

g_autofree gchar *path = NULL;
g_autofree gchar *another_path = NULL;

path = g_canonicalize_filename ("../../usr/bin", "/etc/foo");
another_path = g_canonicalize_filename ("../../usr/bin", NULL);

g_print ("%s \n", path); /* Result: "/usr/bin" */
g_print ("%s \n", another_path); /* Passing NULL uses the current working dir */

No I/O is involved. It handles double and triple slashes nicely too.

This is bug 111848. The bug was fixed exactly 15 years and 1 day after initially reported.

We wish you farewell, bug 111848.

(Thanks Philip Withnall for reviewing, and Endless for sponsoring this work. This bug was fixed entirely in my work time.)


One response to “(PSA) GLib can now canonicalize file paths”

  1. Morten Welinder Avatar
    Morten Welinder

    Nice. For a while I feared it was going to be discussed to death.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.