-
Notifications
You must be signed in to change notification settings - Fork 9
/
pmatch.c
154 lines (128 loc) · 2.73 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
/* $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
*/
#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) || defined(ATARI)
/* portable pattern matching routines - no headers */
#elif defined(_SGI_SOURCE)
#include <libgen.h>
#else /* UNIX */
#include <fnmatch.h>
#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) || defined(ATARI)
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);
}