Added sys_fork() and sys_getpid() functions to stop the overhead
[abartlet/samba.git/.git] / source3 / 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 /* the following function was derived using the masktest utility -
35    after years of effort we finally have a perfect MS wildcard
36    matching routine! 
37
38    NOTE: this matches only filenames with no directory component
39
40    Returns 0 on match, -1 on fail.
41 */
42 int ms_fnmatch(char *pattern, char *string)
43 {
44         char *p = pattern, *n = string;
45         char c;
46
47         while ((c = *p++)) {
48                 switch (c) {
49                 case '?':
50                         if (! *n) return -1;
51                         n++;
52                         break;
53
54                 case '>':
55                         if (n[0] == '.') {
56                                 if (! n[1] && ms_fnmatch(p, n+1) == 0) return 0;
57                                 if (ms_fnmatch(p, n) == 0) return 0;
58                                 return -1;
59                         }
60                         if (! *n) return ms_fnmatch(p, n);
61                         n++;
62                         break;
63
64                 case '*':
65                         for (; *n; n++) {
66                                 if (ms_fnmatch(p, n) == 0) return 0;
67                         }
68                         break;
69
70                 case '<':
71                         for (; *n; n++) {
72                                 if (ms_fnmatch(p, n) == 0) return 0;
73                                 if (*n == '.' && !strchr(n+1,'.')) {
74                                         n++;
75                                         break;
76                                 }
77                         }
78                         break;
79
80                 case '"':
81                         if (*n == 0 && ms_fnmatch(p, n) == 0) return 0;
82                         if (*n != '.') return -1;
83                         n++;
84                         break;
85
86                 default:
87                         if (c != *n) return -1;
88                         n++;
89                 }
90         }
91         
92         if (! *n) return 0;
93         
94         return -1;
95 }
96
97
98 #if FNMATCH_TEST
99
100 static int match_one(char *pattern, char *file)
101 {
102         if (strcmp(file,"..") == 0) file = ".";
103         if (strcmp(pattern,".") == 0) return -1;
104
105         return ms_fnmatch(pattern, file);
106 }
107
108 static char *match_test(char *pattern, char *file, char *short_name)
109 {
110         static char ret[4];
111         strncpy(ret, "---", 3);
112
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] = '+';
117         return ret;
118 }
119
120  int main(int argc, char *argv[])
121 {
122         int ret;
123         char ans[4], mask[100], file[100], mfile[100];
124         char *ans2;
125         int n, i=0;
126         char line[200];
127
128         if (argc == 3) {
129                 ret = ms_fnmatch(argv[1], argv[2]);
130                 if (ret == 0) 
131                         printf("YES\n");
132                 else printf("NO\n");
133                 return ret;
134         }
135         mfile[0] = 0;
136
137         while (fgets(line, sizeof(line)-1, stdin)) {
138                 n = sscanf(line, "%3s %s %s %s\n", ans, mask, file, mfile);
139                 if (n < 3) continue;
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);
144                 }
145                 i++;
146                 mfile[0] = 0;
147         }
148         return 0;
149 }
150 #endif /* FNMATCH_TEST */
151