2 * $Id: fnmatch.c,v 0.0.0.1 2003/08/21
6 * -------------------------------------------------------------------
8 * Open Real-Time Ethernet
10 * Copyright (C) 2001-2006
11 * Department of Control Engineering FEE CTU Prague, Czech Republic
12 * http://dce.felk.cvut.cz
13 * http://www.ocera.org
15 * Author: Petr Smolik petr@smoliku.cz
17 * Project Responsible: Zdenek Hanzalek
18 * --------------------------------------------------------------------
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
34 /* Match STRING against the filename pattern PATTERN, returning zero if
35 it matches, nonzero if not. */
37 fnmatch (const char *pattern,const char *string,int flags ) {
38 register const char *p = pattern, *n = string;
39 register unsigned char c;
41 /* Note that this evalutes C many times. */
42 #define FOLD(c) ((flags & FNM_CASEFOLD) && isupper (c) ? tolower (c) : (c))
45 flags |= FNM_CASEFOLD;
48 while ((c = *p++) != '\0')
56 else if ((flags & FNM_FILE_NAME) && *n == '/')
58 else if ((flags & FNM_PERIOD) && *n == '.' &&
59 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
64 if (!(flags & FNM_NOESCAPE))
69 if (FOLD ((unsigned char)*n) != c)
74 if ((flags & FNM_PERIOD) && *n == '.' &&
75 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
78 for (c = *p++; c == '?' || c == '*'; c = *p++, ++n)
79 if (((flags & FNM_FILE_NAME) && *n == '/') ||
80 (c == '?' && *n == '\0'))
87 unsigned char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c;
89 for (--p; *n != '\0'; ++n)
90 if ((c == '[' || FOLD ((unsigned char)*n) == c1) &&
91 fnmatch (p, n, flags & ~FNM_PERIOD) == 0)
98 /* Nonzero if the sense of the character class is inverted. */
104 if ((flags & FNM_PERIOD) && *n == '.' &&
105 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
108 not = (*p == '!' || *p == '^');
114 register unsigned char cstart = c, cend = c;
116 if (!(flags & FNM_NOESCAPE) && c == '\\')
117 cstart = cend = *p++;
119 cstart = cend = FOLD (cstart);
122 /* [ (unterminated) loses. */
128 if ((flags & FNM_FILE_NAME) && c == '/')
129 /* [/] can never match. */
132 if (c == '-' && *p != ']')
135 if (!(flags & FNM_NOESCAPE) && cend == '\\')
144 if (FOLD ((unsigned char)*n) >= cstart
145 && FOLD ((unsigned char)*n) <= cend)
156 /* Skip the rest of the [...] that already matched. */
159 /* [... (unterminated) loses. */
163 if (!(flags & FNM_NOESCAPE) && c == '\\')
164 /* XXX 1003.2d11 is unclear if this is right. */
173 if (c != FOLD ((unsigned char)*n))
183 if ((flags & FNM_LEADING_DIR) && *n == '/')
184 /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */