From 71e0f9374743a66d77430c1a46f0b06c58942f79 Mon Sep 17 00:00:00 2001 From: Tor Lillqvist Date: Fri, 5 Jan 2001 20:19:37 +0000 Subject: [PATCH] Add test for g_path_skip_root(). 2001-01-05 Tor Lillqvist * testglib.c (main): Add test for g_path_skip_root(). * gutils.c (g_path_skip_root): Fix bugs in code I just added ;-) --- ChangeLog | 2 ++ ChangeLog.pre-2-0 | 2 ++ ChangeLog.pre-2-10 | 2 ++ ChangeLog.pre-2-12 | 2 ++ ChangeLog.pre-2-2 | 2 ++ ChangeLog.pre-2-4 | 2 ++ ChangeLog.pre-2-6 | 2 ++ ChangeLog.pre-2-8 | 2 ++ glib/gutils.c | 24 +++++++++++++++++++---- gutils.c | 24 +++++++++++++++++++---- testglib.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++------- tests/testglib.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++------- 12 files changed, 156 insertions(+), 22 deletions(-) diff --git a/ChangeLog b/ChangeLog index e5ca5dd..10269d5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,7 @@ 2001-01-05 Tor Lillqvist + * testglib.c (main): Add test for g_path_skip_root(). + * gfileutils.c (g_file_open_tmp): (Win32:) Look also for (illegal) forward slashes in the template. diff --git a/ChangeLog.pre-2-0 b/ChangeLog.pre-2-0 index e5ca5dd..10269d5 100644 --- a/ChangeLog.pre-2-0 +++ b/ChangeLog.pre-2-0 @@ -1,5 +1,7 @@ 2001-01-05 Tor Lillqvist + * testglib.c (main): Add test for g_path_skip_root(). + * gfileutils.c (g_file_open_tmp): (Win32:) Look also for (illegal) forward slashes in the template. diff --git a/ChangeLog.pre-2-10 b/ChangeLog.pre-2-10 index e5ca5dd..10269d5 100644 --- a/ChangeLog.pre-2-10 +++ b/ChangeLog.pre-2-10 @@ -1,5 +1,7 @@ 2001-01-05 Tor Lillqvist + * testglib.c (main): Add test for g_path_skip_root(). + * gfileutils.c (g_file_open_tmp): (Win32:) Look also for (illegal) forward slashes in the template. diff --git a/ChangeLog.pre-2-12 b/ChangeLog.pre-2-12 index e5ca5dd..10269d5 100644 --- a/ChangeLog.pre-2-12 +++ b/ChangeLog.pre-2-12 @@ -1,5 +1,7 @@ 2001-01-05 Tor Lillqvist + * testglib.c (main): Add test for g_path_skip_root(). + * gfileutils.c (g_file_open_tmp): (Win32:) Look also for (illegal) forward slashes in the template. diff --git a/ChangeLog.pre-2-2 b/ChangeLog.pre-2-2 index e5ca5dd..10269d5 100644 --- a/ChangeLog.pre-2-2 +++ b/ChangeLog.pre-2-2 @@ -1,5 +1,7 @@ 2001-01-05 Tor Lillqvist + * testglib.c (main): Add test for g_path_skip_root(). + * gfileutils.c (g_file_open_tmp): (Win32:) Look also for (illegal) forward slashes in the template. diff --git a/ChangeLog.pre-2-4 b/ChangeLog.pre-2-4 index e5ca5dd..10269d5 100644 --- a/ChangeLog.pre-2-4 +++ b/ChangeLog.pre-2-4 @@ -1,5 +1,7 @@ 2001-01-05 Tor Lillqvist + * testglib.c (main): Add test for g_path_skip_root(). + * gfileutils.c (g_file_open_tmp): (Win32:) Look also for (illegal) forward slashes in the template. diff --git a/ChangeLog.pre-2-6 b/ChangeLog.pre-2-6 index e5ca5dd..10269d5 100644 --- a/ChangeLog.pre-2-6 +++ b/ChangeLog.pre-2-6 @@ -1,5 +1,7 @@ 2001-01-05 Tor Lillqvist + * testglib.c (main): Add test for g_path_skip_root(). + * gfileutils.c (g_file_open_tmp): (Win32:) Look also for (illegal) forward slashes in the template. diff --git a/ChangeLog.pre-2-8 b/ChangeLog.pre-2-8 index e5ca5dd..10269d5 100644 --- a/ChangeLog.pre-2-8 +++ b/ChangeLog.pre-2-8 @@ -1,5 +1,7 @@ 2001-01-05 Tor Lillqvist + * testglib.c (main): Add test for g_path_skip_root(). + * gfileutils.c (g_file_open_tmp): (Win32:) Look also for (illegal) forward slashes in the template. diff --git a/glib/gutils.c b/glib/gutils.c index 957155c..7e2cec5 100644 --- a/glib/gutils.c +++ b/glib/gutils.c @@ -472,12 +472,28 @@ g_path_skip_root (gchar *file_name) g_return_val_if_fail (file_name != NULL, NULL); #ifdef G_OS_WIN32 - /* Skip \\server\share\ */ + /* Skip \\server\share */ if (file_name[0] == G_DIR_SEPARATOR && file_name[1] == G_DIR_SEPARATOR && - file_name[2] && - strchr (file_name + 2, G_DIR_SEPARATOR) > file_name + 2) - return strchr (file_name + 2, G_DIR_SEPARATOR) + 1; + file_name[2]) + { + gchar *p, *q; + + if ((p = strchr (file_name + 2, G_DIR_SEPARATOR)) > file_name + 2 && + p[1]) + { + file_name = p + 1; + + while (file_name[0] && file_name[0] != G_DIR_SEPARATOR) + file_name++; + + /* Possibly skip a backslash after the share name */ + if (file_name[0] == G_DIR_SEPARATOR) + file_name++; + + return file_name; + } + } #endif /* Skip initial slashes */ diff --git a/gutils.c b/gutils.c index 957155c..7e2cec5 100644 --- a/gutils.c +++ b/gutils.c @@ -472,12 +472,28 @@ g_path_skip_root (gchar *file_name) g_return_val_if_fail (file_name != NULL, NULL); #ifdef G_OS_WIN32 - /* Skip \\server\share\ */ + /* Skip \\server\share */ if (file_name[0] == G_DIR_SEPARATOR && file_name[1] == G_DIR_SEPARATOR && - file_name[2] && - strchr (file_name + 2, G_DIR_SEPARATOR) > file_name + 2) - return strchr (file_name + 2, G_DIR_SEPARATOR) + 1; + file_name[2]) + { + gchar *p, *q; + + if ((p = strchr (file_name + 2, G_DIR_SEPARATOR)) > file_name + 2 && + p[1]) + { + file_name = p + 1; + + while (file_name[0] && file_name[0] != G_DIR_SEPARATOR) + file_name++; + + /* Possibly skip a backslash after the share name */ + if (file_name[0] == G_DIR_SEPARATOR) + file_name++; + + return file_name; + } + } #endif /* Skip initial slashes */ diff --git a/testglib.c b/testglib.c index 2f7b615..5399a6e 100644 --- a/testglib.c +++ b/testglib.c @@ -333,28 +333,48 @@ main (int argc, { "/", "/" }, { "////", "/" }, { ".////", "." }, - { ".", "." }, - { "..", "." }, { "../", ".." }, { "..////", ".." }, - { "", "." }, { "a/b", "a" }, { "a/b/", "a/b" }, { "c///", "c" }, #else { "\\", "\\" }, { ".\\\\\\\\", "." }, - { ".", "." }, - { "..", "." }, { "..\\", ".." }, { "..\\\\\\\\", ".." }, - { "", "." }, { "a\\b", "a" }, { "a\\b\\", "a\\b" }, { "c\\\\\\", "c" }, #endif + { ".", "." }, + { "..", "." }, + { "", "." }, }; - guint n_dirname_checks = sizeof (dirname_checks) / sizeof (dirname_checks[0]); + guint n_dirname_checks = G_N_ELEMENTS (dirname_checks); + + struct { + gchar *filename; + gchar *without_root; + } skip_root_checks[] = { +#ifndef G_OS_WIN32 + { "/", "" }, + { "//", "" }, + { "/foo", "foo" }, + { "//foo", "foo" }, + { "a/b", NULL }, +#else + { "\\", "" }, + { "\\foo", "foo" }, + { "\\\\server\\foo", "" }, + { "\\\\server\\foo\\bar", "bar" }, + { "a\\b", NULL }, +#endif + { ".", NULL }, + { "", NULL }, + }; + guint n_skip_root_checks = G_N_ELEMENTS (skip_root_checks); + guint16 gu16t1 = 0x44afU, gu16t2 = 0xaf44U; guint32 gu32t1 = 0x02a7f109U, gu32t2 = 0x09f1a702U; #ifdef G_HAVE_GINT64 @@ -367,6 +387,7 @@ main (int argc, char template[10]; GError *error; char *name_used; + gchar *p; g_print ("TestGLib v%u.%u.%u (i:%u b:%u)\n", glib_major_version, @@ -416,6 +437,28 @@ main (int argc, if (n_dirname_checks) g_print ("ok\n"); + g_print ("checking g_path_skip_root()..."); + for (i = 0; i < n_skip_root_checks; i++) + { + gchar *skipped; + + skipped = g_path_skip_root (skip_root_checks[i].filename); + if ((skipped && !skip_root_checks[i].without_root) || + (!skipped && skip_root_checks[i].without_root) || + ((skipped && skip_root_checks[i].without_root) && + strcmp (skipped, skip_root_checks[i].without_root))) + { + g_print ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n", + skip_root_checks[i].filename, + (skip_root_checks[i].without_root ? + skip_root_checks[i].without_root : ""), + (skipped ? skipped : "")); + n_skip_root_checks = 0; + } + } + if (n_skip_root_checks) + g_print ("ok\n"); + g_print ("checking doubly linked lists..."); list = NULL; diff --git a/tests/testglib.c b/tests/testglib.c index 2f7b615..5399a6e 100644 --- a/tests/testglib.c +++ b/tests/testglib.c @@ -333,28 +333,48 @@ main (int argc, { "/", "/" }, { "////", "/" }, { ".////", "." }, - { ".", "." }, - { "..", "." }, { "../", ".." }, { "..////", ".." }, - { "", "." }, { "a/b", "a" }, { "a/b/", "a/b" }, { "c///", "c" }, #else { "\\", "\\" }, { ".\\\\\\\\", "." }, - { ".", "." }, - { "..", "." }, { "..\\", ".." }, { "..\\\\\\\\", ".." }, - { "", "." }, { "a\\b", "a" }, { "a\\b\\", "a\\b" }, { "c\\\\\\", "c" }, #endif + { ".", "." }, + { "..", "." }, + { "", "." }, }; - guint n_dirname_checks = sizeof (dirname_checks) / sizeof (dirname_checks[0]); + guint n_dirname_checks = G_N_ELEMENTS (dirname_checks); + + struct { + gchar *filename; + gchar *without_root; + } skip_root_checks[] = { +#ifndef G_OS_WIN32 + { "/", "" }, + { "//", "" }, + { "/foo", "foo" }, + { "//foo", "foo" }, + { "a/b", NULL }, +#else + { "\\", "" }, + { "\\foo", "foo" }, + { "\\\\server\\foo", "" }, + { "\\\\server\\foo\\bar", "bar" }, + { "a\\b", NULL }, +#endif + { ".", NULL }, + { "", NULL }, + }; + guint n_skip_root_checks = G_N_ELEMENTS (skip_root_checks); + guint16 gu16t1 = 0x44afU, gu16t2 = 0xaf44U; guint32 gu32t1 = 0x02a7f109U, gu32t2 = 0x09f1a702U; #ifdef G_HAVE_GINT64 @@ -367,6 +387,7 @@ main (int argc, char template[10]; GError *error; char *name_used; + gchar *p; g_print ("TestGLib v%u.%u.%u (i:%u b:%u)\n", glib_major_version, @@ -416,6 +437,28 @@ main (int argc, if (n_dirname_checks) g_print ("ok\n"); + g_print ("checking g_path_skip_root()..."); + for (i = 0; i < n_skip_root_checks; i++) + { + gchar *skipped; + + skipped = g_path_skip_root (skip_root_checks[i].filename); + if ((skipped && !skip_root_checks[i].without_root) || + (!skipped && skip_root_checks[i].without_root) || + ((skipped && skip_root_checks[i].without_root) && + strcmp (skipped, skip_root_checks[i].without_root))) + { + g_print ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n", + skip_root_checks[i].filename, + (skip_root_checks[i].without_root ? + skip_root_checks[i].without_root : ""), + (skipped ? skipped : "")); + n_skip_root_checks = 0; + } + } + if (n_skip_root_checks) + g_print ("ok\n"); + g_print ("checking doubly linked lists..."); list = NULL; -- 2.7.4