+ const char *asterisk, *wildcard, *nextseparator, *prevseparator;
+ char subpath[MAX_OSPATH];
+ char subpattern[MAX_OSPATH];
+
+ // find the next wildcard
+ wildcard = strchr(start, '?');
+ asterisk = strchr(start, '*');
+ if (asterisk && (!wildcard || asterisk < wildcard))
+ {
+ wildcard = asterisk;
+ }
+
+ if (wildcard)
+ {
+ nextseparator = strchr( wildcard, '/' );
+ }
+ else
+ {
+ nextseparator = NULL;
+ }
+
+ if( !nextseparator ) {
+ nextseparator = start + strlen( start );
+ }
+
+ // copy everything up except nextseperator
+ strlcpy(subpattern, pattern, min(sizeof(subpattern), nextseparator - pattern + 1));
+ // find the last /
+ prevseparator = strrchr( subpattern, '/' ) + 1;
+ if (!prevseparator)
+ {
+ prevseparator = subpattern;
+ }
+ // copy everything including the last '/'
+ strlcpy(subpath, start, min(sizeof(subpath), (prevseparator - subpattern) - (start - pattern) + 1));
+
+ // prevseparator points to the one right before the wildcard and nextseparator to the one following it (or past the end of the string (at \0))
+ // start to prevseparator can be opened now and added to the other resultset
+ for( dirlistindex = 0 ; dirlistindex < matchedSet.numstrings ; dirlistindex++ ) {
+ strlcpy( temp, matchedSet.strings[ dirlistindex ], sizeof(temp) );
+ strlcat( temp, subpath, sizeof(temp) );
+ listdirectory( &foundSet, searchpath->filename, temp );
+ }
+ if( dirlistindex == 0 ) {
+ break;
+ }
+ // reset the current result set
+ stringlistfreecontents( &matchedSet );
+ // match against the pattern
+ for( dirlistindex = 0 ; dirlistindex < foundSet.numstrings ; dirlistindex++ ) {
+ const char *direntry = foundSet.strings[ dirlistindex ];
+ if (matchpattern(direntry, subpattern, true)) {
+ stringlistappend( &matchedSet, direntry );
+ }
+ }
+ stringlistfreecontents( &foundSet );
+
+ start = nextseparator;
+ }
+
+ for (dirlistindex = 0;dirlistindex < matchedSet.numstrings;dirlistindex++)
+ {
+ const char *temp = matchedSet.strings[dirlistindex];
+ if (matchpattern(temp, (char *)pattern, true))