(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.)

Advertisements