Changes in uspace/lib/posix/fnmatch.c [6921178:f215bb5f] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/posix/fnmatch.c
r6921178 rf215bb5f 33 33 */ 34 34 35 /* This file contains an implementation of the fnmatch() pattern matching 36 * function. There is more code than necessary to account for the possibility 37 * of adding POSIX-like locale support to the system in the future. Functions 38 * that are only necessary for locale support currently simply use single 39 * characters for "collation elements". 40 * When (or if) locales are properly implemented, extending this implementation 41 * will be fairly straightforward. 42 */ 35 // TODO: clean this up a bit 43 36 44 37 #include "stdbool.h" … … 53 46 #include "fnmatch.h" 54 47 55 /* Returned by _match... functions. */ 48 // TODO: documentation 49 56 50 #define INVALID_PATTERN -1 57 51 … … 59 53 * but may be extended for better locale support. 60 54 */ 61 typedef int coll_elm_t; 62 63 /** Return value indicating that the element in question 64 * is not valid in the current locale. (That is, if locales are supported.) 65 */ 55 typedef int _coll_elm_t; 56 66 57 #define COLL_ELM_INVALID -1 67 58 68 /** 69 * Get collating element matching a string. 70 * 71 * @param str String representation of the element. 72 * @return Matching collating element or COLL_ELM_INVALID. 73 */ 74 static coll_elm_t _coll_elm_get(const char* str) 59 /** Get collating element matching a string. 60 * 61 * @param str 62 * @return 63 */ 64 static _coll_elm_t _coll_elm_get(const char* str) 75 65 { 76 66 if (str[0] == '\0' || str[1] != '\0') { … … 80 70 } 81 71 82 /** 83 * Get collating element matching a single character. 84 * 85 * @param c Character representation of the element. 86 * @return Matching collating element. 87 */ 88 static coll_elm_t _coll_elm_char(int c) 72 /** Get collating element matching a single character. 73 * 74 * @param c 75 * @return 76 */ 77 static _coll_elm_t _coll_elm_char(int c) 89 78 { 90 79 return c; 91 80 } 92 81 93 /** 94 * Match collating element with a beginning of a string. 95 * 96 * @param elm Collating element to match. 97 * @param str String which beginning should match the element. 82 /** Match collating element with a beginning of a string. 83 * 84 * @param elm 85 * @param str 98 86 * @return 0 if the element doesn't match, or the number of characters matched. 99 87 */ 100 static int _coll_elm_match( coll_elm_t elm, const char *str)88 static int _coll_elm_match(_coll_elm_t elm, const char *str) 101 89 { 102 90 return elm == *str; 103 91 } 104 92 105 /** 106 * Checks whether a string begins with a collating element in the given range. 107 * Ordering depends on the locale (if locales are supported). 108 * 109 * @param first First element of the range. 110 * @param second Last element of the range. 111 * @param str String to match. 112 * @return 0 if there is no match, or the number of characters matched. 113 */ 114 static int _coll_elm_between(coll_elm_t first, coll_elm_t second, 93 static int _coll_elm_between(_coll_elm_t first, _coll_elm_t second, 115 94 const char *str) 116 95 { … … 118 97 } 119 98 120 /** 121 * Read a string delimited by [? and ?]. 99 /** Read a string delimited by [? and ?]. 122 100 * 123 101 * @param pattern Pointer to the string to read from. Its position is moved … … 127 105 * @param buf_sz Read buffer's size. If the buffer is not large enough for 128 106 * the entire string, the string is cut with no error indication. 129 * @param flags Flags modifying the behavior. 130 * @return True on success, false if the pattern is invalid. 107 * @return 131 108 */ 132 109 static bool _get_delimited( … … 195 172 196 173 /** 197 * Compare function for binary search in the _char_classes array. 198 * 199 * @param key Key of the searched element. 200 * @param elem Element of _char_classes array. 201 * @return Ordering indicator (-1 less than, 0 equal, 1 greater than). 174 * 175 * @param key 176 * @param elem 177 * @return 202 178 */ 203 179 static int _class_compare(const void *key, const void *elem) … … 208 184 209 185 /** 210 * Returns whether the given character belongs to the specified character class. 211 * 212 * @param cname Name of the character class. 213 * @param c Character. 214 * @return True if the character belongs to the class, false otherwise. 186 * 187 * @param cname 188 * @param c 189 * @return 215 190 */ 216 191 static bool _is_in_class (const char *cname, int c) … … 231 206 232 207 /** 233 * Tries to parse an initial part of the pattern as a character class pattern, 234 * and if successful, matches the beginning of the given string against the class. 235 * 236 * @param pattern Pointer to the pattern to match. Must begin with a class 237 * specifier and is repositioned to the first character after the specifier 238 * if successful. 239 * @param str String to match. 240 * @param flags Flags modifying the behavior (see fnmatch()). 241 * @return INVALID_PATTERN if the pattern doesn't start with a valid class 242 * specifier, 0 if the beginning of the matched string doesn't belong 243 * to the class, or positive number of characters matched. 208 * 209 * @param pattern 210 * @param str 211 * @param flags 212 * @return 244 213 */ 245 214 static int _match_char_class(const char **pattern, const char *str, int flags) … … 257 226 258 227 /** 259 * Reads the next collating element in the pattern, taking into account 260 * locale (if supported) and flags (see fnmatch()). 261 * 262 * @param pattern Pattern. 263 * @param flags Flags given to fnmatch(). 264 * @return Collating element on success, 265 * or COLL_ELM_INVALID if the pattern is invalid. 266 */ 267 static coll_elm_t _next_coll_elm(const char **pattern, int flags) 268 { 269 assert(pattern != NULL); 270 assert(*pattern != NULL); 271 assert(**pattern != '\0'); 272 228 * 229 * @param pattern 230 * @param flags 231 * @return 232 */ 233 static _coll_elm_t _next_coll_elm(const char **pattern, int flags) 234 { 273 235 const char *p = *pattern; 274 236 const bool noescape = (flags & FNM_NOESCAPE) != 0; … … 295 257 if (!noescape && *p == '\\') { 296 258 p++; 297 if (*p == '\0') {298 *pattern = p;299 return COLL_ELM_INVALID;300 }301 259 } 302 260 if (pathname && *p == '/') { 303 261 return COLL_ELM_INVALID; 304 262 } 305 263 306 264 *pattern = p + 1; 307 265 return _coll_elm_char(*p); … … 309 267 310 268 /** 311 * Matches the beginning of the given string against a bracket expression 312 * the pattern begins with. 313 * 314 * @param pattern Pointer to the beginning of a bracket expression in a pattern. 315 * On success, the pointer is moved to the first character after the 316 * bracket expression. 317 * @param str Unmatched part of the string. 318 * @param flags Flags given to fnmatch(). 319 * @return INVALID_PATTERN if the pattern is invalid, 0 if there is no match 320 * or the number of matched characters on success. 269 * 270 * @param pattern 271 * @param str 272 * @param flags 273 * @return 321 274 */ 322 275 static int _match_bracket_expr(const char **pattern, const char *str, int flags) … … 362 315 } 363 316 364 coll_elm_t current_elm = COLL_ELM_INVALID;317 _coll_elm_t current_elm = COLL_ELM_INVALID; 365 318 366 319 while (*p != ']') { … … 369 322 /* Range expression. */ 370 323 p++; 371 coll_elm_t end_elm = _next_coll_elm(&p, flags);324 _coll_elm_t end_elm = _next_coll_elm(&p, flags); 372 325 if (end_elm == COLL_ELM_INVALID) { 373 326 return INVALID_PATTERN; … … 405 358 406 359 /** 407 * Matches a portion of the pattern containing no asterisks (*) against 408 * the given string. 409 * 410 * @param pattern Pointer to the unmatched portion of the pattern. 411 * On success, the pointer is moved to the first asterisk, or to the 412 * terminating nul character, whichever occurs first. 413 * @param string Pointer to the input string. On success, the pointer is moved 414 * to the first character that wasn't explicitly matched. 415 * @param flags Flags given to fnmatch(). 416 * @return True if the entire subpattern matched. False otherwise. 360 * 361 * @param pattern 362 * @param string 363 * @param flags 364 * @return 417 365 */ 418 366 static bool _partial_match(const char **pattern, const char **string, int flags) … … 509 457 510 458 /** 511 * Match string against a pattern. 512 * 513 * @param pattern Pattern. 514 * @param string String to match. 515 * @param flags Flags given to fnmatch(). 516 * @return True if the string matched the pattern, false otherwise. 459 * 460 * @param pattern 461 * @param string 462 * @param flags 463 * @return 517 464 */ 518 465 static bool _full_match(const char *pattern, const char *string, int flags) … … 549 496 end = string; 550 497 } else { 551 end 498 end= strchrnul(string, pathname ? '/' : '\0'); 552 499 } 553 500 … … 572 519 573 520 /** 574 * Transform the entire string to lowercase. 575 * 576 * @param s Input string. 577 * @return Newly allocated copy of the input string with all uppercase 578 * characters folded to their lowercase variants. 521 * 522 * @param s 523 * @return 579 524 */ 580 525 static char *_casefold(const char *s) … … 591 536 * Filename pattern matching. 592 537 * 593 * @param pattern Pattern to match the string against. 594 * @param string Matched string. 595 * @param flags Flags altering the matching of special characters 596 * (mainly for dot and slash). 597 * @return Zero if the string matches the pattern, FNM_NOMATCH otherwise. 538 * @param pattern 539 * @param string 540 * @param flags 541 * @return 598 542 */ 599 543 int posix_fnmatch(const char *pattern, const char *string, int flags) … … 624 568 } 625 569 626 // FIXME: put the testcases to the app/tester after fnmatch is included into libc570 // FIXME: put the testcases somewhere else 627 571 628 572 #if 0
Note:
See TracChangeset
for help on using the changeset viewer.