diff options
-rw-r--r-- | utils.c | 39 | ||||
-rw-r--r-- | utils.h | 2 |
2 files changed, 41 insertions, 0 deletions
@@ -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) @@ -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); |