Changes in uspace/lib/posix/fnmatch.c [4c8f5e7:f215bb5f] in mainline
- File:
-
- 1 edited
-
uspace/lib/posix/fnmatch.c (modified) (18 diffs)
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/posix/fnmatch.c
r4c8f5e7 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 … … 69 60 * 70 61 * @param str 71 * @return Matching collating element or COLL_ELM_INVALID.72 */ 73 static coll_elm_t _coll_elm_get(const char* str)62 * @return 63 */ 64 static _coll_elm_t _coll_elm_get(const char* str) 74 65 { 75 66 if (str[0] == '\0' || str[1] != '\0') { … … 84 75 * @return 85 76 */ 86 static coll_elm_t _coll_elm_char(int c)77 static _coll_elm_t _coll_elm_char(int c) 87 78 { 88 79 return c; … … 95 86 * @return 0 if the element doesn't match, or the number of characters matched. 96 87 */ 97 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) 98 89 { 99 90 return elm == *str; 100 91 } 101 92 102 /** Checks whether a string begins with a collating element in the given range. 103 * Ordering depends on the locale (if locales are supported). 104 * 105 * @param first First element of the range. 106 * @param second Last element of the range. 107 * @param str String to match. 108 * @return 0 if there is no match, or the number of characters matched. 109 */ 110 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, 111 94 const char *str) 112 95 { … … 122 105 * @param buf_sz Read buffer's size. If the buffer is not large enough for 123 106 * the entire string, the string is cut with no error indication. 124 * @param flags Flags modifying the behavior. 125 * @return True on success, false if the pattern is invalid. 107 * @return 126 108 */ 127 109 static bool _get_delimited( … … 189 171 }; 190 172 191 /** Compare function for binary search in the _char_classes array.173 /** 192 174 * 193 175 * @param key … … 201 183 } 202 184 203 /** Returns whether the given character belongs to the specified character class.204 * 205 * @param cname Name of the character class.206 * @param c Character.207 * @return True if the character belongs to the class, false otherwise.185 /** 186 * 187 * @param cname 188 * @param c 189 * @return 208 190 */ 209 191 static bool _is_in_class (const char *cname, int c) … … 223 205 } 224 206 225 /** Tries to parse an initial part of the pattern as a character class pattern, 226 * and if successful, matches the beginning of the given string against the class. 227 * 228 * @param pattern Pointer to the pattern to match. Must begin with a class 229 * specifier and is repositioned to the first character after the specifier 230 * if successful. 231 * @param str String to match. 232 * @param flags Flags modifying the behavior (see fnmatch()). 233 * @return INVALID_PATTERN if the pattern doesn't start with a valid class 234 * specifier, 0 if the beginning of the matched string doesn't belong 235 * to the class, or positive number of characters matched. 207 /** 208 * 209 * @param pattern 210 * @param str 211 * @param flags 212 * @return 236 213 */ 237 214 static int _match_char_class(const char **pattern, const char *str, int flags) … … 248 225 /************** END CHARACTER CLASSES ****************/ 249 226 250 /** Reads the next collating element in the pattern, taking into account 251 * locale (if supported) and flags (see fnmatch()). 252 * 253 * @param pattern Pattern. 254 * @param flags Flags given to fnmatch(). 255 * @return Collating element on success, 256 * or COLL_ELM_INVALID if the pattern is invalid. 257 */ 258 static coll_elm_t _next_coll_elm(const char **pattern, int flags) 259 { 260 assert(pattern != NULL); 261 assert(*pattern != NULL); 262 assert(**pattern != '\0'); 263 227 /** 228 * 229 * @param pattern 230 * @param flags 231 * @return 232 */ 233 static _coll_elm_t _next_coll_elm(const char **pattern, int flags) 234 { 264 235 const char *p = *pattern; 265 236 const bool noescape = (flags & FNM_NOESCAPE) != 0; … … 286 257 if (!noescape && *p == '\\') { 287 258 p++; 288 if (*p == '\0') {289 *pattern = p;290 return COLL_ELM_INVALID;291 }292 259 } 293 260 if (pathname && *p == '/') { 294 261 return COLL_ELM_INVALID; 295 262 } 296 263 297 264 *pattern = p + 1; 298 265 return _coll_elm_char(*p); 299 266 } 300 267 301 /** Matches the beginning of the given string against a bracket expression 302 * the pattern begins with. 303 * 304 * @param pattern Pointer to the beginning of a bracket expression in a pattern. 305 * On success, the pointer is moved to the first character after the 306 * bracket expression. 307 * @param str Unmatched part of the string. 308 * @param flags Flags given to fnmatch(). 309 * @return INVALID_PATTERN if the pattern is invalid, 0 if there is no match 310 * or the number of matched characters on success. 268 /** 269 * 270 * @param pattern 271 * @param str 272 * @param flags 273 * @return 311 274 */ 312 275 static int _match_bracket_expr(const char **pattern, const char *str, int flags) … … 352 315 } 353 316 354 coll_elm_t current_elm = COLL_ELM_INVALID;317 _coll_elm_t current_elm = COLL_ELM_INVALID; 355 318 356 319 while (*p != ']') { … … 359 322 /* Range expression. */ 360 323 p++; 361 coll_elm_t end_elm = _next_coll_elm(&p, flags);324 _coll_elm_t end_elm = _next_coll_elm(&p, flags); 362 325 if (end_elm == COLL_ELM_INVALID) { 363 326 return INVALID_PATTERN; … … 394 357 } 395 358 396 /** Matches a portion of the pattern containing no asterisks (*) against 397 * the given string. 398 * 399 * @param pattern Pointer to the unmatched portion of the pattern. 400 * On success, the pointer is moved to the first asterisk, or to the 401 * terminating nul character, whichever occurs first. 402 * @param string Pointer to the input string. On success, the pointer is moved 403 * to the first character that wasn't explicitly matched. 404 * @param flags Flags given to fnmatch(). 405 * @return True if the entire subpattern matched. False otherwise. 359 /** 360 * 361 * @param pattern 362 * @param string 363 * @param flags 364 * @return 406 365 */ 407 366 static bool _partial_match(const char **pattern, const char **string, int flags) … … 497 456 } 498 457 499 /** Match string against a pattern.500 * 501 * @param pattern Pattern.502 * @param string String to match.503 * @param flags Flags given to fnmatch().504 * @return True if the string matched the pattern, false otherwise.458 /** 459 * 460 * @param pattern 461 * @param string 462 * @param flags 463 * @return 505 464 */ 506 465 static bool _full_match(const char *pattern, const char *string, int flags) … … 537 496 end = string; 538 497 } else { 539 end = strchrnul(string, pathname ? '/' : '\0');498 end= strchrnul(string, pathname ? '/' : '\0'); 540 499 } 541 500 … … 559 518 } 560 519 561 /** Transform the entire string to lowercase. 562 * 563 * @param s Input string. 564 * @return Newly allocated copy of the input string with all uppercase 565 * characters folded to their lowercase variants. 520 /** 521 * 522 * @param s 523 * @return 566 524 */ 567 525 static char *_casefold(const char *s)
Note:
See TracChangeset
for help on using the changeset viewer.
