1 /* Copyright (C) 1991-1993, 1996-1999, 2000 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with this library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 /* Match STRING against the filename pattern PATTERN, returning zero if
20 it matches, nonzero if not. */
21 static int FCT (const CHAR *pattern, const CHAR *string,
22 int no_leading_period, int flags) internal_function;
26 FCT (pattern, string, no_leading_period, flags)
29 int no_leading_period;
32 register const CHAR *p = pattern, *n = string;
35 while ((c = *p++) != L('\0'))
44 else if (*n == L('/') && (flags & FNM_FILE_NAME))
46 else if (*n == L('.') && no_leading_period
48 || (n[-1] == L('/') && (flags & FNM_FILE_NAME))))
53 if (!(flags & FNM_NOESCAPE))
57 /* Trailing \ loses. */
61 if (FOLD ((UCHAR) *n) != c)
66 if (*n == L('.') && no_leading_period
68 || (n[-1] == L('/') && (flags & FNM_FILE_NAME))))
71 for (c = *p++; c == L('?') || c == L('*'); c = *p++)
73 if (*n == L('/') && (flags & FNM_FILE_NAME))
74 /* A slash does not match a wildcard under FNM_FILE_NAME. */
78 /* A ? needs to match one character. */
80 /* There isn't another character; no match. */
83 /* One character of the string is consumed in matching
84 this ? wildcard, so *??? won't match if there are
85 less than three characters. */
91 /* The wildcard(s) is/are the last element of the pattern.
92 If the name is a file name and contains another slash
93 this does mean it cannot match. */
94 return ((flags & FNM_FILE_NAME) && STRCHR (n, L('/')) != NULL
100 endp = STRCHRNUL (n, (flags & FNM_FILE_NAME) ? L('/') : L('\0'));
104 int flags2 = ((flags & FNM_FILE_NAME)
105 ? flags : (flags & ~FNM_PERIOD));
107 for (--p; n < endp; ++n)
108 if (FCT (p, n, (no_leading_period
111 && (flags & FNM_FILE_NAME)))),
115 else if (c == L('/') && (flags & FNM_FILE_NAME))
117 while (*n != L('\0') && *n != L('/'))
120 && (FCT (p, n + 1, flags & FNM_PERIOD, flags) == 0))
125 int flags2 = ((flags & FNM_FILE_NAME)
126 ? flags : (flags & ~FNM_PERIOD));
128 if (c == L('\\') && !(flags & FNM_NOESCAPE))
131 for (--p; n < endp; ++n)
132 if (FOLD ((UCHAR) *n) == c
133 && (FCT (p, n, (no_leading_period
136 && (flags & FNM_FILE_NAME)))),
142 /* If we come here no match is possible with the wildcard. */
147 /* Nonzero if the sense of the character class is inverted. */
148 static int posixly_correct;
152 if (posixly_correct == 0)
153 posixly_correct = getenv ("POSIXLY_CORRECT") != NULL ? 1 : -1;
158 if (*n == L('.') && no_leading_period
160 || (n[-1] == L('/') && (flags & FNM_FILE_NAME))))
163 if (*n == L('/') && (flags & FNM_FILE_NAME))
164 /* `/' cannot be matched. */
167 not = (*p == L('!') || (posixly_correct < 0 && *p == L('^')));
174 UCHAR fn = FOLD ((UCHAR) *n);
176 if (!(flags & FNM_NOESCAPE) && c == L('\\'))
180 c = FOLD ((UCHAR) *p);
186 else if (c == L('[') && *p == L(':'))
188 /* Leave room for the null. */
189 CHAR str[CHAR_CLASS_MAX_LENGTH + 1];
191 # if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
194 const CHAR *startp = p;
198 if (c1 == CHAR_CLASS_MAX_LENGTH)
199 /* The name is too long and therefore the pattern
204 if (c == L(':') && p[1] == L(']'))
209 if (c < L('a') || c >= L('z'))
211 /* This cannot possibly be a character class name.
212 Match it as a normal range. */
221 # if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
222 wt = IS_CHAR_CLASS (str);
224 /* Invalid character class name. */
227 if (ISWCTYPE (BTOWC ((UCHAR) *n), wt))
230 if ((STREQ (str, L("alnum")) && ISALNUM ((UCHAR) *n))
231 || (STREQ (str, L("alpha")) && ISALPHA ((UCHAR) *n))
232 || (STREQ (str, L("blank")) && ISBLANK ((UCHAR) *n))
233 || (STREQ (str, L("cntrl")) && ISCNTRL ((UCHAR) *n))
234 || (STREQ (str, L("digit")) && ISDIGIT ((UCHAR) *n))
235 || (STREQ (str, L("graph")) && ISGRAPH ((UCHAR) *n))
236 || (STREQ (str, L("lower")) && ISLOWER ((UCHAR) *n))
237 || (STREQ (str, L("print")) && ISPRINT ((UCHAR) *n))
238 || (STREQ (str, L("punct")) && ISPUNCT ((UCHAR) *n))
239 || (STREQ (str, L("space")) && ISSPACE ((UCHAR) *n))
240 || (STREQ (str, L("upper")) && ISUPPER ((UCHAR) *n))
241 || (STREQ (str, L("xdigit")) && ISXDIGIT ((UCHAR) *n)))
245 else if (c == L('\0'))
246 /* [ (unterminated) loses. */
257 if (c == L('-') && *p != L(']'))
261 if (!(flags & FNM_NOESCAPE) && cend == L('\\'))
266 if (cold <= fn && fn <= FOLD (cend))
282 /* Skip the rest of the [...] that already matched. */
286 /* [... (unterminated) loses. */
290 if (!(flags & FNM_NOESCAPE) && c == L('\\'))
294 /* XXX 1003.2d11 is unclear if this is right. */
297 else if (c == L('[') && *p == L(':'))
302 while (*p != L(':') || p[1] == L(']'));
313 if (c != FOLD ((UCHAR) *n))
323 if ((flags & FNM_LEADING_DIR) && *n == L('/'))
324 /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */