-
Notifications
You must be signed in to change notification settings - Fork 4
/
pmatch.c
163 lines (138 loc) · 3.1 KB
/
pmatch.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
/* $VER: vlink pmatch.c V0.9f (04.08.04)
*
* This file is part of vlink, a portable linker for multiple
* object formats.
* Copyright (c) 1997-2005 Frank Wille
*
* vlink is freeware and part of the portable and retargetable ANSI C
* compiler vbcc, copyright (c) 1995-2005 by Volker Barthelmann.
* vlink may be freely redistributed as long as no modifications are
* made and nothing is charged for it. Non-commercial usage is allowed
* without any restrictions.
* EVERY PRODUCT OR PROGRAM DERIVED DIRECTLY FROM MY SOURCE MAY NOT BE
* SOLD COMMERCIALLY WITHOUT PERMISSION FROM THE AUTHOR.
*/
#define PMATCH_C
#include "vlink.h"
#ifdef AMIGAOS
#pragma amiga-align
#include <dos/dos.h>
#include <proto/dos.h>
#pragma default-align
#elif defined(_WIN32)
/* no headers needed */
#else /* UNIX */
#ifdef _SGI_SOURCE
#include <libgen.h>
#else
#include <fnmatch.h>
#endif
#endif
#ifdef AMIGAOS
bool pattern_match(const char *pat,const char *str)
{
char c;
char *pat1,*pat2;
LONG len;
bool rc = FALSE;
/* convert Unix to AmigaDos pattern */
pat2 = pat1 = alloc(2*strlen(pat)+1);
while (c = *pat++) {
if (c == '*') {
*pat2++ = '#';
*pat2++ = '?';
}
else
*pat2++ = c;
}
*pat2 = '\0';
/* tokenize pattern and match it against str */
len = 2*strlen(pat1)+3;
pat2 = alloc(len);
if (ParsePattern((STRPTR)pat1,(STRPTR)pat2,len) >= 0) {
if (MatchPattern((STRPTR)pat2,(STRPTR)str))
rc = TRUE;
}
else
ierror("pattern_match(): ParsePattern() failed for \"%s\"",pat);
free(pat2);
free(pat1);
return (rc);
}
#elif defined(_WIN32)
static bool portable_pattern_match(const char *mask, const char *name)
{
int wild = 0,
q = 0;
const char * m = mask,
* n = name,
* ma = mask,
* na = name;
for(;;) {
if (*m == '*') {
while (*m == '*')
++m;
wild = 1;
ma = m;
na = n;
}
if (!*m) {
if (!*n)
return(FALSE);
for (--m; (m > mask) && (*m == '?'); --m);
if ((*m == '*') && (m > mask) && (m[-1] != '\\'))
return(FALSE);
if (!wild)
return(TRUE);
m = ma;
}
else if (!*n) {
while(*m == '*')
++m;
return(*m != 0);
}
if ((*m == '\\') && ((m[1] == '*') || (m[1] == '?'))) {
++m;
q = 1;
}
else {
q = 0;
}
if ((tolower(*m) != tolower(*n)) && ((*m != '?') || q)) {
if (!wild)
return(TRUE);
m = ma;
n = ++na;
}
else {
if (*m) ++m;
if (*n) ++n;
}
}
}
bool pattern_match(const char *mask, const char *name)
{
return !portable_pattern_match(mask,name);
}
#else /* UNIX */
bool pattern_match(const char *pat,const char *str)
{
#ifdef _SGI_SOURCE
return (gmatch(str,pat) != 0);
#else
return (fnmatch(pat,str,0) == 0);
#endif
}
#endif
bool patternlist_match(char **patlist,const char *str)
/* match string against a list of patterns */
{
if (patlist) {
while (*patlist) {
if (pattern_match(*patlist,str))
return (TRUE);
patlist++;
}
}
return (FALSE);
}