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
34 /* the following function was derived using the masktest utility -
35 after years of effort we finally have a perfect MS wildcard
38 NOTE: this matches only filenames with no directory component
40 Returns 0 on match, -1 on fail.
42 int ms_fnmatch(char *pattern, char *string)
44 char *p = pattern, *n = string;
56 if (! n[1] && ms_fnmatch(p, n+1) == 0) return 0;
57 if (ms_fnmatch(p, n) == 0) return 0;
60 if (! *n) return ms_fnmatch(p, n);
66 if (ms_fnmatch(p, n) == 0) return 0;
72 if (ms_fnmatch(p, n) == 0) return 0;
73 if (*n == '.' && !strchr(n+1,'.')) {
81 if (*n == 0 && ms_fnmatch(p, n) == 0) return 0;
82 if (*n != '.') return -1;
87 if (c != *n) return -1;
100 static int match_one(char *pattern, char *file)
102 if (strcmp(file,"..") == 0) file = ".";
103 if (strcmp(pattern,".") == 0) return -1;
105 return ms_fnmatch(pattern, file);
108 static char *match_test(char *pattern, char *file, char *short_name)
111 strncpy(ret, "---", 3);
113 if (match_one(pattern, ".") == 0) ret[0] = '+';
114 if (match_one(pattern, "..") == 0) ret[1] = '+';
115 if (match_one(pattern, file) == 0 ||
116 (*short_name && match_one(pattern, short_name)==0)) ret[2] = '+';
120 int main(int argc, char *argv[])
123 char ans[4], mask[100], file[100], mfile[100];
129 ret = ms_fnmatch(argv[1], argv[2]);
137 while (fgets(line, sizeof(line)-1, stdin)) {
138 n = sscanf(line, "%3s %s %s %s\n", ans, mask, file, mfile);
140 ans2 = match_test(mask, file, mfile);
141 if (strcmp(ans2, ans)) {
142 printf("%s %s %d mask=[%s] file=[%s] mfile=[%s]\n",
143 ans, ans2, i, mask, file, mfile);
150 #endif /* FNMATCH_TEST */