summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--utils.c39
-rw-r--r--utils.h2
2 files changed, 41 insertions, 0 deletions
diff --git a/utils.c b/utils.c
index 88ba218..98c6255 100644
--- a/utils.c
+++ b/utils.c
@@ -112,6 +112,45 @@ strfreev (char **str_array)
}
}
+/* Compares if str has a specific path prefix. This differs
+ from a regular prefix in two ways. First of all there may
+ be multiple slashes separating the path elements, and
+ secondly, if a prefix is matched that has to be en entire
+ path element. For instance /a/prefix matches /a/prefix/foo/bar,
+ but not /a/prefixfoo/bar. */
+bool
+has_path_prefix (const char *str,
+ const char *prefix)
+{
+ while (TRUE)
+ {
+ /* Skip consecutive slashes to reach next path
+ element */
+ while (*str == '/')
+ str++;
+ while (*prefix == '/')
+ prefix++;
+
+ /* No more prefix path elements? Done! */
+ if (*prefix == 0)
+ return TRUE;
+
+ /* Compare path element */
+ while (*prefix != 0 && *prefix != '/')
+ {
+ if (*str != *prefix)
+ return FALSE;
+ str++;
+ prefix++;
+ }
+
+ /* Matched prefix path element,
+ must be entire str path element */
+ if (*str != '/' && *str != 0)
+ return FALSE;
+ }
+}
+
bool
has_prefix (const char *str,
const char *prefix)
diff --git a/utils.h b/utils.h
index 4a8261e..2a7790a 100644
--- a/utils.h
+++ b/utils.h
@@ -70,6 +70,8 @@ char* strdup_printf (const char *format,
...);
bool has_prefix (const char *str,
const char *prefix);
+bool has_path_prefix (const char *str,
+ const char *prefix);
int fdwalk (int proc_fd,
int (*cb)(void *data, int fd),
void *data);