fnmatch: use the system one if available

If configure detects fnmatch to be available, use that instead of our
custom one for FTP wildcard pattern matching. For standard compliance,
to reduce our footprint and to use already well tested and well
exercised code.

A POSIX fnmatch behaves slightly different than the internal function
for a few test patterns currently and the macOS one yet slightly
different. Test case 1307 is adjusted for these differences.

Closes #2626
This commit is contained in:
Daniel Stenberg 2018-05-31 15:57:54 +02:00
parent 6d8c628912
commit a115c6bbe7
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
3 changed files with 119 additions and 19 deletions

View file

@ -30,6 +30,17 @@
/* The last #include file should be: */
#include "memdebug.h"
#ifndef HAVE_FNMATCH
/*
* TODO:
*
* Make this function match POSIX. Test 1307 includes a set of test patterns
* that returns different results with a POSIX fnmatch() than with this
* implemenation and this is considered a bug where POSIX is the guiding
* light.
*/
#define CURLFNM_CHARSET_LEN (sizeof(char) * 256)
#define CURLFNM_CHSET_SIZE (CURLFNM_CHARSET_LEN + 15)
@ -357,3 +368,29 @@ int Curl_fnmatch(void *ptr, const char *pattern, const char *string)
}
return loop((unsigned char *)pattern, (unsigned char *)string, 2);
}
#else
#include <fnmatch.h>
/*
* @unittest: 1307
*/
int Curl_fnmatch(void *ptr, const char *pattern, const char *string)
{
int rc;
(void)ptr; /* the argument is specified by the curl_fnmatch_callback
prototype, but not used by Curl_fnmatch() */
if(!pattern || !string) {
return CURL_FNMATCH_FAIL;
}
rc = fnmatch(pattern, string, 0);
switch(rc) {
case 0:
return CURL_FNMATCH_MATCH;
case FNM_NOMATCH:
return CURL_FNMATCH_NOMATCH;
default:
return CURL_FNMATCH_FAIL;
}
/* not reached */
}
#endif