-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatterns.c
More file actions
70 lines (62 loc) · 1.54 KB
/
patterns.c
File metadata and controls
70 lines (62 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "mysync.h"
// converts a glob string to a regular expression
char *glob2regex(char *glob)
{
char *re = NULL;
if(glob != NULL) {
re = calloc(strlen(glob)*2 + 4, sizeof(char));
if(re == NULL) {
return NULL;
}
char *r = re;
*r++ = '^';
while(*glob != '\0')
switch (*glob) {
case '.' :
case '\\':
case '$' : *r++ = '\\'; *r++ = *glob++; break;
case '*' : *r++ = '.'; *r++ = *glob++; break;
case '?' : *r++ = '.'; glob++; break;
case '/' : free(re);
re = NULL;
break;
default : *r++ = *glob++;
break;
}
if(re) {
*r++ = '$';
*r = '\0';
}
}
return re;
}
// checks if filename matches any ignore patterns
int matches_ignore(const char *name)
{
int match_found = 1;
if (option->iPattern->flag) {
for (int i = 0; i < option->iPattern->npatterns; i++) {
int regex_result = regexec(option->iPattern->patterns[i], name, 0, NULL, 0);
if (regex_result == 0) {
match_found = 0;
break;
}
}
}
return match_found;
}
// checks if filename matches any only patterns
int matches_only(const char *name)
{
int match_found = 1;
if (option->oPattern->flag) {
for (int i = 0; i < option->oPattern->npatterns; i++) {
int regex_result = regexec(option->oPattern->patterns[i], name, 0, NULL, 0);
if (regex_result == 0) {
match_found = 0;
break;
}
}
}
return match_found;
}