(argz_create): Fix param type.
[glibc.git] / posix / fnmatch.c
blob08c1c9448e50aea77f9afd7a51d30ed8262eba93
1 /* Copyright (C) 1991, 1992, 1993, 1996 Free Software Foundation, Inc.
3 This library is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Library General Public License as
5 published by the Free Software Foundation; either version 2 of the
6 License, or (at your option) any later version.
8 This library 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 GNU
11 Library General Public License for more details.
13 You should have received a copy of the GNU Library General Public
14 License along with this library; see the file COPYING.LIB. If
15 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
16 Cambridge, MA 02139, USA. */
18 #ifdef HAVE_CONFIG_H
19 #include <config.h>
20 #endif
22 /* Enable GNU extensions in fnmatch.h. */
23 #ifndef _GNU_SOURCE
24 #define _GNU_SOURCE 1
25 #endif
27 #include <errno.h>
28 #include <fnmatch.h>
29 #include <ctype.h>
32 /* Comment out all this code if we are using the GNU C Library, and are not
33 actually compiling the library itself. This code is part of the GNU C
34 Library, but also included in many other GNU distributions. Compiling
35 and linking in this code is a waste when using the GNU C library
36 (especially if it is a shared library). Rather than having every GNU
37 program understand `configure --with-gnu-libc' and omit the object files,
38 it is simpler to just do this in the source for each such file. */
40 #if defined (_LIBC) || !defined (__GNU_LIBRARY__)
43 #ifndef errno
44 extern int errno;
45 #endif
47 /* Match STRING against the filename pattern PATTERN, returning zero if
48 it matches, nonzero if not. */
49 int
50 fnmatch (pattern, string, flags)
51 const char *pattern;
52 const char *string;
53 int flags;
55 register const char *p = pattern, *n = string;
56 register char c;
58 /* Note that this evalutes C many times. */
59 #define FOLD(c) ((flags & FNM_CASEFOLD) && isupper (c) ? tolower (c) : (c))
61 while ((c = *p++) != '\0')
63 c = FOLD (c);
65 switch (c)
67 case '?':
68 if (*n == '\0')
69 return FNM_NOMATCH;
70 else if ((flags & FNM_FILE_NAME) && *n == '/')
71 return FNM_NOMATCH;
72 else if ((flags & FNM_PERIOD) && *n == '.' &&
73 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
74 return FNM_NOMATCH;
75 break;
77 case '\\':
78 if (!(flags & FNM_NOESCAPE))
80 c = *p++;
81 if (c == '\0')
82 /* Trailing \ loses. */
83 return FNM_NOMATCH;
84 c = FOLD (c);
86 if (FOLD (*n) != c)
87 return FNM_NOMATCH;
88 break;
90 case '*':
91 if ((flags & FNM_PERIOD) && *n == '.' &&
92 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
93 return FNM_NOMATCH;
95 for (c = *p++; c == '?' || c == '*'; c = *p++, ++n)
96 if (((flags & FNM_FILE_NAME) && *n == '/') ||
97 (c == '?' && *n == '\0'))
98 return FNM_NOMATCH;
100 if (c == '\0')
101 return 0;
104 char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c;
105 c1 = FOLD (c1);
106 for (--p; *n != '\0'; ++n)
107 if ((c == '[' || FOLD (*n) == c1) &&
108 fnmatch (p, n, flags & ~FNM_PERIOD) == 0)
109 return 0;
110 return FNM_NOMATCH;
113 case '[':
115 /* Nonzero if the sense of the character class is inverted. */
116 register int not;
118 if (*n == '\0')
119 return FNM_NOMATCH;
121 if ((flags & FNM_PERIOD) && *n == '.' &&
122 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
123 return FNM_NOMATCH;
125 not = (*p == '!' || *p == '^');
126 if (not)
127 ++p;
129 c = *p++;
130 for (;;)
132 register char cstart = c, cend = c;
134 if (!(flags & FNM_NOESCAPE) && c == '\\')
136 if (*p == '\0')
137 return FNM_NOMATCH;
138 cstart = cend = *p++;
141 cstart = cend = FOLD (cstart);
143 if (c == '\0')
144 /* [ (unterminated) loses. */
145 return FNM_NOMATCH;
147 c = *p++;
148 c = FOLD (c);
150 if ((flags & FNM_FILE_NAME) && c == '/')
151 /* [/] can never match. */
152 return FNM_NOMATCH;
154 if (c == '-' && *p != ']')
156 cend = *p++;
157 if (!(flags & FNM_NOESCAPE) && cend == '\\')
158 cend = *p++;
159 if (cend == '\0')
160 return FNM_NOMATCH;
161 cend = FOLD (cend);
163 c = *p++;
166 if (FOLD (*n) >= cstart && FOLD (*n) <= cend)
167 goto matched;
169 if (c == ']')
170 break;
172 if (!not)
173 return FNM_NOMATCH;
174 break;
176 matched:;
177 /* Skip the rest of the [...] that already matched. */
178 while (c != ']')
180 if (c == '\0')
181 /* [... (unterminated) loses. */
182 return FNM_NOMATCH;
184 c = *p++;
185 if (!(flags & FNM_NOESCAPE) && c == '\\')
187 if (*p == '\0')
188 return FNM_NOMATCH;
189 /* XXX 1003.2d11 is unclear if this is right. */
190 ++p;
193 if (not)
194 return FNM_NOMATCH;
196 break;
198 default:
199 if (c != FOLD (*n))
200 return FNM_NOMATCH;
203 ++n;
206 if (*n == '\0')
207 return 0;
209 if ((flags & FNM_LEADING_DIR) && *n == '/')
210 /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */
211 return 0;
213 return FNM_NOMATCH;
216 #endif /* _LIBC or not __GNU_LIBRARY__. */