aboutsummaryrefslogtreecommitdiff
path: root/sftsr.c
diff options
context:
space:
mode:
Diffstat (limited to 'sftsr.c')
-rw-r--r--sftsr.c95
1 files changed, 62 insertions, 33 deletions
diff --git a/sftsr.c b/sftsr.c
index 1387933..470ce31 100644
--- a/sftsr.c
+++ b/sftsr.c
@@ -266,43 +266,72 @@ static const char * get_basename(const char *path)
* into win32-like (as expected by VirtualBox). */
static void fix_wildcards(SHFLSTRING *str, bool expand_tilde)
{
- unsigned i;
-
- // If this is the standard ????????.??? pattern, replace with *
- // (and not *.*, since we want to accept files without extension).
- if (str->u16Length >= 8+1+3) {
- i = str->u16Length - (8+1+3);
- if (memcmp(&str->ach[i], "????????.???", (8+1+3)) == 0) {
- strcpy(&str->ach[i], "*");
- str->u16Length = i + 1;
- }
- } else if (str->u16Length >= 1+3) {
- // Also, if this ends with .???, replace it with *,
- // for the same reason.
- i = str->u16Length - (1+3);
- if (memcmp(&str->ach[i], ".???", (1+3)) == 0) {
- strcpy(&str->ach[i], "*");
- str->u16Length = i + 1;
+ unsigned i, o;
+ unsigned last_wildcard = 0; // we can assume char 0 is never a wildcard ('\')
+ unsigned last_period = 0;
+
+ // If pattern is hello???.txt, DOS expects to match
+ // hello123.txt, but also hello1.txt, and even hello.txt
+ // However in the host a hello???.txt will match exactly 3 chars.
+ // On the other hand, if the pattern is ???bye.txt, DOS matches
+ // exactly 123bye.txt but not 1byte.txt
+
+ // So the idea here is that if we find a sequence of ?s before the '.'
+ // we replace them with *s, which matches arbitrary number of chars.
+
+ for (i = 1, o = 1; i < str->u16Length; i++, o++) {
+ str->ach[o] = str->ach[i];
+ switch (str->ach[i]) {
+ case '?':
+ // First ? in the sequence..
+ if (last_wildcard == 0) last_wildcard = i;
+ break;
+ case '.':
+ last_period = i;
+ if (last_wildcard && (i - last_wildcard >= 2)) {
+ // A sequence of ?s that ends with a '.': replace it all with *
+ o = last_wildcard;
+ str->ach[o++] = '*';
+ str->ach[o] = '.';
+ }
+ // Note: don't reset last_wildcard until we find a real non-wildcard char
+ // The extension could still be '.???' (all wildcards)
+ break;
+ case '~':
+#if USE_LFN_TO_SHORT
+ if (expand_tilde) {
+ // This could be a shortened file name.
+ // Replace the tilde and any following characters with an '*'.
+ str->ach[o] = '*';
+ i = str->u16Length; // don't look at the rest of the pattern
+ break;
+ }
+#endif
+ /* fallthrough */
+ default:
+ last_wildcard = 0;
}
}
- for (i = 1; i < str->u16Length; i++) {
- // VirtualBox will only match N consecutive ?s to filenames longer than N,
- // while DOS doesn't care.
- // Replace consecutive ???? with ?***,
- // VirtualBox will merge consecutive *s so we don't have to.
- if ( str->ach[i] == '?' &&
- (str->ach[i-1] == '*' || str->ach[i-1] == '?') ) {
- str->ach[i] = '*';
- }
+ if (last_wildcard && (i - last_wildcard >= 2)) {
+ // We reached the end of the pattern with a sequence of ?s
+ // Either no extension or .??? or .x??
+ // The same logic applies: replace with *
+ o = last_wildcard;
+ str->ach[o++] = '*';
+ }
+ if (last_wildcard && last_period && last_wildcard == last_period + 1) {
+ // We reached the end of the pattern with .???
+ // This should also match files with no extension, so replace
+ // everything from '.' inclusive with *
+ o = last_period;
+ str->ach[o++] = '*';
+ }
- // If there is a '~' in the pattern search, assume that this is a shortened
- // file name. We have to replace the tilde and any following characters with an '*'.
- if (expand_tilde && str->ach[i] == '~') {
- strcpy(&str->ach[i], "*");
- str->u16Length = i + 1;
- break;
- }
+ if (o < str->u16Length) {
+ // After fixing wildcards, string may be shorter...
+ str->ach[o] = '\0';
+ str->u16Length = o;
}
}