-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpattern.c
154 lines (142 loc) · 3.1 KB
/
pattern.c
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
@author: Huzaifa Naseer
*/
#define _GNU_SOURCE /* FNM_CASEFOLD */
#include "shell.h"
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fnmatch.h>
#include <glob.h>
#include <locale.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
/*
* check if the string *p has any regular expression (regex) characters,
* which are *, ?, [ and ].
*/
int has_glob_chars(char *p, size_t len) {
char *p2 = p + len;
char ob = 0, cb = 0; /* count of opening and closing brackets */
while (p < p2 && *p) {
switch (*p) {
case '*':
case '?':
return 1;
case '[':
ob++;
break;
case ']':
cb++;
break;
}
p++;
}
/* do we have a matching number of opening and closing brackets? */
if (ob && ob == cb) {
return 1;
}
return 0;
}
/*
* find the shortest or longest prefix of str that matches
* pattern, depending on the value of longest.
* return value is the index of 1 after the last character
* in the prefix, i.e. where you should put a '\0' to get
* the prefix.
*/
int match_prefix(char *pattern, char *str, int longest) {
if (!pattern || !str) {
return 0;
}
char *s = str + 1;
char c = *s;
char *smatch = NULL;
char *lmatch = NULL;
while (c) {
*s = '\0';
if (fnmatch(pattern, str, 0) == 0) {
if (!smatch) {
if (!longest) {
*s = c;
return s - str;
}
smatch = s;
}
lmatch = s;
}
*s = c;
c = *(++s);
}
/* check the result of the comparison */
if (lmatch) {
return lmatch - str;
}
if (smatch) {
return smatch - str;
}
return 0;
}
/*
* find the shortest or longest suffix of str that matches
* pattern, depending on the value of longest.
* return value is the index of the first character in the
* matched suffix.
*/
int match_suffix(char *pattern, char *str, int longest) {
if (!pattern || !str) {
return 0;
}
char *s = str + strlen(str) - 1;
char *smatch = NULL;
char *lmatch = NULL;
while (s > str) {
if (fnmatch(pattern, str, 0) == 0) {
if (!smatch) {
if (!longest) {
return s - str;
}
smatch = s;
}
lmatch = s;
}
s--;
}
/* check the result of the comparison */
if (lmatch) {
return lmatch - str;
}
if (smatch) {
return smatch - str;
}
return 0;
}
/*
* perform pathname (or filename) expansion, matching files in the given *dir to
* the given *path, which is treated as a regex pattern that specifies which
* filename(s) we should match.
*
* returns a char ** pointer to the list of matched filenames, or NULL if
* nothing matched.
*/
char **get_filename_matches(char *pattern, glob_t *matches) {
/* to guard our caller from trying to free an unused struct in case of
* expansion failure */
matches->gl_pathc = 0;
matches->gl_pathv = NULL;
if (!pattern) {
return NULL;
}
/* perform the match */
int res = glob(pattern, 0, NULL, matches);
/* return the result */
if (res != 0) {
globfree(matches);
return NULL;
}
return matches->gl_pathv;
}