2 Unix SMB/CIFS implementation.
3 filename matching routine
4 Copyright (C) Andrew Tridgell 1992-1998
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. */
21 This module was originally based on fnmatch.c copyright by the Free
22 Software Foundation. It bears little resemblence to that code now
34 bugger. we need a separate wildcard routine for older versions
35 of the protocol. This is not yet perfect, but its a lot
36 better than what we had */
37 static int ms_fnmatch_lanman_core(const smb_ucs2_t *pattern,
38 const smb_ucs2_t *string,
41 const smb_ucs2_t *p = pattern, *n = string;
44 if (strcmp_wa(p, "?")==0 && strcmp_wa(n, ".")) goto match;
50 if (*n != UCS2_CHAR('.')) goto nomatch;
56 if ((*n == UCS2_CHAR('.') &&
57 n[1] != UCS2_CHAR('.')) || ! *n)
64 if (n[0] == UCS2_CHAR('.')) {
65 if (! n[1] && ms_fnmatch_lanman_core(p, n+1, case_sensitive) == 0) goto match;
66 if (ms_fnmatch_lanman_core(p, n, case_sensitive) == 0) goto match;
76 if (ms_fnmatch_lanman_core(p, n, case_sensitive) == 0) goto match;
82 if (ms_fnmatch_lanman_core(p, n, case_sensitive) == 0) goto match;
83 if (*n == UCS2_CHAR('.') &&
84 !strchr_w(n+1,UCS2_CHAR('.'))) {
92 if (*n == 0 && ms_fnmatch_lanman_core(p, n, case_sensitive) == 0) goto match;
93 if (*n != UCS2_CHAR('.')) goto nomatch;
99 if (c != *n) goto nomatch;
101 if (tolower_w(c) != tolower_w(*n)) goto nomatch;
107 if (! *n) goto match;
111 if (verbose) printf("NOMATCH pattern=[%s] string=[%s]\n", pattern, string);
116 if (ms_fnmatch_lanman_core(p, n, case_sensitive) == 0) goto match;
121 if (verbose) printf("MATCH pattern=[%s] string=[%s]\n", pattern, string);
126 static int ms_fnmatch_lanman1(const smb_ucs2_t *pattern,
127 const smb_ucs2_t *string, BOOL case_sensitive)
129 if (!strpbrk_wa(pattern, "?*<>\"")) {
130 smb_ucs2_t s[] = {UCS2_CHAR('.'), 0};
131 if (strcmp_wa(string,"..") == 0) string = s;
132 return strcasecmp_w(pattern, string);
135 if (strcmp_wa(string,"..") == 0 || strcmp_wa(string,".") == 0) {
136 smb_ucs2_t dot[] = {UCS2_CHAR('.'), 0};
137 smb_ucs2_t dotdot[] = {UCS2_CHAR('.'), UCS2_CHAR('.'), 0};
138 return ms_fnmatch_lanman_core(pattern, dotdot, case_sensitive) &&
139 ms_fnmatch_lanman_core(pattern, dot, case_sensitive);
142 return ms_fnmatch_lanman_core(pattern, string, case_sensitive);
146 /* the following function was derived using the masktest utility -
147 after years of effort we finally have a perfect MS wildcard
150 NOTE: this matches only filenames with no directory component
152 Returns 0 on match, -1 on fail.
154 static int ms_fnmatch_w(const smb_ucs2_t *pattern, const smb_ucs2_t *string,
155 int protocol, BOOL case_sensitive)
157 const smb_ucs2_t *p = pattern, *n = string;
160 if (protocol <= PROTOCOL_LANMAN2) {
161 return ms_fnmatch_lanman1(pattern, string, case_sensitive);
172 if (n[0] == UCS2_CHAR('.')) {
173 if (! n[1] && ms_fnmatch_w(p, n+1, protocol, case_sensitive) == 0) return 0;
174 if (ms_fnmatch_w(p, n, protocol, case_sensitive) == 0) return 0;
177 if (! *n) return ms_fnmatch_w(p, n, protocol, case_sensitive);
183 if (ms_fnmatch_w(p, n, protocol, case_sensitive) == 0) return 0;
189 if (ms_fnmatch_w(p, n, protocol, case_sensitive) == 0) return 0;
190 if (*n == UCS2_CHAR('.') && !strchr_wa(n+1,'.')) {
198 if (*n == 0 && ms_fnmatch_w(p, n, protocol, case_sensitive) == 0) return 0;
199 if (*n != UCS2_CHAR('.')) return -1;
204 if (case_sensitive) {
205 if (c != *n) return -1;
207 if (tolower_w(c) != tolower_w(*n)) return -1;
218 int ms_fnmatch(const char *pattern, const char *string, int protocol,
221 wpstring buffer_pattern, buffer_string;
225 size = push_ucs2(NULL, buffer_pattern, pattern, sizeof(buffer_pattern), STR_TERMINATE);
226 if (size == (size_t)-1) {
228 /* Not quite the right answer, but finding the right one
229 under this failure case is expensive, and it's pretty close */
232 size = push_ucs2(NULL, buffer_string, string, sizeof(buffer_string), STR_TERMINATE);
233 if (size == (size_t)-1) {
235 /* Not quite the right answer, but finding the right one
236 under this failure case is expensive, and it's pretty close */
239 ret = ms_fnmatch_w(buffer_pattern, buffer_string, protocol, case_senstive);
240 DEBUG(10,("ms_fnmatch(%s,%s) -> %d\n", pattern, string, ret));
245 /* a generic fnmatch function - uses for non-CIFS pattern matching */
246 int gen_fnmatch(const char *pattern, const char *string)
248 return ms_fnmatch(pattern, string, PROTOCOL_NT1, True);