From b326f23c85018ee0208deeb5f9ab09726e918f59 Mon Sep 17 00:00:00 2001 From: H Hautakoski Date: Wed, 9 Jun 2010 22:44:05 +0200 Subject: [PATCH] common/path.c: fixed bug whit trailing slash in fmt_path --- src/common/path.c | 21 +++++++-------------- test/t_path.c | 11 ++++------- 2 files changed, 11 insertions(+), 21 deletions(-) diff --git a/src/common/path.c b/src/common/path.c index 5200059..9215387 100644 --- a/src/common/path.c +++ b/src/common/path.c @@ -58,7 +58,7 @@ static inline int has_delim(const char *str) { /* * copy a clean path to buf - * NOTE: buffer should be atleast of size pathlen(path) + * NOTE: buffer should be atleast of size pathlen(path)+1 */ static char* cpy_path(char *buf, const char *path) { @@ -120,7 +120,7 @@ size_t pathlen(const char *path) { size_t size = 0; - while(*path != 0) { + while(*path) { size++; @@ -129,7 +129,7 @@ size_t pathlen(const char *path) { else path++; } - + return size; } @@ -149,6 +149,9 @@ char* fmt_path(const char *base, const char *name, unsigned char dir) { size++; } + if (*(base+size) != '/') + size++; + ptr = alloc_path(size); if (ptr == NULL) @@ -157,9 +160,7 @@ char* fmt_path(const char *base, const char *name, unsigned char dir) { ret = ptr; ptr = cpy_path(ptr, base); - - dassert((ptr-ret) == pathlen(base)); - + if (name != NULL) { memcpy(ptr, name, strlen(name)); if (dir) @@ -180,22 +181,16 @@ char* basename(char *path) { } while(*path != '\0') { - if (*path == '/') { - if (*(path+1) == '\0') { - if (pos >= path) break; - *(path--) = '\0'; continue; } - if (*(path+1) != '/') pos = path+1; } - path++; } @@ -216,10 +211,8 @@ char* dirname(char *path) { len = split_path(path) - path; if (len <= 1) { - if (*path != '/') path[0] = '.'; - path[1] = '\0'; } else { path[len] = '\0'; diff --git a/test/t_path.c b/test/t_path.c index ebf3e68..ba44487 100644 --- a/test/t_path.c +++ b/test/t_path.c @@ -14,17 +14,14 @@ void test_fmt_path() { ptr = fmt_path("/usr/src/", "linux", 0); assert_string(ptr, "/usr/src/linux"); free(ptr); -/* - ptr = fmt_path("/segment1/segment2/", "segment3/", 1); + + ptr = fmt_path("/segment1/segment2/", "segment3", 1); assert_string(ptr, "/segment1/segment2/segment3/"); free(ptr); - */ -/* - ptr = fmt_path("/stuff/with/ahell/lot/of/slashes/at/the/", "end/////////", 1); - assert_string(ptr, "/stuff/with/ahell/lot/of/slashes/at/the/end/////////"); + ptr = fmt_path("/stuff/with/ahell/lot/of/slashes/at/the/", "end", 1); + assert_string(ptr, "/stuff/with/ahell/lot/of/slashes/at/the/end/"); free(ptr); - */ ptr = fmt_path("/mnt/cdrom", "keff", 0); assert_string(ptr, "/mnt/cdrom/keff");