path-util: add flavour of path_startswith() that leaves a leading slash in place
This commit is contained in:
+28
-11
@@ -433,8 +433,8 @@ int path_simplify_alloc(const char *path, char **ret) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
char* path_startswith_full(const char *path, const char *prefix, bool accept_dot_dot) {
|
||||
assert(path);
|
||||
char* path_startswith_full(const char *original_path, const char *prefix, PathStartWithFlags flags) {
|
||||
assert(original_path);
|
||||
assert(prefix);
|
||||
|
||||
/* Returns a pointer to the start of the first component after the parts matched by
|
||||
@@ -447,28 +447,45 @@ char* path_startswith_full(const char *path, const char *prefix, bool accept_dot
|
||||
* Returns NULL otherwise.
|
||||
*/
|
||||
|
||||
const char *path = original_path;
|
||||
|
||||
if ((path[0] == '/') != (prefix[0] == '/'))
|
||||
return NULL;
|
||||
|
||||
for (;;) {
|
||||
const char *p, *q;
|
||||
int r, k;
|
||||
int m, n;
|
||||
|
||||
r = path_find_first_component(&path, accept_dot_dot, &p);
|
||||
if (r < 0)
|
||||
m = path_find_first_component(&path, FLAGS_SET(flags, PATH_STARTSWITH_ACCEPT_DOT_DOT), &p);
|
||||
if (m < 0)
|
||||
return NULL;
|
||||
|
||||
k = path_find_first_component(&prefix, accept_dot_dot, &q);
|
||||
if (k < 0)
|
||||
n = path_find_first_component(&prefix, FLAGS_SET(flags, PATH_STARTSWITH_ACCEPT_DOT_DOT), &q);
|
||||
if (n < 0)
|
||||
return NULL;
|
||||
|
||||
if (k == 0)
|
||||
return (char*) (p ?: path);
|
||||
if (n == 0) {
|
||||
if (!p)
|
||||
p = path;
|
||||
|
||||
if (r != k)
|
||||
if (FLAGS_SET(flags, PATH_STARTSWITH_RETURN_LEADING_SLASH)) {
|
||||
|
||||
if (p <= original_path)
|
||||
return NULL;
|
||||
|
||||
p--;
|
||||
|
||||
if (*p != '/')
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return (char*) p;
|
||||
}
|
||||
|
||||
if (m != n)
|
||||
return NULL;
|
||||
|
||||
if (!strneq(p, q, r))
|
||||
if (!strneq(p, q, m))
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user