20 #include <winpr/config.h>
22 #include <winpr/crt.h>
23 #include <winpr/handle.h>
25 #include <winpr/file.h>
27 #ifdef WINPR_HAVE_UNISTD_H
31 #ifdef WINPR_HAVE_FCNTL_H
36 #define TAG WINPR_TAG("file")
43 LPSTR FilePatternFindNextWildcardA(LPCSTR lpPattern, DWORD* pFlags)
45 LPSTR lpWildcard = NULL;
47 lpWildcard = strpbrk(lpPattern,
"*?~");
51 if (*lpWildcard ==
'*')
53 *pFlags = WILDCARD_STAR;
56 else if (*lpWildcard ==
'?')
58 *pFlags = WILDCARD_QM;
61 else if (*lpWildcard ==
'~')
63 if (lpWildcard[1] ==
'*')
65 *pFlags = WILDCARD_DOS_STAR;
68 else if (lpWildcard[1] ==
'?')
70 *pFlags = WILDCARD_DOS_QM;
73 else if (lpWildcard[1] ==
'.')
75 *pFlags = WILDCARD_DOS_DOT;
84 static BOOL FilePatternMatchSubExpressionA(LPCSTR lpFileName,
size_t cchFileName, LPCSTR lpX,
85 size_t cchX, LPCSTR lpY,
size_t cchY, LPCSTR lpWildcard,
88 LPCSTR lpMatch = NULL;
93 if (*lpWildcard ==
'*')
105 if (_strnicmp(lpFileName, lpX, cchX) != 0)
121 lpMatch = strchr(&lpFileName[cchX], *lpY);
126 if (_strnicmp(lpMatch, lpY, cchY) != 0)
131 lpMatch = &lpFileName[cchFileName];
137 *ppMatchEnd = &lpMatch[cchY];
140 else if (*lpWildcard ==
'?')
150 if (cchFileName < cchX)
153 if (_strnicmp(lpFileName, lpX, cchX) != 0)
167 lpMatch = strchr(&lpFileName[cchX + 1], *lpY);
172 if (_strnicmp(lpMatch, lpY, cchY) != 0)
177 if ((cchX + 1) > cchFileName)
180 lpMatch = &lpFileName[cchX + 1];
186 *ppMatchEnd = &lpMatch[cchY];
189 else if (*lpWildcard ==
'~')
191 WLog_ERR(TAG,
"warning: unimplemented '~' pattern match");
198 BOOL FilePatternMatchA(LPCSTR lpFileName, LPCSTR lpPattern)
201 LPCSTR lpTail = NULL;
203 size_t cchPattern = 0;
204 size_t cchFileName = 0;
206 DWORD dwNextFlags = 0;
207 LPSTR lpWildcard = NULL;
208 LPSTR lpNextWildcard = NULL;
230 cchPattern = strlen(lpPattern);
231 cchFileName = strlen(lpFileName);
240 if ((lpPattern[0] ==
'*') && (cchPattern == 1))
253 if (lpPattern[0] ==
'*')
255 lpTail = &lpPattern[1];
256 cchTail = strlen(lpTail);
258 if (!FilePatternFindNextWildcardA(lpTail, &dwFlags))
261 if (cchFileName < cchTail)
264 if (_stricmp(&lpFileName[cchFileName - cchTail], lpTail) == 0)
304 lpWildcard = FilePatternFindNextWildcardA(lpPattern, &dwFlags);
312 LPCSTR lpMatchEnd = NULL;
313 LPCSTR lpSubPattern = NULL;
314 size_t cchSubPattern = 0;
315 LPCSTR lpSubFileName = NULL;
316 size_t cchSubFileName = 0;
317 size_t cchWildcard = 0;
318 size_t cchNextWildcard = 0;
319 cchSubPattern = cchPattern;
320 lpSubPattern = lpPattern;
321 cchSubFileName = cchFileName;
322 lpSubFileName = lpFileName;
323 cchWildcard = ((dwFlags & WILDCARD_DOS) ? 2 : 1);
324 lpNextWildcard = FilePatternFindNextWildcardA(&lpWildcard[cchWildcard], &dwNextFlags);
329 cchX = (lpWildcard - lpSubPattern);
330 lpY = &lpSubPattern[cchX + cchWildcard];
331 cchY = (cchSubPattern - (lpY - lpSubPattern));
332 match = FilePatternMatchSubExpressionA(lpSubFileName, cchSubFileName, lpX, cchX, lpY,
333 cchY, lpWildcard, &lpMatchEnd);
338 while (lpNextWildcard)
340 cchSubFileName = cchFileName - (lpSubFileName - lpFileName);
341 cchNextWildcard = ((dwNextFlags & WILDCARD_DOS) ? 2 : 1);
343 cchX = (lpWildcard - lpSubPattern);
344 lpY = &lpSubPattern[cchX + cchWildcard];
345 cchY = (lpNextWildcard - lpWildcard) - cchWildcard;
346 match = FilePatternMatchSubExpressionA(lpSubFileName, cchSubFileName, lpX, cchX,
347 lpY, cchY, lpWildcard, &lpMatchEnd);
352 lpSubFileName = lpMatchEnd;
353 cchWildcard = cchNextWildcard;
354 lpWildcard = lpNextWildcard;
355 dwFlags = dwNextFlags;
357 FilePatternFindNextWildcardA(&lpWildcard[cchWildcard], &dwNextFlags);
366 if (_stricmp(lpFileName, lpPattern) == 0)