Enumerate files code: cleaned up memory leak, Doesn't throw away file entry
authorRyan C. Gordon <icculus@icculus.org>
Sat, 08 Jun 2002 06:19:27 +0000
changeset 268 0f47386615f0
parent 267 6b5590f20252
child 269 d41c0a399a99
Enumerate files code: cleaned up memory leak, Doesn't throw away file entry from FindFirstFile(), and handles paths more robustly.
platform/win32.c
--- a/platform/win32.c	Fri Jun 07 12:12:22 2002 +0000
+++ b/platform/win32.c	Sat Jun 08 06:19:27 2002 +0000
@@ -349,18 +349,27 @@
     HANDLE dir;
     WIN32_FIND_DATA ent;
     char *SearchPath;
+    size_t len = strlen(dirname);
 
-    /* Allocate a new string for path, "*", and NULL terminator */
-    SearchPath = malloc(strlen(dirname) + 2);
+    /* Allocate a new string for path, maybe '\\', "*", and NULL terminator */
+    SearchPath = alloca(len + 3);
     /* Copy current 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 */
     strcat(SearchPath, "*");
 
     dir = FindFirstFile(SearchPath, &ent);
     BAIL_IF_MACRO(dir == INVALID_HANDLE_VALUE, win32strerror(), NULL);
 
-    while (FindNextFile(dir, &ent) != 0)
+    do
     {
         if (strcmp(ent.cFileName, ".") == 0)
             continue;
@@ -388,7 +397,7 @@
 
         prev = l;
         l->next = NULL;
-    } /* while */
+    } while (FindNextFile(dir, &ent) != 0);
 
     FindClose(dir);
     return(retval);