common/path.c: fixed bug whit trailing slash in fmt_path
This commit is contained in:
parent
fd3d2c0fa6
commit
b326f23c85
2 changed files with 11 additions and 21 deletions
|
|
@ -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';
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
|
|
|
|||
Reference in a new issue