2 * $Id: fnmatch.c,v 0.0.0.1 2003/08/21
5 * AUTHOR: xxx, Petr Smolik petr.smolik@wo.cz
7 * ORTE - OCERA Real-Time Ethernet http://www.ocera.org/
8 * --------------------------------------------------------------------
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
24 /* Match STRING against the filename pattern PATTERN, returning zero if
25 it matches, nonzero if not. */
27 fnmatch (const char *pattern,const char *string,int flags ) {
28 register const char *p = pattern, *n = string;
29 register unsigned char c;
31 /* Note that this evalutes C many times. */
32 #define FOLD(c) ((flags & FNM_CASEFOLD) && isupper (c) ? tolower (c) : (c))
35 flags |= FNM_CASEFOLD;
38 while ((c = *p++) != '\0')
46 else if ((flags & FNM_FILE_NAME) && *n == '/')
48 else if ((flags & FNM_PERIOD) && *n == '.' &&
49 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
54 if (!(flags & FNM_NOESCAPE))
59 if (FOLD ((unsigned char)*n) != c)
64 if ((flags & FNM_PERIOD) && *n == '.' &&
65 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
68 for (c = *p++; c == '?' || c == '*'; c = *p++, ++n)
69 if (((flags & FNM_FILE_NAME) && *n == '/') ||
70 (c == '?' && *n == '\0'))
77 unsigned char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c;
79 for (--p; *n != '\0'; ++n)
80 if ((c == '[' || FOLD ((unsigned char)*n) == c1) &&
81 fnmatch (p, n, flags & ~FNM_PERIOD) == 0)
88 /* Nonzero if the sense of the character class is inverted. */
94 if ((flags & FNM_PERIOD) && *n == '.' &&
95 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
98 not = (*p == '!' || *p == '^');
104 register unsigned char cstart = c, cend = c;
106 if (!(flags & FNM_NOESCAPE) && c == '\\')
107 cstart = cend = *p++;
109 cstart = cend = FOLD (cstart);
112 /* [ (unterminated) loses. */
118 if ((flags & FNM_FILE_NAME) && c == '/')
119 /* [/] can never match. */
122 if (c == '-' && *p != ']')
125 if (!(flags & FNM_NOESCAPE) && cend == '\\')
134 if (FOLD ((unsigned char)*n) >= cstart
135 && FOLD ((unsigned char)*n) <= cend)
146 /* Skip the rest of the [...] that already matched. */
149 /* [... (unterminated) loses. */
153 if (!(flags & FNM_NOESCAPE) && c == '\\')
154 /* XXX 1003.2d11 is unclear if this is right. */
163 if (c != FOLD ((unsigned char)*n))
173 if ((flags & FNM_LEADING_DIR) && *n == '/')
174 /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */