util-lib: handle empty string in last_path_component

Now the function returns an empty string when given an empty string.
Not sure if this is the best option (maybe this should be an error?),
but at least the behaviour is well defined.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2017-11-30 20:54:31 +01:00
parent 58e0ac3349
commit 69f9ccf140
2 changed files with 4 additions and 0 deletions

View file

@ -719,6 +719,9 @@ const char *last_path_component(const char *path) {
unsigned l, k;
l = k = strlen(path);
if (l == 0) /* special case — an empty string */
return path;
while (k > 0 && path[k-1] == '/')
k--;

View file

@ -411,6 +411,7 @@ static void test_last_path_component(void) {
assert_se(streq(last_path_component("././/"), ".//"));
assert_se(streq(last_path_component("/foo/a"), "a"));
assert_se(streq(last_path_component("/foo/a/"), "a/"));
assert_se(streq(last_path_component(""), ""));
}
static void test_filename_is_valid(void) {