Enumerate files code: cleaned up memory leak, Doesn't throw away file entry

from FindFirstFile(), and handles paths more robustly.
This commit is contained in:
Ryan C. Gordon 2002-06-08 06:19:27 +00:00
parent a89efe6972
commit 1a665efb6a
1 changed files with 13 additions and 4 deletions

View File

@ -349,18 +349,27 @@ LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname,
HANDLE dir; HANDLE dir;
WIN32_FIND_DATA ent; WIN32_FIND_DATA ent;
char *SearchPath; char *SearchPath;
size_t len = strlen(dirname);
/* Allocate a new string for path, "*", and NULL terminator */ /* Allocate a new string for path, maybe '\\', "*", and NULL terminator */
SearchPath = malloc(strlen(dirname) + 2); SearchPath = alloca(len + 3);
/* Copy current dirname */ /* Copy current dirname */
strcpy(SearchPath, dirname); strcpy(SearchPath, dirname);
/* if there's no '\\' at the end of the path, stick one in there. */
if (dirname[len - 1] != '\\')
{
dirname[len++] = '\\';
dirname[len] = '\0';
} /* if */
/* Append the "*" to the end of the string */ /* Append the "*" to the end of the string */
strcat(SearchPath, "*"); strcat(SearchPath, "*");
dir = FindFirstFile(SearchPath, &ent); dir = FindFirstFile(SearchPath, &ent);
BAIL_IF_MACRO(dir == INVALID_HANDLE_VALUE, win32strerror(), NULL); BAIL_IF_MACRO(dir == INVALID_HANDLE_VALUE, win32strerror(), NULL);
while (FindNextFile(dir, &ent) != 0) do
{ {
if (strcmp(ent.cFileName, ".") == 0) if (strcmp(ent.cFileName, ".") == 0)
continue; continue;
@ -388,7 +397,7 @@ LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname,
prev = l; prev = l;
l->next = NULL; l->next = NULL;
} /* while */ } while (FindNextFile(dir, &ent) != 0);
FindClose(dir); FindClose(dir);
return(retval); return(retval);