util-lib: introduce new empty_or_root() helper (#8746)

We check the same condition at various places. Let's add a trivial,
common helper for this, and use it everywhere.

It's not going to make things much faster or much shorter, but I think a
lot more readable
This commit is contained in:
Lennart Poettering
2018-04-18 14:20:49 +02:00
committed by Zbigniew Jędrzejewski-Szmek
parent 12777909c9
commit 57ea45e11a
11 changed files with 50 additions and 18 deletions
+12 -1
View File
@@ -640,7 +640,7 @@ char *prefix_root(const char *root, const char *path) {
while (path[0] == '/' && path[1] == '/')
path++;
if (isempty(root) || path_equal(root, "/"))
if (empty_or_root(root))
return strdup(path);
l = strlen(root) + 1 + strlen(path) + 1;
@@ -965,3 +965,14 @@ bool dot_or_dot_dot(const char *path) {
return path[2] == 0;
}
bool empty_or_root(const char *root) {
/* For operations relative to some root directory, returns true if the specified root directory is redundant,
* i.e. either / or NULL or the empty string or any equivalent. */
if (!root)
return true;
return root[strspn(root, "/")] == 0;
}