aboutsummaryrefslogtreecommitdiff
path: root/src/fnmatch.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/fnmatch.c')
-rw-r--r--src/fnmatch.c195
1 files changed, 0 insertions, 195 deletions
diff --git a/src/fnmatch.c b/src/fnmatch.c
deleted file mode 100644
index f2b6f6d4..00000000
--- a/src/fnmatch.c
+++ /dev/null
@@ -1,195 +0,0 @@
1/*
2 * Luis Figueiredo - why remake the wheel, this functions feets perfectly
3 * and the credits still here :)
4 */
5
6/*
7 * Copyright (c) 1989, 1993, 1994
8 * The Regents of the University of California. All rights reserved.
9 *
10 * This code is derived from software contributed to Berkeley by
11 * Guido van Rossum.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * SUCH DAMAGE.
40 */
41
42#if defined(LIBC_SCCS) && !defined(lint)
43static char sccsid[] = "@(#)fnmatch.c 8.2 (Berkeley) 4/16/94";
44#endif /* LIBC_SCCS and not lint */
45
46/*
47 * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
48 * Compares a filename or pathname to a pattern.
49 */
50
51#include "fnmatch.h"
52#include <string.h>
53
54#define EOS '\0'
55
56static const char *rangematch(const char *, int, int);
57
58int fnmatch(const char *pattern, const char *string, int flags) {
59 const char *stringstart;
60 char c, test;
61
62 for (stringstart = string;;) {
63 switch (c = *pattern++) {
64 case EOS:
65 return (*string == EOS ? 0 : FNM_NOMATCH);
66 case '?':
67 if (*string == EOS) {
68 return (FNM_NOMATCH);
69 }
70 if (*string == '/' && (flags & FNM_PATHNAME)) {
71 return (FNM_NOMATCH);
72 }
73 if (*string == '.' && (flags & FNM_PERIOD) &&
74 (string == stringstart ||
75 ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) {
76 return (FNM_NOMATCH);
77 }
78 ++string;
79 break;
80 case '*':
81 c = *pattern;
82 /* Collapse multiple stars. */
83 while (c == '*') {
84 c = *++pattern;
85 }
86
87 if (*string == '.' && (flags & FNM_PERIOD) &&
88 (string == stringstart ||
89 ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) {
90 return (FNM_NOMATCH);
91 }
92
93 /* Optimize for pattern with * at end or before /. */
94 if (c == EOS) {
95 if (flags & FNM_PATHNAME) {
96 return (strchr(string, '/') == NULL ? 0 : FNM_NOMATCH);
97 }
98 else {
99 return (0);
100 }
101 }
102 else if (c == '/' && flags & FNM_PATHNAME) {
103 if ((string = strchr(string, '/')) == NULL) {
104 return (FNM_NOMATCH);
105 }
106 break;
107 }
108
109 /* General case, use recursion. */
110 while ((test = *string) != EOS) {
111 if (!fnmatch(pattern, string, flags & ~FNM_PERIOD)) {
112 return (0);
113 }
114 if (test == '/' && flags & FNM_PATHNAME) {
115 break;
116 }
117 ++string;
118 }
119 return (FNM_NOMATCH);
120 case '[':
121 if (*string == EOS) {
122 return (FNM_NOMATCH);
123 }
124 if (*string == '/' && flags & FNM_PATHNAME) {
125 return (FNM_NOMATCH);
126 }
127 if (*string == '.' && (flags & FNM_PERIOD) &&
128 (string == stringstart ||
129 ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) {
130 return (FNM_NOMATCH);
131 }
132 if ((pattern = rangematch(pattern, *string, flags)) == NULL) {
133 return (FNM_NOMATCH);
134 }
135 ++string;
136 break;
137 case '\\':
138 if (!(flags & FNM_NOESCAPE)) {
139 if ((c = *pattern++) == EOS) {
140 c = '\\';
141 --pattern;
142 }
143 }
144 /* FALLTHROUGH */
145 default:
146 if (c != *string) {
147 return (FNM_NOMATCH);
148 }
149 string++;
150 break;
151 }
152 /* NOTREACHED */
153 }
154}
155
156static const char *rangematch(const char *pattern, int test, int flags) {
157 int negate, ok;
158 char c, c2;
159
160 /*
161 * A bracket expression starting with an unquoted circumflex
162 * character produces unspecified results (IEEE 1003.2-1992,
163 * 3.13.2). This implementation treats it like '!', for
164 * consistency with the regular expression syntax.
165 * J.T. Conklin (conklin@ngai.kaleida.com)
166 */
167 if ((negate = (*pattern == '!' || *pattern == '^'))) {
168 ++pattern;
169 }
170
171 for (ok = 0; (c = *pattern++) != ']';) {
172 if (c == '\\' && !(flags & FNM_NOESCAPE)) {
173 c = *pattern++;
174 }
175 if (c == EOS) {
176 return (NULL);
177 }
178 if (*pattern == '-' && (c2 = *(pattern + 1)) != EOS && c2 != ']') {
179 pattern += 2;
180 if (c2 == '\\' && !(flags & FNM_NOESCAPE)) {
181 c2 = *pattern++;
182 }
183 if (c2 == EOS) {
184 return (NULL);
185 }
186 if ((c <= test && test <= c2)) {
187 ok = 1;
188 }
189 }
190 else if ((c == test)) {
191 ok = 1;
192 }
193 }
194 return (ok == negate ? NULL : pattern);
195}