2 * Copyright (c) 2003-2007 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "cpio_platform.h"
28 __FBSDID("$FreeBSD$");
34 #include "pathmatch.h"
37 * Check whether a character 'c' is matched by a list specification [...]:
38 * * Leading '!' negates the class.
39 * * <char>-<char> is a range of characters
40 * * \<char> removes any special meaning for <char>
42 * Some interesting boundary cases:
43 * a-d-e is one range (a-d) followed by two single characters - and e.
44 * \a-\d is same as a-d
45 * a\-d is three single characters: a, d, -
46 * Trailing - is not special (so [a-] is two characters a and -).
47 * Initial - is not special ([a-] is same as [-a] is same as [\\-a])
48 * This function never sees a trailing \.
53 pm_list(const char *start
, const char *end
, const char c
, int flags
)
55 const char *p
= start
;
56 char rangeStart
= '\0', nextRangeStart
;
57 int match
= 1, nomatch
= 0;
59 /* This will be used soon... */
60 (void)flags
; /* UNUSED */
62 /* If this is a negated class, return success for nomatch. */
63 if (*p
== '!' && p
< end
) {
70 nextRangeStart
= '\0';
73 /* Trailing or initial '-' is not special. */
74 if ((rangeStart
== '\0') || (p
== end
- 1)) {
81 if ((rangeStart
<= c
) && (c
<= rangeEnd
))
91 nextRangeStart
= *p
; /* Possible start of range. */
93 rangeStart
= nextRangeStart
;
100 * If s is pointing to "./", ".//", "./././" or the like, skip it.
103 pm_slashskip(const char *s
) {
104 while (*s
== '.' || *s
== '/') {
105 if (s
[0] != '/' && s
[1] != '/')
113 pm(const char *p
, const char *s
, int flags
)
118 * Ignore leading './', './/', '././', etc.
120 if (s
[0] == '.' && s
[1] == '/')
121 s
= pm_slashskip(s
+ 1);
122 if (p
[0] == '.' && p
[1] == '/')
123 p
= pm_slashskip(p
+ 1);
129 if (flags
& PATHMATCH_NO_ANCHOR_END
)
131 /* "dir" == "dir/" == "dir/." */
133 if (s
[0] == '.' && s
[1] == '\0')
139 /* ? always succeds, unless we hit end of 's' */
144 /* "*" == "**" == "***" ... */
147 /* Trailing '*' always succeeds. */
151 if (pathmatch(p
, s
, flags
))
159 * Find the end of the [...] character class,
160 * ignoring \] that might occur within the class.
163 while (*end
!= '\0' && *end
!= ']') {
164 if (*end
== '\\' && end
[1] != '\0')
169 /* We found [...], try to match it. */
170 if (!pm_list(p
+ 1, end
, *s
, flags
))
172 p
= end
; /* Jump to trailing ']' char. */
175 /* No final ']', so just match '['. */
182 if ((*s
== '\0') && (*p
== '/')) {
186 if (p
[0] == '.' && p
[1] == '\0')
193 /* Trailing '\\' matches itself. */
205 * TODO: pattern of "\/\.\/" should not match plain "/",
206 * it should only match explicit "/./".
219 /* Main entry point. */
221 pathmatch(const char *p
, const char *s
, int flags
)
223 /* Empty pattern only matches the empty string. */
224 if (p
== NULL
|| *p
== '\0')
225 return (s
== NULL
|| *s
== '\0');
227 /* Leading '^' anchors the start of the pattern. */
230 flags
&= ~PATHMATCH_NO_ANCHOR_START
;
233 /* Certain patterns anchor implicitly. */
234 if (*p
== '*' || *p
== '/')
235 return (pm(p
, s
, flags
));
237 /* If start is unanchored, try to match start of each path element. */
238 if (flags
& PATHMATCH_NO_ANCHOR_START
) {
239 for ( ; p
!= NULL
; p
= strchr(p
, '/')) {
248 /* Default: Match from beginning. */
249 return (pm(p
, s
, flags
));