Initial commit.
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmtar / compat / fnmatch.c
blob936f21ec1835e711f257e37dfff67a9674c8fcdc
1 /* $OpenBSD: fnmatch.c,v 1.6 1998/03/19 00:29:59 millert Exp $ */
3 /*
4 * Copyright (c) 1989, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Guido van Rossum.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
39 #if defined(LIBC_SCCS) && !defined(lint)
40 #if 0
41 static char sccsid[] = "@(#)fnmatch.c 8.2 (Berkeley) 4/16/94";
42 #else
43 static char rcsid[] = "$OpenBSD: fnmatch.c,v 1.6 1998/03/19 00:29:59 millert Exp $";
44 #endif
45 #endif /* LIBC_SCCS and not lint */
48 * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
49 * Compares a filename or pathname to a pattern.
52 #include <libtar/config.h>
54 #include <stdio.h>
56 #ifdef STDC_HEADERS
57 # include <string.h>
58 #endif
60 #ifdef HAVE_CTYPE_H
61 # include <ctype.h>
62 #endif
64 #include <libtar/compat.h>
67 #define EOS '\0'
69 #define RANGE_MATCH 1
70 #define RANGE_NOMATCH 0
71 #define RANGE_ERROR (-1)
73 static int rangematch (const char *, char, int, char **);
75 int
76 fnmatch(pattern, string, flags)
77 const char *pattern, *string;
78 int flags;
80 const char *stringstart;
81 char *newp;
82 char c, test;
84 for (stringstart = string;;)
85 switch (c = *pattern++) {
86 case EOS:
87 if ((flags & FNM_LEADING_DIR) && *string == '/')
88 return (0);
89 return (*string == EOS ? 0 : FNM_NOMATCH);
90 case '?':
91 if (*string == EOS)
92 return (FNM_NOMATCH);
93 if (*string == '/' && (flags & FNM_PATHNAME))
94 return (FNM_NOMATCH);
95 if (*string == '.' && (flags & FNM_PERIOD) &&
96 (string == stringstart ||
97 ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
98 return (FNM_NOMATCH);
99 ++string;
100 break;
101 case '*':
102 c = *pattern;
103 /* Collapse multiple stars. */
104 while (c == '*')
105 c = *++pattern;
107 if (*string == '.' && (flags & FNM_PERIOD) &&
108 (string == stringstart ||
109 ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
110 return (FNM_NOMATCH);
112 /* Optimize for pattern with * at end or before /. */
113 if (c == EOS) {
114 if (flags & FNM_PATHNAME)
115 return ((flags & FNM_LEADING_DIR) ||
116 strchr(string, '/') == NULL ?
117 0 : FNM_NOMATCH);
118 else
119 return (0);
120 } else if (c == '/' && (flags & FNM_PATHNAME)) {
121 if ((string = strchr(string, '/')) == NULL)
122 return (FNM_NOMATCH);
123 break;
126 /* General case, use recursion. */
127 while ((test = *string) != EOS) {
128 if (!fnmatch(pattern, string, flags & ~FNM_PERIOD))
129 return (0);
130 if (test == '/' && (flags & FNM_PATHNAME))
131 break;
132 ++string;
134 return (FNM_NOMATCH);
135 case '[':
136 if (*string == EOS)
137 return (FNM_NOMATCH);
138 if (*string == '/' && (flags & FNM_PATHNAME))
139 return (FNM_NOMATCH);
140 if (*string == '.' && (flags & FNM_PERIOD) &&
141 (string == stringstart ||
142 ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
143 return (FNM_NOMATCH);
145 switch (rangematch(pattern, *string, flags, &newp)) {
146 case RANGE_ERROR:
147 /* not a good range, treat as normal text */
148 goto normal;
149 case RANGE_MATCH:
150 pattern = newp;
151 break;
152 case RANGE_NOMATCH:
153 return (FNM_NOMATCH);
155 ++string;
156 break;
157 case '\\':
158 if (!(flags & FNM_NOESCAPE)) {
159 if ((c = *pattern++) == EOS) {
160 c = '\\';
161 --pattern;
164 /* FALLTHROUGH */
165 default:
166 normal:
167 if (c != *string && !((flags & FNM_CASEFOLD) &&
168 (tolower((unsigned char)c) ==
169 tolower((unsigned char)*string))))
170 return (FNM_NOMATCH);
171 ++string;
172 break;
174 /* NOTREACHED */
177 static int
178 rangematch(pattern, test, flags, newp)
179 const char *pattern;
180 char test;
181 int flags;
182 char **newp;
184 int negate, ok;
185 char c, c2;
188 * A bracket expression starting with an unquoted circumflex
189 * character produces unspecified results (IEEE 1003.2-1992,
190 * 3.13.2). This implementation treats it like '!', for
191 * consistency with the regular expression syntax.
192 * J.T. Conklin (conklin@ngai.kaleida.com)
194 negate = (*pattern == '!' || *pattern == '^');
195 if (negate)
196 ++pattern;
198 if (flags & FNM_CASEFOLD)
199 test = tolower((unsigned char)test);
202 * A right bracket shall lose its special meaning and represent
203 * itself in a bracket expression if it occurs first in the list.
204 * -- POSIX.2 2.8.3.2
206 ok = 0;
207 c = *pattern++;
208 do {
209 if (c == '\\' && !(flags & FNM_NOESCAPE))
210 c = *pattern++;
211 if (c == EOS)
212 return (RANGE_ERROR);
213 if (c == '/' && (flags & FNM_PATHNAME))
214 return (RANGE_NOMATCH);
215 if ((flags & FNM_CASEFOLD))
216 c = tolower((unsigned char)c);
217 if (*pattern == '-'
218 && (c2 = *(pattern+1)) != EOS && c2 != ']') {
219 pattern += 2;
220 if (c2 == '\\' && !(flags & FNM_NOESCAPE))
221 c2 = *pattern++;
222 if (c2 == EOS)
223 return (RANGE_ERROR);
224 if (flags & FNM_CASEFOLD)
225 c2 = tolower((unsigned char)c2);
226 if (c <= test && test <= c2)
227 ok = 1;
228 } else if (c == test)
229 ok = 1;
230 } while ((c = *pattern++) != ']');
232 *newp = (char *)pattern;
233 return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH);