From d5d79c77dbf48a92d15d3a61e60d1896b9315393 Mon Sep 17 00:00:00 2001 From: H Hautakoski Date: Sun, 22 Aug 2010 14:08:30 +0200 Subject: [PATCH] common/path.c: fixed is_abspath. --- src/common/path.c | 20 +++++++++++++++----- test/t_path.c | 15 +++++++++++++++ 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/common/path.c b/src/common/path.c index 3db7524..6e9516e 100644 --- a/src/common/path.c +++ b/src/common/path.c @@ -82,12 +82,22 @@ static char* cpy_path(char *buf, const char *path) { } int is_abspath(const char *path) { - - if (*path != '/') + + if (path == NULL || *path != '/') return 0; - - return strstr(path, "/./") == NULL && - strstr(path, "/../") == NULL; + + for(; *path; path++) { + + if (*path == '/' && *(path+1) == '.') { + path += 2; + if (*path == '.') + path++; + if (*path == '/' || *path == '\0') + return 0; + } + } + + return 1; } size_t pathlen(const char *path) { diff --git a/test/t_path.c b/test/t_path.c index ba44487..85254d9 100644 --- a/test/t_path.c +++ b/test/t_path.c @@ -37,6 +37,20 @@ void test_pathlen() { assert(pathlen("dir") == 3); } +void test_isabspath() { + + assert(is_abspath("file") == 0); + assert(is_abspath("./file") == 0); + assert(is_abspath(".file") == 0); + assert(is_abspath("..file") == 0); + assert(is_abspath("/../relpath") == 0); + assert(is_abspath("/path/to/file") == 1); + assert(is_abspath("/ab/xy/.file") == 1); + assert(is_abspath("/ab/xy/..file") == 1); + assert(is_abspath("/ab/.xy/file") == 1); + assert(is_abspath("/ab/..xy/file") ==1); +} + void test_basename() { int i; @@ -83,6 +97,7 @@ void test_dirname() { int main(int argc, char *argv[]) { + test_isabspath(); test_fmt_path(); test_pathlen(); test_basename();