-
Notifications
You must be signed in to change notification settings - Fork 0
/
match.c
174 lines (163 loc) · 3.83 KB
/
match.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* IRC - Internet Relay Chat, common/match.c
* Copyright (C) 1990 Jarkko Oikarinen
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 1, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
* Compare if a given string (name) matches the given
* mask (which can contain wild cards: '*' - match any
* number of chars, '?' - match any single character.
*
* return 0, if match
* 1, if no match
*/
/*
* Modificado por Alberto Alonso, para hacerlo "case-sensitive".
*/
/*
* match
*
* Rewritten by Andrea Cocito (Nemesi), November 1998.
*
*/
/****************** Nemesi's match() ***************/
int match(const char *mask, const char *string)
{
const char *m = mask, *s = string;
char ch;
const char *bm, *bs; /* Will be reg anyway on a decent CPU/compiler */
/* Process the "head" of the mask, if any */
while ((ch = *m++) && (ch != '*'))
switch (ch)
{
case '\\':
if (*m == '?' || *m == '*')
ch = *m++;
default:
if (*s != ch)
return 1;
case '?':
if (!*s++)
return 1;
};
if (!ch)
return *s;
/* We got a star: quickly find if/where we match the next char */
got_star:
bm = m; /* Next try rollback here */
while ((ch = *m++))
switch (ch)
{
case '?':
if (!*s++)
return 1;
case '*':
bm = m;
continue; /* while */
case '\\':
if (*m == '?' || *m == '*')
ch = *m++;
default:
goto break_while; /* C is structured ? */
};
break_while:
if (!ch)
return 0; /* mask ends with '*', we got it */
while (*s++ != ch)
if (!*s)
return 1;
bs = s; /* Next try start from here */
/* Check the rest of the "chunk" */
while ((ch = *m++))
{
switch (ch)
{
case '*':
goto got_star;
case '\\':
if (*m == '?' || *m == '*')
ch = *m++;
default:
if (*s != ch)
{
m = bm;
s = bs;
goto got_star;
};
case '?':
if (!*s++)
return 1;
};
};
if (*s)
{
m = bm;
s = bs;
goto got_star;
};
return 0;
}
/*
* collapse()
* Collapse a pattern string into minimal components.
* This particular version is "in place", so that it changes the pattern
* which is to be reduced to a "minimal" size.
*
* (C) Carlo Wood - 6 Oct 1998
* Speedup rewrite by Andrea Cocito, December 1998.
* Note that this new optimized alghoritm can *only* work in place.
*/
char *collapse(char *mask)
{
int star = 0;
char *m = mask;
char *b;
if (m)
{
do
{
if ((*m == '*') && ((m[1] == '*') || (m[1] == '?')))
{
b = m;
do
{
if (*m == '*')
star = 1;
else
{
if (star && (*m != '?'))
{
*b++ = '*';
star = 0;
};
*b++ = *m;
if ((*m == '\\') && ((m[1] == '*') || (m[1] == '?')))
*b++ = *++m;
};
}
while (*m++);
break;
}
else
{
if ((*m == '\\') && ((m[1] == '*') || (m[1] == '?')))
m++;
};
}
while (*m++);
};
return mask;
}