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)
39 register const char *p = pattern, *n = string;
40 register unsigned char c;
42 /* Note that this evalutes C many times. */
43 #define FOLD(c) ((flags & FNM_CASEFOLD) && isupper(c) ? tolower(c) : (c))
46 flags |= FNM_CASEFOLD;
49 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)) {
68 if (FOLD((unsigned char)*n) != c)
73 if ((flags & FNM_PERIOD) && *n == '.' &&
74 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
77 for (c = *p++; c == '?' || c == '*'; c = *p++, ++n)
78 if (((flags & FNM_FILE_NAME) && *n == '/') ||
79 (c == '?' && *n == '\0'))
86 unsigned char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c;
88 for (--p; *n != '\0'; ++n)
89 if ((c == '[' || FOLD((unsigned char)*n) == c1) &&
90 fnmatch(p, n, flags & ~FNM_PERIOD) == 0)
97 /* Nonzero if the sense of the character class is inverted. */
103 if ((flags & FNM_PERIOD) && *n == '.' &&
104 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
107 not = (*p == '!' || *p == '^');
113 register unsigned char cstart = c, cend = c;
115 if (!(flags & FNM_NOESCAPE) && c == '\\')
116 cstart = cend = *p++;
118 cstart = cend = FOLD(cstart);
121 /* [ (unterminated) loses. */
127 if ((flags & FNM_FILE_NAME) && c == '/')
128 /* [/] can never match. */
131 if (c == '-' && *p != ']') {
133 if (!(flags & FNM_NOESCAPE) && cend == '\\')
142 if (FOLD((unsigned char)*n) >= cstart
143 && FOLD((unsigned char)*n) <= cend)
154 /* Skip the rest of the [...] that already matched. */
157 /* [... (unterminated) loses. */
161 if (!(flags & FNM_NOESCAPE) && c == '\\')
162 /* XXX 1003.2d11 is unclear if this is right. */
171 if (c != FOLD((unsigned char)*n))
181 if ((flags & FNM_LEADING_DIR) && *n == '/')
182 /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */