x
[heimdal.git] / lib / roken / fnmatch.c
blob126949a8e08d1a2d93aa4c4613b92b598719f9d6
1 /* $NetBSD: fnmatch.c,v 1.11 1995/02/27 03:43:06 cgd 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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
35 #if defined(LIBC_SCCS) && !defined(lint)
36 #if 0
37 static char sccsid[] = "@(#)fnmatch.c 8.2 (Berkeley) 4/16/94";
38 #else
39 static char rcsid[] = "$NetBSD: fnmatch.c,v 1.11 1995/02/27 03:43:06 cgd Exp $";
40 #endif
41 #endif /* LIBC_SCCS and not lint */
44 * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
45 * Compares a filename or pathname to a pattern.
48 #include <fnmatch.h>
49 #include <string.h>
51 #define EOS '\0'
53 static const char *rangematch (const char *, int, int);
55 int ROKEN_LIB_FUNCTION
56 rk_fnmatch(const char *pattern, const char *string, int flags)
58 const char *stringstart;
59 char c, test;
61 for (stringstart = string;;)
62 switch (c = *pattern++) {
63 case EOS:
64 return (*string == EOS ? 0 : FNM_NOMATCH);
65 case '?':
66 if (*string == EOS)
67 return (FNM_NOMATCH);
68 if (*string == '/' && (flags & FNM_PATHNAME))
69 return (FNM_NOMATCH);
70 if (*string == '.' && (flags & FNM_PERIOD) &&
71 (string == stringstart ||
72 ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
73 return (FNM_NOMATCH);
74 ++string;
75 break;
76 case '*':
77 c = *pattern;
78 /* Collapse multiple stars. */
79 while (c == '*')
80 c = *++pattern;
82 if (*string == '.' && (flags & FNM_PERIOD) &&
83 (string == stringstart ||
84 ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
85 return (FNM_NOMATCH);
87 /* Optimize for pattern with * at end or before /. */
88 if (c == EOS)
89 if (flags & FNM_PATHNAME)
90 return (strchr(string, '/') == NULL ?
91 0 : FNM_NOMATCH);
92 else
93 return (0);
94 else if (c == '/' && flags & FNM_PATHNAME) {
95 if ((string = strchr(string, '/')) == NULL)
96 return (FNM_NOMATCH);
97 break;
100 /* General case, use recursion. */
101 while ((test = *string) != EOS) {
102 if (!rk_fnmatch(pattern, string, flags & ~FNM_PERIOD))
103 return (0);
104 if (test == '/' && flags & FNM_PATHNAME)
105 break;
106 ++string;
108 return (FNM_NOMATCH);
109 case '[':
110 if (*string == EOS)
111 return (FNM_NOMATCH);
112 if (*string == '/' && flags & FNM_PATHNAME)
113 return (FNM_NOMATCH);
114 if ((pattern =
115 rangematch(pattern, *string, flags)) == NULL)
116 return (FNM_NOMATCH);
117 ++string;
118 break;
119 case '\\':
120 if (!(flags & FNM_NOESCAPE)) {
121 if ((c = *pattern++) == EOS) {
122 c = '\\';
123 --pattern;
126 /* FALLTHROUGH */
127 default:
128 if (c != *string++)
129 return (FNM_NOMATCH);
130 break;
132 /* NOTREACHED */
135 static const char *
136 rangematch(const char *pattern, int test, int flags)
138 int negate, ok;
139 char c, c2;
142 * A bracket expression starting with an unquoted circumflex
143 * character produces unspecified results (IEEE 1003.2-1992,
144 * 3.13.2). This implementation treats it like '!', for
145 * consistency with the regular expression syntax.
146 * J.T. Conklin (conklin@ngai.kaleida.com)
148 if (negate = (*pattern == '!' || *pattern == '^'))
149 ++pattern;
151 for (ok = 0; (c = *pattern++) != ']';) {
152 if (c == '\\' && !(flags & FNM_NOESCAPE))
153 c = *pattern++;
154 if (c == EOS)
155 return (NULL);
156 if (*pattern == '-'
157 && (c2 = *(pattern+1)) != EOS && c2 != ']') {
158 pattern += 2;
159 if (c2 == '\\' && !(flags & FNM_NOESCAPE))
160 c2 = *pattern++;
161 if (c2 == EOS)
162 return (NULL);
163 if (c <= test && test <= c2)
164 ok = 1;
165 } else if (c == test)
166 ok = 1;
168 return (ok == negate ? NULL : pattern);