123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553 |
- /*++
- Copyright (c) 2015 Minoca Corp.
- This file is licensed under the terms of the GNU General Public License
- version 3. Alternative licensing terms are available. Contact
- info@minocacorp.com for details. See the LICENSE file at the root of this
- project for complete licensing information.
- Module Name:
- fnmatch.c
- Abstract:
- This module implements the fnmatch function, which is used to match
- patterns.
- Author:
- Evan Green 10-Feb-2015
- Environment:
- User Mode C Library
- --*/
- //
- // ------------------------------------------------------------------- Includes
- //
- #include "libcp.h"
- #include <assert.h>
- #include <ctype.h>
- #include <fnmatch.h>
- #include <string.h>
- //
- // ---------------------------------------------------------------- Definitions
- //
- //
- // ------------------------------------------------------ Data Type Definitions
- //
- //
- // ----------------------------------------------- Internal Function Prototypes
- //
- int
- ClpFnMatch (
- const char *Pattern,
- const char *String,
- const char *Start,
- int Flags
- );
- INT
- ClpFnMatchPatternSet (
- const char *Pattern,
- char Character,
- int Flags,
- const char **PatternAfterSet
- );
- //
- // -------------------------------------------------------------------- Globals
- //
- //
- // ------------------------------------------------------------------ Functions
- //
- LIBC_API
- int
- fnmatch (
- const char *Pattern,
- const char *String,
- int Flags
- )
- /*++
- Routine Description:
- This routine matches patterns as described by POSIX in the shell grammar
- sections of "Patterns Matching a Single Character", "Patterns Matching
- Multiple Characters", and "Patterns Used for Filename Expansion".
- Arguments:
- Pattern - Supplies a pointer to the null terminated pattern string.
- String - Supplies a pointer to the null terminated string to match against.
- Flags - Supplies a bitfield of flags governing the behavior of the matching
- function. See FNM_* definitions.
- Return Value:
- 0 if the pattern matches.
- FNM_NOMATCH if the pattern does not match.
- -1 on error.
- --*/
- {
- return ClpFnMatch(Pattern, String, String, Flags);
- }
- //
- // --------------------------------------------------------- Internal Functions
- //
- int
- ClpFnMatch (
- const char *Pattern,
- const char *String,
- const char *Start,
- int Flags
- )
- /*++
- Routine Description:
- This routine represents the inner worker for the fnmatch function, which
- may be called recursively.
- Arguments:
- Pattern - Supplies a pointer to the null terminated pattern string.
- String - Supplies a pointer to the null terminated string to match against.
- Start - Supplies the original beginning of the string to match against.
- Flags - Supplies a bitfield of flags governing the behavior of the matching
- function. See FNM_* definitions.
- Return Value:
- 0 if the pattern matches.
- FNM_NOMATCH if the pattern does not match.
- -1 on error.
- --*/
- {
- const char *PatternAfterSet;
- CHAR PatternCharacter;
- INT SetResult;
- CHAR StringCharacter;
- while (TRUE) {
- PatternCharacter = *Pattern;
- Pattern += 1;
- StringCharacter = *String;
- switch (PatternCharacter) {
- case '\0':
- if (((Flags & FNM_LEADING_DIR) != 0) && (StringCharacter == '/')) {
- return 0;
- }
- if (StringCharacter == PatternCharacter) {
- return 0;
- }
- return FNM_NOMATCH;
- //
- // Question mark matches any character.
- //
- case '?':
- if (StringCharacter == '\0') {
- return FNM_NOMATCH;
- }
- //
- // If the pathname flag is set, don't match wildcards against
- // slashes.
- //
- if ((StringCharacter == '/') && ((Flags & FNM_PATHNAME) != 0)) {
- return FNM_NOMATCH;
- }
- //
- // If the period flag is set, then a leading period is matched
- // explicitly. If the pathname flag is set, then leading means at
- // the beginning of a path component. Otherwise, leading means at
- // the beginning of the string.
- //
- if ((StringCharacter == '.') && ((Flags & FNM_PERIOD) != 0)) {
- if (String == Start) {
- return FNM_NOMATCH;
- } else if (((Flags & FNM_PATHNAME) != 0) &&
- (*(String - 1) == '/')) {
- return FNM_NOMATCH;
- }
- }
- String += 1;
- break;
- //
- // Asterisks match a bunch of any character.
- //
- case '*':
- //
- // Collapse multiple asterisks in a row.
- //
- while (*Pattern == '*') {
- Pattern += 1;
- }
- //
- // Again, if the period flag is set, then leading periods don't
- // match against wildcards.
- //
- if ((StringCharacter == '.') && ((Flags & FNM_PERIOD) != 0)) {
- if (String == Start) {
- return FNM_NOMATCH;
- } else if (((Flags & FNM_PATHNAME) != 0) &&
- (*(String - 1) == '/')) {
- return FNM_NOMATCH;
- }
- }
- //
- // Specially handle an asterisk at the end of the pattern.
- //
- if (*Pattern == '\0') {
- if ((Flags & FNM_PATHNAME) == 0) {
- return 0;
- }
- //
- // The pathname flag is set. If there are no more path
- // components, or the leading directory flag is set, then this
- // matches. Otherwise, it doesn't.
- //
- if (((Flags & FNM_LEADING_DIR) != 0) ||
- (strchr(String, '/') == NULL)) {
- return 0;
- }
- return FNM_NOMATCH;
- //
- // If the next pattern character is a path separator and the
- // pathname flag is set, then the star only matches up to the next
- // slash.
- //
- } else if ((*Pattern == '/') && ((Flags & FNM_PATHNAME) != 0)) {
- String = strchr(String, '/');
- if (String == NULL) {
- return FNM_NOMATCH;
- }
- //
- // The asterisk matched up to the next slash.
- //
- break;
- }
- //
- // Detrermine how much of the string to chew through with the
- // asterisk.
- //
- while (StringCharacter != '\0') {
- if (ClpFnMatch(Pattern, String, Start, Flags) == 0) {
- return 0;
- }
- StringCharacter = *String;
- if ((StringCharacter == '/') && ((Flags & FNM_PATHNAME) != 0)) {
- break;
- }
- String += 1;
- }
- return FNM_NOMATCH;
- //
- // An open bracket matches a set of characters or a character class.
- //
- case '[':
- if (StringCharacter == '\0') {
- return FNM_NOMATCH;
- }
- //
- // Slashes don't match wildcards if the pathname flag is set.
- //
- if ((StringCharacter == '/') && ((Flags & FNM_PATHNAME) != 0)) {
- return FNM_NOMATCH;
- }
- //
- // Leading periods don't match wildcards if the period flag is set.
- //
- if ((StringCharacter == '.') && ((Flags & FNM_PERIOD) != 0)) {
- if (String == Start) {
- return FNM_NOMATCH;
- } else if (((Flags & FNM_PATHNAME) != 0) &&
- (*(String - 1) == '/')) {
- return FNM_NOMATCH;
- }
- }
- SetResult = ClpFnMatchPatternSet(Pattern,
- StringCharacter,
- Flags,
- &PatternAfterSet);
- if (SetResult == 0) {
- Pattern = PatternAfterSet;
- String += 1;
- break;
- } else if (SetResult == FNM_NOMATCH) {
- return FNM_NOMATCH;
- }
- //
- // Fall through if the pattern set had an error and treat it as a
- // normal character. The lack of a break is intentional.
- //
- default:
- //
- // This is the normal character area. If it's a backslash, then the
- // normal character is actually the next character (unless
- // escaping was disabled).
- //
- if (PatternCharacter == '\\') {
- if ((Flags & FNM_NOESCAPE) == 0) {
- PatternCharacter = *Pattern;
- Pattern += 1;
- }
- }
- if (PatternCharacter != StringCharacter) {
- if (((Flags & FNM_CASEFOLD) == 0) ||
- (tolower(PatternCharacter) != tolower(StringCharacter))) {
- return FNM_NOMATCH;
- }
- }
- String += 1;
- break;
- }
- }
- //
- // Execution never gets here.
- //
- assert(FALSE);
- return -1;
- }
- INT
- ClpFnMatchPatternSet (
- const char *Pattern,
- char Character,
- int Flags,
- const char **PatternAfterSet
- )
- /*++
- Routine Description:
- This routine matches against a character against a character set.
- Arguments:
- Pattern - Supplies a pointer to the null terminated pattern string.
- Character - Supplies the character to match against.
- Flags - Supplies a bitfield of flags governing the behavior of the matching
- function. See FNM_* definitions.
- PatternAfterSet - Supplies a pointer where the advanced pattern string will
- be returned on success.
- Return Value:
- 0 if the pattern matches.
- FNM_NOMATCH if the pattern does not match.
- -1 on error.
- --*/
- {
- char EndCharacter;
- BOOL Found;
- BOOL Negated;
- char PatternCharacter;
- const char *PatternStart;
- //
- // Treat a ! or a ^ as a negation of the character set.
- //
- Negated = FALSE;
- if ((*Pattern == '!') || (*Pattern == '^')) {
- Negated = TRUE;
- Pattern += 1;
- }
- if ((Flags & FNM_CASEFOLD) != 0) {
- Character = tolower(Character);
- }
- PatternStart = Pattern;
- Found = FALSE;
- while (TRUE) {
- //
- // Look for the closing bracket, and stop looping once found. If the
- // closing bracket is the very first character, it is treated as a
- // normal character.
- //
- if ((*Pattern == ']') && (Pattern > PatternStart)) {
- Pattern += 1;
- break;
- //
- // This wasn't a valid pattern set if the string ended before a closing
- // bracket (ie. [abc).
- //
- } else if (*Pattern == '\0') {
- return -1;
- //
- // If the pathname flag is set, slashes had better not be in the
- // pattern.
- //
- } else if ((*Pattern == '/') && ((Flags & FNM_PATHNAME) != 0)) {
- return FNM_NOMATCH;
- //
- // Backslash escapes characters (unless disabled).
- //
- } else if ((*Pattern == '\\') && ((Flags & FNM_NOESCAPE) == 0)) {
- Pattern += 1;
- }
- PatternCharacter = *Pattern;
- Pattern += 1;
- if ((Flags & FNM_CASEFOLD) != 0) {
- PatternCharacter = tolower(PatternCharacter);
- }
- //
- // Handle a range, like a-f.
- //
- if ((*Pattern == '-') && (*(Pattern + 1) != '\0') &&
- (*(Pattern + 1) != ']')) {
- Pattern += 1;
- if ((*Pattern == '\\') && ((Flags & FNM_NOESCAPE) == 0) &&
- (*(Pattern + 1) != '\0')) {
- Pattern += 1;
- }
- EndCharacter = *Pattern;
- Pattern += 1;
- if (EndCharacter == '\0') {
- return -1;
- }
- if ((Flags & FNM_CASEFOLD) != 0) {
- EndCharacter = tolower(EndCharacter);
- }
- if ((Character >= PatternCharacter) &&
- (Character <= EndCharacter)) {
- Found = TRUE;
- }
- //
- // Otherwise, just look to see if this particular character matches.
- //
- } else if (Character == PatternCharacter) {
- Found = TRUE;
- }
- }
- *PatternAfterSet = Pattern;
- if (Negated != FALSE) {
- if (Found != FALSE) {
- Found = FALSE;
- } else {
- Found = TRUE;
- }
- }
- if (Found != FALSE) {
- return 0;
- }
- return FNM_NOMATCH;
- }
|