diff --git a/src/namedos.c b/src/namedos.c index 12b738c..02fd010 100644 --- a/src/namedos.c +++ b/src/namedos.c @@ -181,13 +181,47 @@ static int dos83_alias_used_in_dir(const char *dir_unix, strncpy((char *)other, dirbuff->d_name, sizeof(other) - 1); other[sizeof(other) - 1] = '\0'; up_fn(other); - } else { - dos83_build_alias_candidate((uint8 *)dirbuff->d_name, 1, other, sizeof(other)); - } - if (!strcmp((char *)other, (const char *)candidate)) { - closedir(d); - return 1; + if (!strcmp((char *)other, (const char *)candidate)) { + closedir(d); + return 1; + } + } else { + char base[260], ext[260]; + char nbase[9], next[4]; + + /* + * First test the same unnumbered short alias that dos83_build_name_in_dir() + * tries before falling back to ~n aliases. This prevents two different long + * filenames like "long_file_name.txt" and "long_file_name_too.txt" from + * both receiving "LONG_FIL.TXT". + */ + dos83_split_name((uint8 *)dirbuff->d_name, base, sizeof(base), ext, sizeof(ext)); + dos83_normalize_part(base, nbase, sizeof(nbase), 8); + dos83_normalize_part(ext, next, sizeof(next), 3); + + if (*nbase) { + if (*next) + snprintf((char *)other, sizeof(other), "%s.%s", nbase, next); + else + snprintf((char *)other, sizeof(other), "%s", nbase); + + if (!strcmp((char *)other, (const char *)candidate)) { + closedir(d); + return 1; + } + } + + /* + * Also test the first numbered alias candidate. This keeps the old behavior + * for collision checks once numbering starts. + */ + dos83_build_alias_candidate((uint8 *)dirbuff->d_name, 1, other, sizeof(other)); + + if (!strcmp((char *)other, (const char *)candidate)) { + closedir(d); + return 1; + } } } @@ -212,12 +246,6 @@ int dos83_build_name_in_dir(const char *dir_unix, strncpy((char *)out, (const char *)src, out_size - 1); out[out_size - 1] = '\0'; up_fn(out); - XDPRINTF((2,0, - "DOS83 BUILD dir='%s' src='%s' out='%s' valid=%d", - dir_unix ? dir_unix : "(null)", - src ? (char *)src : "(null)", - out ? (char *)out : "(null)", - dos83_is_valid_name(src, options))); return (int)strlen((char *)out); } @@ -231,28 +259,14 @@ int dos83_build_name_in_dir(const char *dir_unix, else snprintf((char *)out, out_size, "%s", nbase); - if (!dir_unix || !dos83_alias_used_in_dir(dir_unix, src, out, options)) { - XDPRINTF((2,0, - "DOS83 BUILD dir='%s' src='%s' out='%s' valid=%d", - dir_unix ? dir_unix : "(null)", - src ? (char *)src : "(null)", - out ? (char *)out : "(null)", - dos83_is_valid_name(src, options))); + if (!dir_unix || !dos83_alias_used_in_dir(dir_unix, src, out, options)) return (int)strlen((char *)out); - } } for (seq = 1; seq < 1000000; seq++) { dos83_build_alias_candidate(src, seq, out, out_size); - if (!dir_unix || !dos83_alias_used_in_dir(dir_unix, src, out, options)) { - XDPRINTF((2,0, - "DOS83 BUILD dir='%s' src='%s' out='%s' valid=%d", - dir_unix ? dir_unix : "(null)", - src ? (char *)src : "(null)", - out ? (char *)out : "(null)", - dos83_is_valid_name(src, options))); + if (!dir_unix || !dos83_alias_used_in_dir(dir_unix, src, out, options)) return (int)strlen((char *)out); - } } strncpy((char *)out, "FILE~1", out_size - 1); @@ -301,17 +315,7 @@ int dos83_match_name_in_dir(const char *dir_unix, out_dos_name[out_dos_name_size - 1] = '\0'; } - { - int match_result = dos83_x_str_match(dos_name, dos_pattern, options); - XDPRINTF((2,0, - "DOS83 MATCH dir='%s' unix='%s' pattern='%s' alias='%s' result=%d", - dir_unix ? dir_unix : "(null)", - unix_name ? (char *)unix_name : "(null)", - dos_pattern ? (char *)dos_pattern : "(null)", - dos_name, - match_result)); - return match_result; - } + return dos83_x_str_match(dos_name, dos_pattern, options); } int dos83_resolve_component(const char *dir_unix,