1 /* Copyright (C) 1991, 1992, 1993 Free Software Foundation, Inc.
3 NOTE: The canonical source of this file is maintained with the GNU C Library.
4 Bugs can be reported to bug-glibc@prep.ai.mit.edu.
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
29 /* Comment out all this code if we are using the GNU C Library, and are not
30 actually compiling the library itself. This code is part of the GNU C
31 Library, but also included in many other GNU distributions. Compiling
32 and linking in this code is a waste when using the GNU C library
33 (especially if it is a shared library). Rather than having every GNU
34 program understand `configure --with-gnu-libc' and omit the object files,
35 it is simpler to just do this in the source for each such file. */
37 #if defined (_LIBC) || !defined (__GNU_LIBRARY__)
44 /* Match STRING against the filename pattern PATTERN, returning zero if
45 it matches, nonzero if not. */
47 fnmatch (pattern, string, flags)
52 register const char *p = pattern, *n = string;
55 /* Note that this evalutes C many times. */
56 #define FOLD(c) ((flags & FNM_CASEFOLD) && isupper (c) ? tolower (c) : (c))
58 while ((c = *p++) != '\0')
67 else if ((flags & FNM_FILE_NAME) && *n == '/')
69 else if ((flags & FNM_PERIOD) && *n == '.' &&
70 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
75 if (!(flags & FNM_NOESCAPE))
85 if ((flags & FNM_PERIOD) && *n == '.' &&
86 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
89 for (c = *p++; c == '?' || c == '*'; c = *p++, ++n)
90 if (((flags & FNM_FILE_NAME) && *n == '/') ||
91 (c == '?' && *n == '\0'))
98 char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c;
100 for (--p; *n != '\0'; ++n)
101 if ((c == '[' || FOLD (*n) == c1) &&
102 fnmatch (p, n, flags & ~FNM_PERIOD) == 0)
109 /* Nonzero if the sense of the character class is inverted. */
115 if ((flags & FNM_PERIOD) && *n == '.' &&
116 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
119 not = (*p == '!' || *p == '^');
126 register char cstart = c, cend = c;
128 if (!(flags & FNM_NOESCAPE) && c == '\\')
129 cstart = cend = *p++;
131 cstart = cend = FOLD (cstart);
134 /* [ (unterminated) loses. */
140 if ((flags & FNM_FILE_NAME) && c == '/')
141 /* [/] can never match. */
144 if (c == '-' && *p != ']')
147 if (!(flags & FNM_NOESCAPE) && cend == '\\')
156 if (FOLD (*n) >= cstart && FOLD (*n) <= cend)
167 /* Skip the rest of the [...] that already matched. */
171 /* [... (unterminated) loses. */
175 if (!(flags & FNM_NOESCAPE) && c == '\\')
176 /* XXX 1003.2d11 is unclear if this is right. */
195 if ((flags & FNM_LEADING_DIR) && *n == '/')
196 /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */
202 #endif /* _LIBC or not __GNU_LIBRARY__. */