1 /* $NetBSD: fnmatch.c,v 1.1.1.2 2014/04/24 12:45:52 pettai Exp $ */
3 /* NetBSD: fnmatch.c,v 1.11 1995/02/27 03:43:06 cgd Exp */
6 * Copyright (c) 1989, 1993, 1994
7 * The Regents of the University of California. All rights reserved.
9 * This code is derived from software contributed to Berkeley by
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #if defined(LIBC_SCCS) && !defined(lint)
39 static char sccsid
[] = "@(#)fnmatch.c 8.2 (Berkeley) 4/16/94";
41 static char rcsid
[] = "NetBSD: fnmatch.c,v 1.11 1995/02/27 03:43:06 cgd Exp";
43 #endif /* LIBC_SCCS and not lint */
46 * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
47 * Compares a filename or pathname to a pattern.
54 #include <krb5/roken.h>
61 static const char *rangematch (const char *, int, int);
63 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
64 rk_fnmatch(const char *pattern
, const char *string
, int flags
)
66 const char *stringstart
;
69 for (stringstart
= string
;;)
70 switch (c
= *pattern
++) {
72 return (*string
== EOS
? 0 : FNM_NOMATCH
);
76 if (*string
== '/' && (flags
& FNM_PATHNAME
))
78 if (*string
== '.' && (flags
& FNM_PERIOD
) &&
79 (string
== stringstart
||
80 ((flags
& FNM_PATHNAME
) && *(string
- 1) == '/')))
86 /* Collapse multiple stars. */
90 if (*string
== '.' && (flags
& FNM_PERIOD
) &&
91 (string
== stringstart
||
92 ((flags
& FNM_PATHNAME
) && *(string
- 1) == '/')))
95 /* Optimize for pattern with * at end or before /. */
97 if (flags
& FNM_PATHNAME
)
98 return (strchr(string
, '/') == NULL
?
102 else if (c
== '/' && flags
& FNM_PATHNAME
) {
103 if ((string
= strchr(string
, '/')) == NULL
)
104 return (FNM_NOMATCH
);
108 /* General case, use recursion. */
109 while ((test
= *string
) != EOS
) {
110 if (!rk_fnmatch(pattern
, string
, flags
& ~FNM_PERIOD
))
112 if (test
== '/' && flags
& FNM_PATHNAME
)
116 return (FNM_NOMATCH
);
119 return (FNM_NOMATCH
);
120 if (*string
== '/' && flags
& FNM_PATHNAME
)
121 return (FNM_NOMATCH
);
123 rangematch(pattern
, *string
, flags
)) == NULL
)
124 return (FNM_NOMATCH
);
128 if (!(flags
& FNM_NOESCAPE
)) {
129 if ((c
= *pattern
++) == EOS
) {
137 return (FNM_NOMATCH
);
144 rangematch(const char *pattern
, int test
, int flags
)
150 * A bracket expression starting with an unquoted circumflex
151 * character produces unspecified results (IEEE 1003.2-1992,
152 * 3.13.2). This implementation treats it like '!', for
153 * consistency with the regular expression syntax.
154 * J.T. Conklin (conklin@ngai.kaleida.com)
156 if (negate
= (*pattern
== '!' || *pattern
== '^'))
159 for (ok
= 0; (c
= *pattern
++) != ']';) {
160 if (c
== '\\' && !(flags
& FNM_NOESCAPE
))
165 && (c2
= *(pattern
+1)) != EOS
&& c2
!= ']') {
167 if (c2
== '\\' && !(flags
& FNM_NOESCAPE
))
171 if (c
<= test
&& test
<= c2
)
173 } else if (c
== test
)
176 return (ok
== negate
? NULL
: pattern
);