Skip to content
This repository has been archived by the owner on Apr 16, 2023. It is now read-only.

Commit

Permalink
Functions for path matching.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sas committed Jun 13, 2011
1 parent 2e1f8dd commit 56a7881
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
40 changes: 25 additions & 15 deletions src/utils/path.c
Expand Up @@ -27,11 +27,13 @@
**
*/

#include <fnmatch.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

#include <utils/diefuncs.h>
#include <utils/list.h>

#include "path.h"

Expand Down Expand Up @@ -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;
}
5 changes: 4 additions & 1 deletion src/utils/path.h
Expand Up @@ -32,8 +32,11 @@

# include <stdbool.h>

# include <utils/list.h>

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_ */

0 comments on commit 56a7881

Please sign in to comment.