* locate/frcode.c, locate/code.c, locate/bigram.c: change return
[findutils.git] / lib / fnmatch.c
blob7ec417dfed221f1b4aa5780c7b75538f856ef0c3
1 /* Copyright (C) 1991, 1992, 1993, 1996, 1997 Free Software Foundation, Inc.
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software Foundation,
15 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
17 #if HAVE_CONFIG_H
18 # include <config.h>
19 #endif
21 /* Enable GNU extensions in fnmatch.h. */
22 #ifndef _GNU_SOURCE
23 # define _GNU_SOURCE 1
24 #endif
26 #include <errno.h>
27 #include <fnmatch.h>
28 #include <ctype.h>
31 /* Comment out all this code if we are using the GNU C Library, and are not
32 actually compiling the library itself. This code is part of the GNU C
33 Library, but also included in many other GNU distributions. Compiling
34 and linking in this code is a waste when using the GNU C library
35 (especially if it is a shared library). Rather than having every GNU
36 program understand `configure --with-gnu-libc' and omit the object files,
37 it is simpler to just do this in the source for each such file. */
39 #if defined _LIBC || !defined __GNU_LIBRARY__
42 # if defined STDC_HEADERS || !defined isascii
43 # define ISASCII(c) 1
44 # else
45 # define ISASCII(c) isascii(c)
46 # endif
48 # define ISUPPER(c) (ISASCII (c) && isupper (c))
51 # ifndef errno
52 extern int errno;
53 # endif
55 /* Match STRING against the filename pattern PATTERN, returning zero if
56 it matches, nonzero if not. */
57 int
58 fnmatch (pattern, string, flags)
59 const char *pattern;
60 const char *string;
61 int flags;
63 register const char *p = pattern, *n = string;
64 register char c;
66 /* Note that this evaluates C many times. */
67 # define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? tolower (c) : (c))
69 while ((c = *p++) != '\0')
71 c = FOLD (c);
73 switch (c)
75 case '?':
76 if (*n == '\0')
77 return FNM_NOMATCH;
78 else if ((flags & FNM_FILE_NAME) && *n == '/')
79 return FNM_NOMATCH;
80 else if ((flags & FNM_PERIOD) && *n == '.' &&
81 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
82 return FNM_NOMATCH;
83 break;
85 case '\\':
86 if (!(flags & FNM_NOESCAPE))
88 c = *p++;
89 if (c == '\0')
90 /* Trailing \ loses. */
91 return FNM_NOMATCH;
92 c = FOLD (c);
94 if (FOLD (*n) != c)
95 return FNM_NOMATCH;
96 break;
98 case '*':
99 if ((flags & FNM_PERIOD) && *n == '.' &&
100 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
101 return FNM_NOMATCH;
103 for (c = *p++; c == '?' || c == '*'; c = *p++)
105 if ((flags & FNM_FILE_NAME) && *n == '/')
106 /* A slash does not match a wildcard under FNM_FILE_NAME. */
107 return FNM_NOMATCH;
108 else if (c == '?')
110 /* A ? needs to match one character. */
111 if (*n == '\0')
112 /* There isn't another character; no match. */
113 return FNM_NOMATCH;
114 else
115 /* One character of the string is consumed in matching
116 this ? wildcard, so *??? won't match if there are
117 less than three characters. */
118 ++n;
122 if (c == '\0')
123 return 0;
126 char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c;
127 c1 = FOLD (c1);
128 for (--p; *n != '\0'; ++n)
129 if ((c == '[' || FOLD (*n) == c1) &&
130 fnmatch (p, n, flags & ~FNM_PERIOD) == 0)
131 return 0;
132 return FNM_NOMATCH;
135 case '[':
137 /* Nonzero if the sense of the character class is inverted. */
138 register int not;
140 if (*n == '\0')
141 return FNM_NOMATCH;
143 if ((flags & FNM_PERIOD) && *n == '.' &&
144 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
145 return FNM_NOMATCH;
147 not = (*p == '!' || *p == '^');
148 if (not)
149 ++p;
151 c = *p++;
152 for (;;)
154 register char cstart = c, cend = c;
156 if (!(flags & FNM_NOESCAPE) && c == '\\')
158 if (*p == '\0')
159 return FNM_NOMATCH;
160 cstart = cend = *p++;
163 cstart = cend = FOLD (cstart);
165 if (c == '\0')
166 /* [ (unterminated) loses. */
167 return FNM_NOMATCH;
169 c = *p++;
170 c = FOLD (c);
172 if ((flags & FNM_FILE_NAME) && c == '/')
173 /* [/] can never match. */
174 return FNM_NOMATCH;
176 if (c == '-' && *p != ']')
178 cend = *p++;
179 if (!(flags & FNM_NOESCAPE) && cend == '\\')
180 cend = *p++;
181 if (cend == '\0')
182 return FNM_NOMATCH;
183 cend = FOLD (cend);
185 c = *p++;
188 if (FOLD (*n) >= cstart && FOLD (*n) <= cend)
189 goto matched;
191 if (c == ']')
192 break;
194 if (!not)
195 return FNM_NOMATCH;
196 break;
198 matched:;
199 /* Skip the rest of the [...] that already matched. */
200 while (c != ']')
202 if (c == '\0')
203 /* [... (unterminated) loses. */
204 return FNM_NOMATCH;
206 c = *p++;
207 if (!(flags & FNM_NOESCAPE) && c == '\\')
209 if (*p == '\0')
210 return FNM_NOMATCH;
211 /* XXX 1003.2d11 is unclear if this is right. */
212 ++p;
215 if (not)
216 return FNM_NOMATCH;
218 break;
220 default:
221 if (c != FOLD (*n))
222 return FNM_NOMATCH;
225 ++n;
228 if (*n == '\0')
229 return 0;
231 if ((flags & FNM_LEADING_DIR) && *n == '/')
232 /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */
233 return 0;
235 return FNM_NOMATCH;
237 # undef FOLD
240 #endif /* _LIBC or not __GNU_LIBRARY__. */