From 56a7881508578a186542cb0d83303ebbcda86266 Mon Sep 17 00:00:00 2001 From: Stephane Sezer Date: Mon, 13 Jun 2011 17:59:57 +0200 Subject: [PATCH] Functions for path matching. Removed `path_equal()` because we do not need exact path equality, and added `path_match()` and `path_match_list()` functions to match check for a match with a list of patterns. --- src/utils/path.c | 40 +++++++++++++++++++++++++--------------- src/utils/path.h | 5 ++++- 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/utils/path.c b/src/utils/path.c index 794c10c..e2860d8 100644 --- a/src/utils/path.c +++ b/src/utils/path.c @@ -27,11 +27,13 @@ ** */ +#include #include #include #include #include +#include #include "path.h" @@ -76,26 +78,34 @@ char *path_rm_trailing_slashes(char *path) return path; } -/* -** This functions compares two paths for equality. The actual processing uses -** the realpath() function to get a connonical path out of the two paths passed -** as arguments. -*/ -bool path_equal(const char *path1, const char *path2) +bool path_match(const char *path, const char *pattern) { - bool res; - char *realpath1, *realpath2; + return fnmatch(pattern, path, 0) == 0; +} - realpath1 = realpath(path1, NULL); - realpath2 = realpath(path2, NULL); +struct path_match_list_cb_args +{ + bool *res; + const char *path; +}; - if (realpath1 == NULL || realpath2 == NULL) - return false; +static void path_match_list_cb(void *arg, void *data) +{ + struct path_match_list_cb_args *args = data; + + if (path_match(args->path, arg)) + *args->res = true; +} + +bool path_match_list(const char *path, struct list *pattern_list) +{ + bool res = false; + struct path_match_list_cb_args args; - res = (strcmp(realpath1, realpath2) == 0); + args.res = &res; + args.path = path; - free(realpath1); - free(realpath2); + list_foreach(pattern_list, path_match_list_cb, &args); return res; } diff --git a/src/utils/path.h b/src/utils/path.h index 9233d34..ea5c633 100644 --- a/src/utils/path.h +++ b/src/utils/path.h @@ -32,8 +32,11 @@ # include +# include + char *path_concat(const char *path, const char *elem); char *path_rm_trailing_slashes(char *path); -bool path_equal(const char *path1, const char *path2); +bool path_match(const char *path, const char *pattern); +bool path_match_list(const char *path, struct list *pattern_list); #endif /* !PATH_H_ */