diff options
author | Du, Changbin <changbin.du@gmail.com> | 2014-01-23 18:54:12 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2014-01-23 19:36:55 -0500 |
commit | aace05097a0fd467230e39acb148be0fdaa90068 (patch) | |
tree | ca2e61621bcb24ad83f3f145987c8ffd5c28c65b | |
parent | 0d9dfc23f4d8c17365c84eb48ecca28b963ba192 (diff) |
lib/parser.c: add match_wildcard() function
match_wildcard function is a simple implementation of wildcard
matching algorithm. It only supports two usual wildcardes:
'*' - matches zero or more characters
'?' - matches one character
This algorithm is safe since it is non-recursive.
Signed-off-by: Du, Changbin <changbin.du@gmail.com>
Cc: Jason Baron <jbaron@akamai.com>
Cc: Joe Perches <joe@perches.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
-rw-r--r-- | include/linux/parser.h | 1 | ||||
-rw-r--r-- | lib/parser.c | 51 |
2 files changed, 52 insertions, 0 deletions
diff --git a/include/linux/parser.h b/include/linux/parser.h index ea2281e726f6..39d5b7955b23 100644 --- a/include/linux/parser.h +++ b/include/linux/parser.h | |||
@@ -29,5 +29,6 @@ int match_token(char *, const match_table_t table, substring_t args[]); | |||
29 | int match_int(substring_t *, int *result); | 29 | int match_int(substring_t *, int *result); |
30 | int match_octal(substring_t *, int *result); | 30 | int match_octal(substring_t *, int *result); |
31 | int match_hex(substring_t *, int *result); | 31 | int match_hex(substring_t *, int *result); |
32 | bool match_wildcard(const char *pattern, const char *str); | ||
32 | size_t match_strlcpy(char *, const substring_t *, size_t); | 33 | size_t match_strlcpy(char *, const substring_t *, size_t); |
33 | char *match_strdup(const substring_t *); | 34 | char *match_strdup(const substring_t *); |
diff --git a/lib/parser.c b/lib/parser.c index 807b2aaa33fa..ee5295541cea 100644 --- a/lib/parser.c +++ b/lib/parser.c | |||
@@ -193,6 +193,56 @@ int match_hex(substring_t *s, int *result) | |||
193 | } | 193 | } |
194 | 194 | ||
195 | /** | 195 | /** |
196 | * match_wildcard: - parse if a string matches given wildcard pattern | ||
197 | * @pattern: wildcard pattern | ||
198 | * @str: the string to be parsed | ||
199 | * | ||
200 | * Description: Parse the string @str to check if matches wildcard | ||
201 | * pattern @pattern. The pattern may contain two type wildcardes: | ||
202 | * '*' - matches zero or more characters | ||
203 | * '?' - matches one character | ||
204 | * If it's matched, return true, else return false. | ||
205 | */ | ||
206 | bool match_wildcard(const char *pattern, const char *str) | ||
207 | { | ||
208 | const char *s = str; | ||
209 | const char *p = pattern; | ||
210 | bool star = false; | ||
211 | |||
212 | while (*s) { | ||
213 | switch (*p) { | ||
214 | case '?': | ||
215 | s++; | ||
216 | p++; | ||
217 | break; | ||
218 | case '*': | ||
219 | star = true; | ||
220 | str = s; | ||
221 | if (!*++p) | ||
222 | return true; | ||
223 | pattern = p; | ||
224 | break; | ||
225 | default: | ||
226 | if (*s == *p) { | ||
227 | s++; | ||
228 | p++; | ||
229 | } else { | ||
230 | if (!star) | ||
231 | return false; | ||
232 | str++; | ||
233 | s = str; | ||
234 | p = pattern; | ||
235 | } | ||
236 | break; | ||
237 | } | ||
238 | } | ||
239 | |||
240 | if (*p == '*') | ||
241 | ++p; | ||
242 | return !*p; | ||
243 | } | ||
244 | |||
245 | /** | ||
196 | * match_strlcpy: - Copy the characters from a substring_t to a sized buffer | 246 | * match_strlcpy: - Copy the characters from a substring_t to a sized buffer |
197 | * @dest: where to copy to | 247 | * @dest: where to copy to |
198 | * @src: &substring_t to copy | 248 | * @src: &substring_t to copy |
@@ -235,5 +285,6 @@ EXPORT_SYMBOL(match_token); | |||
235 | EXPORT_SYMBOL(match_int); | 285 | EXPORT_SYMBOL(match_int); |
236 | EXPORT_SYMBOL(match_octal); | 286 | EXPORT_SYMBOL(match_octal); |
237 | EXPORT_SYMBOL(match_hex); | 287 | EXPORT_SYMBOL(match_hex); |
288 | EXPORT_SYMBOL(match_wildcard); | ||
238 | EXPORT_SYMBOL(match_strlcpy); | 289 | EXPORT_SYMBOL(match_strlcpy); |
239 | EXPORT_SYMBOL(match_strdup); | 290 | EXPORT_SYMBOL(match_strdup); |