1 /* $NetBSD: fnmatch.c,v 1.11 1995/02/27 03:43:06 cgd Exp $ */
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
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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
39 #if defined(LIBC_SCCS) && !defined(lint)
41 static char sccsid
[] = "@(#)fnmatch.c 8.2 (Berkeley) 4/16/94";
43 static char rcsid
[] = "$NetBSD: fnmatch.c,v 1.11 1995/02/27 03:43:06 cgd Exp $";
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.
57 static const char *rangematch (const char *, int, int);
60 fnmatch(const char *pattern
, const char *string
, int flags
)
62 const char *stringstart
;
65 for (stringstart
= string
;;)
66 switch (c
= *pattern
++) {
68 return (*string
== EOS
? 0 : FNM_NOMATCH
);
72 if (*string
== '/' && (flags
& FNM_PATHNAME
))
74 if (*string
== '.' && (flags
& FNM_PERIOD
) &&
75 (string
== stringstart
||
76 ((flags
& FNM_PATHNAME
) && *(string
- 1) == '/')))
82 /* Collapse multiple stars. */
86 if (*string
== '.' && (flags
& FNM_PERIOD
) &&
87 (string
== stringstart
||
88 ((flags
& FNM_PATHNAME
) && *(string
- 1) == '/')))
91 /* Optimize for pattern with * at end or before /. */
93 if (flags
& FNM_PATHNAME
)
94 return (strchr(string
, '/') == NULL
?
98 else if (c
== '/' && flags
& FNM_PATHNAME
) {
99 if ((string
= strchr(string
, '/')) == NULL
)
100 return (FNM_NOMATCH
);
104 /* General case, use recursion. */
105 while ((test
= *string
) != EOS
) {
106 if (!fnmatch(pattern
, string
, flags
& ~FNM_PERIOD
))
108 if (test
== '/' && flags
& FNM_PATHNAME
)
112 return (FNM_NOMATCH
);
115 return (FNM_NOMATCH
);
116 if (*string
== '/' && flags
& FNM_PATHNAME
)
117 return (FNM_NOMATCH
);
119 rangematch(pattern
, *string
, flags
)) == NULL
)
120 return (FNM_NOMATCH
);
124 if (!(flags
& FNM_NOESCAPE
)) {
125 if ((c
= *pattern
++) == EOS
) {
133 return (FNM_NOMATCH
);
140 rangematch(const char *pattern
, int test
, int flags
)
146 * A bracket expression starting with an unquoted circumflex
147 * character produces unspecified results (IEEE 1003.2-1992,
148 * 3.13.2). This implementation treats it like '!', for
149 * consistency with the regular expression syntax.
150 * J.T. Conklin (conklin@ngai.kaleida.com)
152 if (negate
= (*pattern
== '!' || *pattern
== '^'))
155 for (ok
= 0; (c
= *pattern
++) != ']';) {
156 if (c
== '\\' && !(flags
& FNM_NOESCAPE
))
161 && (c2
= *(pattern
+1)) != EOS
&& c2
!= ']') {
163 if (c2
== '\\' && !(flags
& FNM_NOESCAPE
))
167 if (c
<= test
&& test
<= c2
)
169 } else if (c
== test
)
172 return (ok
== negate
? NULL
: pattern
);