2 Unix SMB/CIFS implementation.
3 filename matching routine
4 Copyright (C) Andrew Tridgell 1992-2004
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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 This module was originally based on fnmatch.c copyright by the Free
23 Software Foundation. It bears little (if any) resemblence to that
30 static int null_match(const smb_ucs2_t
*p
)
33 if (*p
!= UCS2_CHAR('*') &&
34 *p
!= UCS2_CHAR('<') &&
35 *p
!= UCS2_CHAR('"') &&
36 *p
!= UCS2_CHAR('>')) return -1;
42 the max_n structure is purely for efficiency, it doesn't contribute
43 to the matching algorithm except by ensuring that the algorithm does
44 not grow exponentially
47 const smb_ucs2_t
*predot
;
48 const smb_ucs2_t
*postdot
;
53 p and n are the pattern and string being matched. The max_n array is
54 an optimisation only. The ldot pointer is NULL if the string does
55 not contain a '.', otherwise it points at the last dot in 'n'.
57 static int ms_fnmatch_core(const smb_ucs2_t
*p
, const smb_ucs2_t
*n
,
58 struct max_n
*max_n
, const smb_ucs2_t
*ldot
)
65 /* a '*' matches zero or more characters of any type */
67 if (max_n
->predot
&& max_n
->predot
<= n
) {
70 for (i
=0; n
[i
]; i
++) {
71 if (ms_fnmatch_core(p
, n
+i
, max_n
+1, ldot
) == 0) {
75 if (!max_n
->predot
|| max_n
->predot
> n
) max_n
->predot
= n
;
78 /* a '<' matches zero or more characters of
79 any type, but stops matching at the last
82 if (max_n
->predot
&& max_n
->predot
<= n
) {
85 if (max_n
->postdot
&& max_n
->postdot
<= n
&& n
<= ldot
) {
88 for (i
=0; n
[i
]; i
++) {
89 if (ms_fnmatch_core(p
, n
+i
, max_n
+1, ldot
) == 0) return 0;
91 if (ms_fnmatch_core(p
, n
+i
+1, max_n
+1, ldot
) == 0) return 0;
92 if (!max_n
->postdot
|| max_n
->postdot
> n
) max_n
->postdot
= n
;
96 if (!max_n
->predot
|| max_n
->predot
> n
) max_n
->predot
= n
;
99 /* a '?' matches any single character */
107 /* a '?' matches any single character */
109 if (n
[0] == UCS2_CHAR('.')) {
110 if (! n
[1] && null_match(p
) == 0) {
115 if (! *n
) return null_match(p
);
120 if (*n
== 0 && null_match(p
) == 0) {
123 if (*n
!= UCS2_CHAR('.')) return -1;
128 if (c
!= *n
&& toupper_w(c
) != toupper_w(*n
)) {
143 int ms_fnmatch(const char *pattern
, const char *string
, enum protocol_types protocol
)
147 struct max_n
*max_n
= NULL
;
149 if (strcmp(string
, "..") == 0) {
153 if (strpbrk(pattern
, "<>*?\"") == NULL
) {
154 /* this is not just an optmisation - it is essential
155 for LANMAN1 correctness */
156 return StrCaseCmp(pattern
, string
);
159 pstrcpy_wa(p
, pattern
);
160 pstrcpy_wa(s
, string
);
162 if (protocol
<= PROTOCOL_LANMAN2
) {
164 for older negotiated protocols it is possible to
165 translate the pattern to produce a "new style"
166 pattern that exactly matches w2k behaviour
169 if (p
[i
] == UCS2_CHAR('?')) {
170 p
[i
] = UCS2_CHAR('>');
171 } else if (p
[i
] == UCS2_CHAR('.') &&
172 (p
[i
+1] == UCS2_CHAR('?') ||
173 p
[i
+1] == UCS2_CHAR('*') ||
175 p
[i
] = UCS2_CHAR('"');
176 } else if (p
[i
] == UCS2_CHAR('*') && p
[i
+1] == UCS2_CHAR('.')) {
177 p
[i
] = UCS2_CHAR('<');
182 for (count
=i
=0;p
[i
];i
++) {
183 if (p
[i
] == UCS2_CHAR('*') || p
[i
] == UCS2_CHAR('<')) count
++;
187 max_n
= calloc(sizeof(struct max_n
), count
);
193 ret
= ms_fnmatch_core(p
, s
, max_n
, strrchr_w(s
, UCS2_CHAR('.')));
203 /* a generic fnmatch function - uses for non-CIFS pattern matching */
204 int gen_fnmatch(const char *pattern
, const char *string
)
206 return ms_fnmatch(pattern
, string
, PROTOCOL_NT1
);