2 Unix SMB/Netbios implementation.
4 filename matching routine
5 Copyright (C) Andrew Tridgell 1992-1998
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 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 resemblence to that code now
37 bugger. we need a separate wildcard routine for older versions
38 of the protocol. This is not yet perfect, but its a lot
39 better thaan what we had */
40 static int ms_fnmatch_lanman_core(char *pattern, char *string)
42 char *p = pattern, *n = string;
45 if (strcmp(p,"?")==0 && strcmp(n,".")==0) goto match;
50 /* if (! *n && ! *p) goto match; */
51 if (*n != '.') goto nomatch;
56 if ((*n == '.' && n[1] != '.') || ! *n) goto next;
62 if (! n[1] && ms_fnmatch_lanman_core(p, n+1) == 0) goto match;
63 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
73 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
79 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
80 if (*n == '.' && !strchr(n+1,'.')) {
88 if (*n == 0 && ms_fnmatch_lanman_core(p, n) == 0) goto match;
89 if (*n != '.') goto nomatch;
94 if (c != *n) goto nomatch;
103 if (verbose) printf("NOMATCH pattern=[%s] string=[%s]\n", pattern, string);
108 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
113 if (verbose) printf("MATCH pattern=[%s] string=[%s]\n", pattern, string);
118 static int ms_fnmatch_lanman1(char *pattern, char *string)
120 if (!strpbrk(pattern, "?*<>\"")) {
121 if (strcmp(string,"..") == 0) string = ".";
122 return strcmp(pattern, string);
125 if (strcmp(string,"..") == 0 || strcmp(string,".") == 0) {
126 return ms_fnmatch_lanman_core(pattern, "..") &&
127 ms_fnmatch_lanman_core(pattern, ".");
130 return ms_fnmatch_lanman_core(pattern, string);
134 /* the following function was derived using the masktest utility -
135 after years of effort we finally have a perfect MS wildcard
138 NOTE: this matches only filenames with no directory component
140 Returns 0 on match, -1 on fail.
142 int ms_fnmatch(char *pattern, char *string)
144 char *p = pattern, *n = string;
148 if (Protocol <= PROTOCOL_LANMAN2) {
149 return ms_fnmatch_lanman1(pattern, string);
161 if (! n[1] && ms_fnmatch(p, n+1) == 0) return 0;
162 if (ms_fnmatch(p, n) == 0) return 0;
165 if (! *n) return ms_fnmatch(p, n);
171 if (ms_fnmatch(p, n) == 0) return 0;
177 if (ms_fnmatch(p, n) == 0) return 0;
178 if (*n == '.' && !strchr(n+1,'.')) {
186 if (*n == 0 && ms_fnmatch(p, n) == 0) return 0;
187 if (*n != '.') return -1;
192 if (c != *n) return -1;
205 static int match_one(char *pattern, char *file)
207 if (strcmp(file,"..") == 0) file = ".";
208 if (strcmp(pattern,".") == 0) return -1;
210 return ms_fnmatch(pattern, file);
213 static char *match_test(char *pattern, char *file, char *short_name)
216 strncpy(ret, "---", 3);
218 if (match_one(pattern, ".") == 0) ret[0] = '+';
219 if (match_one(pattern, "..") == 0) ret[1] = '+';
220 if (match_one(pattern, file) == 0 ||
221 (*short_name && match_one(pattern, short_name)==0)) ret[2] = '+';
225 int main(int argc, char *argv[])
228 char ans[4], mask[100], file[100], mfile[100];
234 ret = ms_fnmatch(argv[1], argv[2]);
242 while (fgets(line, sizeof(line)-1, stdin)) {
243 n = sscanf(line, "%3s %s %s %s\n", ans, mask, file, mfile);
245 ans2 = match_test(mask, file, mfile);
246 if (strcmp(ans2, ans)) {
247 printf("%s %s %d mask=[%s] file=[%s] mfile=[%s]\n",
248 ans, ans2, i, mask, file, mfile);
255 #endif /* FNMATCH_TEST */