+ fssearch_t *search;
+ if(strchr(com_token, '/'))
+ {
+ search = FS_Search(com_token, true, true);
+ }
+ else
+ {
+ const char *slash = strrchr(s, '/');
+ if(slash)
+ {
+ strlcpy(t, s, min(sizeof(t), (unsigned int)(slash - s + 2))); // + 2, because I want to include the slash
+ strlcat(t, com_token, sizeof(t));
+ search = FS_Search(t, true, true);
+ }
+ else
+ search = FS_Search(com_token, true, true);
+ }
+ if(search)
+ {
+ for(i = 0; i < search->numfilenames; ++i)
+ if(!strncmp(search->filenames[i], s, strlen(s)))
+ if(FS_FileType(search->filenames[i]) == FS_FILETYPE_FILE)
+ stringlistappend(&resultbuf, search->filenames[i]);
+ FS_FreeSearch(search);
+ }
+ }
+
+ // In any case, add directory names
+ {
+ fssearch_t *search;
+ const char *slash = strrchr(s, '/');
+ if(slash)
+ {
+ strlcpy(t, s, min(sizeof(t), (unsigned int)(slash - s + 2))); // + 2, because I want to include the slash
+ strlcat(t, "*", sizeof(t));
+ search = FS_Search(t, true, true);
+ }
+ else
+ search = FS_Search("*", true, true);
+ if(search)
+ {
+ for(i = 0; i < search->numfilenames; ++i)
+ if(!strncmp(search->filenames[i], s, strlen(s)))
+ if(FS_FileType(search->filenames[i]) == FS_FILETYPE_DIRECTORY)
+ stringlistappend(&dirbuf, search->filenames[i]);
+ FS_FreeSearch(search);
+ }
+ }
+
+ if(resultbuf.numstrings > 0 || dirbuf.numstrings > 0)
+ {
+ const char *p, *q;
+ unsigned int matchchars;
+ if(resultbuf.numstrings == 0 && dirbuf.numstrings == 1)
+ {
+ dpsnprintf(t, sizeof(t), "%s/", dirbuf.strings[0]);
+ }
+ else
+ if(resultbuf.numstrings == 1 && dirbuf.numstrings == 0)
+ {
+ dpsnprintf(t, sizeof(t), "%s ", resultbuf.strings[0]);
+ }
+ else
+ {
+ stringlistsort(&resultbuf); // dirbuf is already sorted
+ Con_Printf("\n%i possible filenames\n", resultbuf.numstrings + dirbuf.numstrings);
+ for(i = 0; i < dirbuf.numstrings; ++i)
+ {
+ Con_Printf("%s/\n", dirbuf.strings[i]);
+ }
+ for(i = 0; i < resultbuf.numstrings; ++i)
+ {
+ Con_Printf("%s\n", resultbuf.strings[i]);
+ }
+ matchchars = sizeof(t) - 1;
+ if(resultbuf.numstrings > 0)
+ {
+ p = resultbuf.strings[0];
+ q = resultbuf.strings[resultbuf.numstrings - 1];
+ for(; *p && *p == *q; ++p, ++q);
+ matchchars = (unsigned int)(p - resultbuf.strings[0]);
+ }
+ if(dirbuf.numstrings > 0)
+ {
+ p = dirbuf.strings[0];
+ q = dirbuf.strings[dirbuf.numstrings - 1];
+ for(; *p && *p == *q; ++p, ++q);
+ matchchars = min(matchchars, (unsigned int)(p - dirbuf.strings[0]));
+ }
+ // now p points to the first non-equal character, or to the end
+ // of resultbuf.strings[0]. We want to append the characters
+ // from resultbuf.strings[0] to (not including) p as these are
+ // the unique prefix
+ strlcpy(t, (resultbuf.numstrings > 0 ? resultbuf : dirbuf).strings[0], min(matchchars + 1, sizeof(t)));
+ }
+