2 Unix SMB/CIFS implementation.
3 Samba utility functions
4 Copyright (C) Jeremy Allison 2001
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "lib/util/talloc_stack.h"
23 #include "lib/util/charset/charset.h"
24 #include "lib/util/unix_match.h"
26 /*********************************************************
27 Recursive routine that is called by unix_wild_match.
28 *********************************************************/
30 static bool unix_do_match(const char *regexp
, const char *str
)
34 for( p
= regexp
; *p
&& *str
; ) {
45 * Look for a character matching
46 * the one after the '*'.
50 return true; /* Automatic match */
54 while(*str
&& (*p
!= *str
)) {
59 * Patch from weidel@multichart.de.
60 * In the case of the regexp
61 * '*XX*' we want to ensure there are
62 * at least 2 'X' characters in the
63 * string after the '*' for a match to
71 * Eat all the characters that
72 * match, but count how many
76 while(*str
&& (*p
== *str
)) {
82 * Now check that if the regexp
83 * had n identical characters
84 * that matchcount had at least
88 while (*(p
+1) && (*(p
+1)==*p
)) {
93 if ( matchcount
<= 0 ) {
99 * We've eaten the match char
104 if(unix_do_match(p
, str
)) {
130 if (!*p
&& str
[0] == '.' && str
[1] == 0) {
134 if (!*str
&& *p
== '?') {
141 if(!*str
&& (*p
== '*' && p
[1] == '\0')) {
148 /*******************************************************************
149 Simple case insensitive interface to a UNIX wildcard matcher.
150 Returns True if match, False if not.
151 *******************************************************************/
153 bool unix_wild_match(const char *pattern
, const char *string
)
155 TALLOC_CTX
*ctx
= talloc_stackframe();
161 p2
= strlower_talloc(ctx
, pattern
);
162 s2
= strlower_talloc(ctx
, string
);
168 /* Remove any *? and ** from the pattern as they are meaningless */
169 for(p
= p2
; *p
; p
++) {
170 while( *p
== '*' && (p
[1] == '?' ||p
[1] == '*')) {
171 memmove(&p
[1], &p
[2], strlen(&p
[2])+1);
175 if (p2
[0] == '*' && p2
[1] == '\0') {
180 ret
= unix_do_match(p2
, s2
);