Archived
1
0
Fork 0

common/path.c: fixed is_abspath.

This commit is contained in:
H Hautakoski 2010-08-22 14:08:30 +02:00 committed by Henrik Hautakoski
parent af818017ed
commit d5d79c77db
2 changed files with 30 additions and 5 deletions

View file

@ -82,12 +82,22 @@ static char* cpy_path(char *buf, const char *path) {
} }
int is_abspath(const char *path) { int is_abspath(const char *path) {
if (*path != '/') if (path == NULL || *path != '/')
return 0; return 0;
return strstr(path, "/./") == NULL && for(; *path; path++) {
strstr(path, "/../") == NULL;
if (*path == '/' && *(path+1) == '.') {
path += 2;
if (*path == '.')
path++;
if (*path == '/' || *path == '\0')
return 0;
}
}
return 1;
} }
size_t pathlen(const char *path) { size_t pathlen(const char *path) {

View file

@ -37,6 +37,20 @@ void test_pathlen() {
assert(pathlen("dir") == 3); 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() { void test_basename() {
int i; int i;
@ -83,6 +97,7 @@ void test_dirname() {
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
test_isabspath();
test_fmt_path(); test_fmt_path();
test_pathlen(); test_pathlen();
test_basename(); test_basename();