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
|
* 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) {
|
static char* cpy_path(char *buf, const char *path) {
|
||||||
|
|
||||||
|
|
@ -120,7 +120,7 @@ size_t pathlen(const char *path) {
|
||||||
|
|
||||||
size_t size = 0;
|
size_t size = 0;
|
||||||
|
|
||||||
while(*path != 0) {
|
while(*path) {
|
||||||
|
|
||||||
size++;
|
size++;
|
||||||
|
|
||||||
|
|
@ -149,6 +149,9 @@ char* fmt_path(const char *base, const char *name, unsigned char dir) {
|
||||||
size++;
|
size++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (*(base+size) != '/')
|
||||||
|
size++;
|
||||||
|
|
||||||
ptr = alloc_path(size);
|
ptr = alloc_path(size);
|
||||||
|
|
||||||
if (ptr == NULL)
|
if (ptr == NULL)
|
||||||
|
|
@ -158,8 +161,6 @@ char* fmt_path(const char *base, const char *name, unsigned char dir) {
|
||||||
|
|
||||||
ptr = cpy_path(ptr, base);
|
ptr = cpy_path(ptr, base);
|
||||||
|
|
||||||
dassert((ptr-ret) == pathlen(base));
|
|
||||||
|
|
||||||
if (name != NULL) {
|
if (name != NULL) {
|
||||||
memcpy(ptr, name, strlen(name));
|
memcpy(ptr, name, strlen(name));
|
||||||
if (dir)
|
if (dir)
|
||||||
|
|
@ -180,22 +181,16 @@ char* basename(char *path) {
|
||||||
}
|
}
|
||||||
|
|
||||||
while(*path != '\0') {
|
while(*path != '\0') {
|
||||||
|
|
||||||
if (*path == '/') {
|
if (*path == '/') {
|
||||||
|
|
||||||
if (*(path+1) == '\0') {
|
if (*(path+1) == '\0') {
|
||||||
|
|
||||||
if (pos >= path)
|
if (pos >= path)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
*(path--) = '\0';
|
*(path--) = '\0';
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (*(path+1) != '/')
|
if (*(path+1) != '/')
|
||||||
pos = path+1;
|
pos = path+1;
|
||||||
}
|
}
|
||||||
|
|
||||||
path++;
|
path++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -216,10 +211,8 @@ char* dirname(char *path) {
|
||||||
len = split_path(path) - path;
|
len = split_path(path) - path;
|
||||||
|
|
||||||
if (len <= 1) {
|
if (len <= 1) {
|
||||||
|
|
||||||
if (*path != '/')
|
if (*path != '/')
|
||||||
path[0] = '.';
|
path[0] = '.';
|
||||||
|
|
||||||
path[1] = '\0';
|
path[1] = '\0';
|
||||||
} else {
|
} else {
|
||||||
path[len] = '\0';
|
path[len] = '\0';
|
||||||
|
|
|
||||||
|
|
@ -14,17 +14,14 @@ void test_fmt_path() {
|
||||||
ptr = fmt_path("/usr/src/", "linux", 0);
|
ptr = fmt_path("/usr/src/", "linux", 0);
|
||||||
assert_string(ptr, "/usr/src/linux");
|
assert_string(ptr, "/usr/src/linux");
|
||||||
free(ptr);
|
free(ptr);
|
||||||
/*
|
|
||||||
ptr = fmt_path("/segment1/segment2/", "segment3/", 1);
|
ptr = fmt_path("/segment1/segment2/", "segment3", 1);
|
||||||
assert_string(ptr, "/segment1/segment2/segment3/");
|
assert_string(ptr, "/segment1/segment2/segment3/");
|
||||||
free(ptr);
|
free(ptr);
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
ptr = fmt_path("/stuff/with/ahell/lot/of/slashes/at/the/", "end", 1);
|
||||||
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/");
|
||||||
assert_string(ptr, "/stuff/with/ahell/lot/of/slashes/at/the/end/////////");
|
|
||||||
free(ptr);
|
free(ptr);
|
||||||
*/
|
|
||||||
|
|
||||||
ptr = fmt_path("/mnt/cdrom", "keff", 0);
|
ptr = fmt_path("/mnt/cdrom", "keff", 0);
|
||||||
assert_string(ptr, "/mnt/cdrom/keff");
|
assert_string(ptr, "/mnt/cdrom/keff");
|
||||||
|
|
|
||||||
Reference in a new issue