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) {
105 || (s[0] == '.' && s[1] == '/')
106 || (s[0] == '.' && s[1] == '\0'))
112 pm(const char *p, const char *s, int flags)
117 * Ignore leading './', './/', '././', etc.
119 if (s[0] == '.' && s[1] == '/')
120 s = pm_slashskip(s + 1);
121 if (p[0] == '.' && p[1] == '/')
122 p = pm_slashskip(p + 1);
128 if (flags & PATHMATCH_NO_ANCHOR_END)
130 /* "dir" == "dir/" == "dir/." */
135 /* ? always succeds, unless we hit end of 's' */
140 /* "*" == "**" == "***" ... */
143 /* Trailing '*' always succeeds. */
147 if (pathmatch(p, s, flags))
154 * Find the end of the [...] character class,
155 * ignoring \] that might occur within the class.
158 while (*end != '\0' && *end != ']') {
159 if (*end == '\\' && end[1] != '\0')
164 /* We found [...], try to match it. */
165 if (!pm_list(p + 1, end, *s, flags))
167 p = end; /* Jump to trailing ']' char. */
170 /* No final ']', so just match '['. */
175 /* Trailing '\\' matches itself. */
186 if (*s != '/' && *s != '\0')
188 /* Note: pattern "/\./" won't match "/";
189 * pm_slashskip() correctly stops at backslash. */
192 if (*p == '\0' && (flags & PATHMATCH_NO_ANCHOR_END))
194 --p; /* Counteract the increment below. */
198 /* '$' is special only at end of pattern and only
199 * if PATHMATCH_NO_ANCHOR_END is specified. */
200 if (p[1] == '\0' && (flags & PATHMATCH_NO_ANCHOR_END)){
201 /* "dir" == "dir/" == "dir/." */
202 return (*pm_slashskip(s) == '\0');
204 /* Otherwise, '$' is not special. */
216 /* Main entry point. */
218 pathmatch(const char *p, const char *s, int flags)
220 /* Empty pattern only matches the empty string. */
221 if (p == NULL || *p == '\0')
222 return (s == NULL || *s == '\0');
224 /* Leading '^' anchors the start of the pattern. */
227 flags &= ~PATHMATCH_NO_ANCHOR_START;
230 if (*p == '/' && *s != '/')
233 /* Certain patterns and file names anchor implicitly. */
234 if (*p == '*' || *p == '/' || *p == '/') {
239 return (pm(p, s, flags));
242 /* If start is unanchored, try to match start of each path element. */
243 if (flags & PATHMATCH_NO_ANCHOR_START) {
244 for ( ; s != NULL; s = strchr(s, '/')) {
253 /* Default: Match from beginning. */
254 return (pm(p, s, flags));