AIX ACLs donated by IBM.
[tprouty/samba.git] / source / lib / ms_fnmatch.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    filename matching routine
5    Copyright (C) Andrew Tridgell 1992-1998 
6
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.
11    
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.
16    
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.  */
20
21 /*
22    This module was originally based on fnmatch.c copyright by the Free
23    Software Foundation. It bears little resemblence to that code now 
24 */  
25
26
27 #if FNMATCH_TEST
28 #include <stdio.h>
29 #include <stdlib.h>
30 #else
31 #include "includes.h"
32 #endif
33
34
35
36 /* 
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)
41 {
42         char *p = pattern, *n = string;
43         char c;
44
45         if (strcmp(p,"?")==0 && strcmp(n,".")==0) goto match;
46
47         while ((c = *p++)) {
48                 switch (c) {
49                 case '.':
50                         /* if (! *n && ! *p) goto match; */
51                         if (*n != '.') goto nomatch;
52                         n++;
53                         break;
54
55                 case '?':
56                         if ((*n == '.' && n[1] != '.') || ! *n) goto next;
57                         n++;
58                         break;
59
60                 case '>':
61                         if (n[0] == '.') {
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;
64                                 goto nomatch;
65                         }
66                         if (! *n) goto next;
67                         n++;
68                         break;
69
70                 case '*':
71                         if (! *p) goto match;
72                         for (; *n; n++) {
73                                 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
74                         }
75                         break;
76
77                 case '<':
78                         for (; *n; n++) {
79                                 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
80                                 if (*n == '.' && !strchr(n+1,'.')) {
81                                         n++;
82                                         break;
83                                 }
84                         }
85                         break;
86
87                 case '"':
88                         if (*n == 0 && ms_fnmatch_lanman_core(p, n) == 0) goto match;
89                         if (*n != '.') goto nomatch;
90                         n++;
91                         break;
92
93                 default:
94                         if (c != *n) goto nomatch;
95                         n++;
96                 }
97         }
98         
99         if (! *n) goto match;
100         
101  nomatch:
102         /*
103         if (verbose) printf("NOMATCH pattern=[%s] string=[%s]\n", pattern, string);
104         */
105         return -1;
106
107 next:
108         if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
109         goto nomatch;
110
111  match:
112         /*
113         if (verbose) printf("MATCH   pattern=[%s] string=[%s]\n", pattern, string);
114         */
115         return 0;
116 }
117
118 static int ms_fnmatch_lanman1(char *pattern, char *string)
119 {
120         if (!strpbrk(pattern, "?*<>\"")) {
121                 if (strcmp(string,"..") == 0) string = ".";
122                 return strcmp(pattern, string);
123         }
124
125         if (strcmp(string,"..") == 0 || strcmp(string,".") == 0) {
126                 return ms_fnmatch_lanman_core(pattern, "..") &&
127                         ms_fnmatch_lanman_core(pattern, ".");
128         }
129
130         return ms_fnmatch_lanman_core(pattern, string);
131 }
132
133
134 /* the following function was derived using the masktest utility -
135    after years of effort we finally have a perfect MS wildcard
136    matching routine! 
137
138    NOTE: this matches only filenames with no directory component
139
140    Returns 0 on match, -1 on fail.
141 */
142 int ms_fnmatch(char *pattern, char *string)
143 {
144         char *p = pattern, *n = string;
145         char c;
146         extern int Protocol;
147
148         if (Protocol <= PROTOCOL_LANMAN2) {
149                 return ms_fnmatch_lanman1(pattern, string);
150         }
151
152         while ((c = *p++)) {
153                 switch (c) {
154                 case '?':
155                         if (! *n) return -1;
156                         n++;
157                         break;
158
159                 case '>':
160                         if (n[0] == '.') {
161                                 if (! n[1] && ms_fnmatch(p, n+1) == 0) return 0;
162                                 if (ms_fnmatch(p, n) == 0) return 0;
163                                 return -1;
164                         }
165                         if (! *n) return ms_fnmatch(p, n);
166                         n++;
167                         break;
168
169                 case '*':
170                         for (; *n; n++) {
171                                 if (ms_fnmatch(p, n) == 0) return 0;
172                         }
173                         break;
174
175                 case '<':
176                         for (; *n; n++) {
177                                 if (ms_fnmatch(p, n) == 0) return 0;
178                                 if (*n == '.' && !strchr(n+1,'.')) {
179                                         n++;
180                                         break;
181                                 }
182                         }
183                         break;
184
185                 case '"':
186                         if (*n == 0 && ms_fnmatch(p, n) == 0) return 0;
187                         if (*n != '.') return -1;
188                         n++;
189                         break;
190
191                 default:
192                         if (c != *n) return -1;
193                         n++;
194                 }
195         }
196         
197         if (! *n) return 0;
198         
199         return -1;
200 }
201
202
203 #if FNMATCH_TEST
204
205 static int match_one(char *pattern, char *file)
206 {
207         if (strcmp(file,"..") == 0) file = ".";
208         if (strcmp(pattern,".") == 0) return -1;
209
210         return ms_fnmatch(pattern, file);
211 }
212
213 static char *match_test(char *pattern, char *file, char *short_name)
214 {
215         static char ret[4];
216         strncpy(ret, "---", 3);
217
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] = '+';
222         return ret;
223 }
224
225  int main(int argc, char *argv[])
226 {
227         int ret;
228         char ans[4], mask[100], file[100], mfile[100];
229         char *ans2;
230         int n, i=0;
231         char line[200];
232
233         if (argc == 3) {
234                 ret = ms_fnmatch(argv[1], argv[2]);
235                 if (ret == 0) 
236                         printf("YES\n");
237                 else printf("NO\n");
238                 return ret;
239         }
240         mfile[0] = 0;
241
242         while (fgets(line, sizeof(line)-1, stdin)) {
243                 n = sscanf(line, "%3s %s %s %s\n", ans, mask, file, mfile);
244                 if (n < 3) continue;
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);
249                 }
250                 i++;
251                 mfile[0] = 0;
252         }
253         return 0;
254 }
255 #endif /* FNMATCH_TEST */
256