From 79023b26a4f7a44b9e98a69f4b6600920ee74679 Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Tue, 20 Sep 2011 09:21:11 +0400 Subject: [PATCH] (mc_build_filename): incorrect processing of first element of path. If first element is relative, the result path should be also relative not absolute. If first element is empty, the result path is relative. Signed-off-by: Andrew Borodin --- lib/utilunix.c | 6 +++++- tests/lib/mc_build_filename.c | 13 ++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/utilunix.c b/lib/utilunix.c index a0dee800f..38ef462c4 100644 --- a/lib/utilunix.c +++ b/lib/utilunix.c @@ -1006,6 +1006,7 @@ get_user_permissions (struct stat *st) char * mc_build_filename (const char *first_element, ...) { + gboolean absolute; va_list args; const char *element = first_element; GString *path; @@ -1017,6 +1018,8 @@ mc_build_filename (const char *first_element, ...) path = g_string_new (""); va_start (args, first_element); + absolute = (*first_element != '\0' && *first_element == PATH_SEP); + do { if (*element == '\0') @@ -1046,7 +1049,8 @@ mc_build_filename (const char *first_element, ...) va_end (args); - g_string_prepend_c (path, PATH_SEP); + if (absolute) + g_string_prepend_c (path, PATH_SEP); ret = g_string_free (path, FALSE); canonicalize_pathname (ret); diff --git a/tests/lib/mc_build_filename.c b/tests/lib/mc_build_filename.c index e9568ce30..5b3764f65 100644 --- a/tests/lib/mc_build_filename.c +++ b/tests/lib/mc_build_filename.c @@ -54,7 +54,7 @@ START_TEST (test_mc_build_filename) { char *result; - check_mc_build_filename(("test", "path", NULL), "/test/path"); + check_mc_build_filename(("test", "path", NULL), "test/path"); check_mc_build_filename(("/test", "path/", NULL), "/test/path"); @@ -68,6 +68,17 @@ START_TEST (test_mc_build_filename) check_mc_build_filename(("/test", "path", "..", "/test", "path/", NULL), "/test/test/path"); + check_mc_build_filename(("", "path", NULL), "path"); + + check_mc_build_filename(("", "/path", NULL), "path"); + + check_mc_build_filename(("path", "", NULL), "path"); + + check_mc_build_filename(("/path", "", NULL), "/path"); + + check_mc_build_filename(("pa", "", "th", NULL), "pa/th"); + + check_mc_build_filename(("/pa", "", "/th", NULL), "/pa/th"); } END_TEST