GNOME Bugzilla – Bug 117925
dir separator confusion in file utility functions under win32
Last modified: 2011-02-18 15:58:09 UTC
Under Win32 most file operator functions allow either use of backslash or forward slash as a directory separator E.g. to open a file either one of the following will work: FILE *F = fopen("c:/users/me/foo.txt"); or FILE *F = fopen("c:\\users\\me\\foo.txt"); It would therefore seem natural that the gfileutils and gutils functions of glib would support both of these. But that is unfortunately not the case. Thus the following does not work as expected under windows: gchar *path = "c:\\somewhere"; gchar *a_file = g_strdup_printf("%s/a_file", path); gchar *extracted_path = g_path_get_dirname(a_file); (It could be claimed that g_build_filename() should always be used, but if fopen, mkdir, etc support both slashes, then, imho, so should g_path_get_dirname() and g_path_get_basename().)
I tend to agree that glib should handle either slash in its file name manipulating functions on Win32. But is this a new feature or a bugfix? I.e. can it be done already in the 2.2 branch, or only in 2.4?
I'd consider this a 2.4-only thing. It's going to require quite a bit of code change. Especially something like g_build_filename() will require some thought. (What is g_build_filename ("C:\foo", "bar/baz", NULL))
Created attachment 19229 [details] [review] Suggested patch to gutils.c
Just having been bitten by this bug, I think we should after all consider it a thing for 2.2.3... At least the gutils.c part. Will do a patch for gfileutils.c later.
g_build_filename ("C:\\foo", "bar/baz", NULL) should be "C:\\foo\\bar/baz". (C syntax, i.e. backslashes appear doubled.) I have modified fileutils.c and added Win32-only tests that use '/', and mix '\\' and '/' to strfunc-test.c, but the GNOME CVS server says no such user tml in CVSROOT/passwd (some temporary problem, hopefully), so I can't cvs diff...
Created attachment 19257 [details] [review] Suggested patch to gfileutils.c and strfunc-test.c
Fix applied to glib-2-2, HEAD in a moment or two.