don't call a function error_message() as that conflicts with a
[Samba/gebeck_regimport.git] / source / lib / ms_fnmatch.c
blob106efa5bbcf449120ebf92b3c26622fc68f7b400
1 /*
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
23 */
26 #if FNMATCH_TEST
27 #include <stdio.h>
28 #include <stdlib.h>
29 #else
30 #include "includes.h"
31 #endif
33 /*
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)
40 const smb_ucs2_t *p = pattern, *n = string;
41 smb_ucs2_t c;
43 if (strcmp_wa(p, "?")==0 && strcmp_wa(n, ".")) goto match;
45 while ((c = *p++)) {
46 switch (c) {
47 case UCS2_CHAR('.'):
48 if (! *n) goto next;
49 if (*n != UCS2_CHAR('.')) goto nomatch;
50 n++;
51 break;
53 case UCS2_CHAR('?'):
54 if (! *n) goto next;
55 if ((*n == UCS2_CHAR('.') &&
56 n[1] != UCS2_CHAR('.')) || ! *n)
57 goto next;
58 n++;
59 break;
61 case UCS2_CHAR('>'):
62 if (! *n) goto next;
63 if (n[0] == UCS2_CHAR('.')) {
64 if (! n[1] && ms_fnmatch_lanman_core(p, n+1) == 0) goto match;
65 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
66 goto nomatch;
68 n++;
69 break;
71 case UCS2_CHAR('*'):
72 if (! *n) goto next;
73 if (! *p) goto match;
74 for (; *n; n++) {
75 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
77 break;
79 case UCS2_CHAR('<'):
80 for (; *n; n++) {
81 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
82 if (*n == UCS2_CHAR('.') &&
83 !strchr_w(n+1,UCS2_CHAR('.'))) {
84 n++;
85 break;
88 break;
90 case UCS2_CHAR('"'):
91 if (*n == 0 && ms_fnmatch_lanman_core(p, n) == 0) goto match;
92 if (*n != UCS2_CHAR('.')) goto nomatch;
93 n++;
94 break;
96 default:
97 if (c != *n) goto nomatch;
98 n++;
102 if (! *n) goto match;
104 nomatch:
106 if (verbose) printf("NOMATCH pattern=[%s] string=[%s]\n", pattern, string);
108 return -1;
110 next:
111 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
112 goto nomatch;
114 match:
116 if (verbose) printf("MATCH pattern=[%s] string=[%s]\n", pattern, string);
118 return 0;
121 static int ms_fnmatch_lanman1(const smb_ucs2_t *pattern, const smb_ucs2_t *string)
123 if (!strpbrk_wa(pattern, "?*<>\"")) {
124 smb_ucs2_t s[] = {UCS2_CHAR('.'), 0};
125 if (strcmp_wa(string,"..") == 0) string = s;
126 return strcasecmp_w(pattern, string);
129 if (strcmp_wa(string,"..") == 0 || strcmp_wa(string,".") == 0) {
130 smb_ucs2_t dot[] = {UCS2_CHAR('.'), 0};
131 smb_ucs2_t dotdot[] = {UCS2_CHAR('.'), UCS2_CHAR('.'), 0};
132 return ms_fnmatch_lanman_core(pattern, dotdot) &&
133 ms_fnmatch_lanman_core(pattern, dot);
136 return ms_fnmatch_lanman_core(pattern, string);
140 /* the following function was derived using the masktest utility -
141 after years of effort we finally have a perfect MS wildcard
142 matching routine!
144 NOTE: this matches only filenames with no directory component
146 Returns 0 on match, -1 on fail.
148 static int ms_fnmatch_w(const smb_ucs2_t *pattern, const smb_ucs2_t *string, int protocol)
150 const smb_ucs2_t *p = pattern, *n = string;
151 smb_ucs2_t c;
153 if (protocol <= PROTOCOL_LANMAN2) {
154 return ms_fnmatch_lanman1(pattern, string);
157 while ((c = *p++)) {
158 switch (c) {
159 case UCS2_CHAR('?'):
160 if (! *n) return -1;
161 n++;
162 break;
164 case UCS2_CHAR('>'):
165 if (n[0] == UCS2_CHAR('.')) {
166 if (! n[1] && ms_fnmatch_w(p, n+1, protocol) == 0) return 0;
167 if (ms_fnmatch_w(p, n, protocol) == 0) return 0;
168 return -1;
170 if (! *n) return ms_fnmatch_w(p, n, protocol);
171 n++;
172 break;
174 case UCS2_CHAR('*'):
175 for (; *n; n++) {
176 if (ms_fnmatch_w(p, n, protocol) == 0) return 0;
178 break;
180 case UCS2_CHAR('<'):
181 for (; *n; n++) {
182 if (ms_fnmatch_w(p, n, protocol) == 0) return 0;
183 if (*n == UCS2_CHAR('.') && !strchr_wa(n+1,'.')) {
184 n++;
185 break;
188 break;
190 case UCS2_CHAR('"'):
191 if (*n == 0 && ms_fnmatch_w(p, n, protocol) == 0) return 0;
192 if (*n != UCS2_CHAR('.')) return -1;
193 n++;
194 break;
196 default:
197 if (c != *n) return -1;
198 n++;
202 if (! *n) return 0;
204 return -1;
208 int ms_fnmatch(const char *pattern, const char *string, int protocol)
210 wpstring p, s;
211 int ret;
213 pstrcpy_wa(p, pattern);
214 pstrcpy_wa(s, string);
216 ret = ms_fnmatch_w(p, s, protocol);
217 /* DEBUG(0,("ms_fnmatch(%s,%s) -> %d\n", pattern, string, ret)); */
218 return ret;
221 /* a generic fnmatch function - uses for non-CIFS pattern matching */
222 int gen_fnmatch(const char *pattern, const char *string)
224 return ms_fnmatch(pattern, string, PROTOCOL_NT1);