util: add macro for iterating through all prefixes of a path

Syntactic sugar in a macro PATH_FOREACH_PREFIX.
This commit is contained in:
Lennart Poettering 2013-09-25 20:58:23 +02:00
parent bc5fb0809e
commit fecffe5d0a
3 changed files with 34 additions and 22 deletions

View file

@ -106,8 +106,35 @@ static void test_find_binary(void) {
assert(find_binary("xxxx-xxxx", &p) == -ENOENT);
}
static void test_prefixes(void) {
static const char* values[] = { "/a/b/c", "/a/b", "/a", "", NULL};
unsigned i = 0;
char s[PATH_MAX];
PATH_FOREACH_PREFIX(s, "/a/b/c/d") {
log_error("---%s---", s);
assert_se(streq(s, values[i++]));
}
assert_se(values[i] == NULL);
i = 0;
PATH_FOREACH_PREFIX(s, "////a////b////c///d///////")
assert_se(streq(s, values[i++]));
assert_se(values[i] == NULL);
PATH_FOREACH_PREFIX(s, "////")
assert_se(streq(s, ""));
PATH_FOREACH_PREFIX(s, "")
assert_not_reached("wut?");
}
int main(void) {
test_path();
test_find_binary();
test_prefixes();
return 0;
}