Dynamically allocate number of files in list.

This commit is contained in:
Steve 2016-02-21 17:24:21 +00:00
parent fd56b75a6a
commit 4128868692
2 changed files with 22 additions and 14 deletions

View File

@ -48,8 +48,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#define MAX_DESCRIPTION_LENGTH 512 #define MAX_DESCRIPTION_LENGTH 512
#define MAX_FILENAME_LENGTH 1024 #define MAX_FILENAME_LENGTH 1024
#define MAX_LISTED_FILES 64
#define NUM_TEXTURE_BUCKETS 32 #define NUM_TEXTURE_BUCKETS 32
#define MAX_STARS 500 #define MAX_STARS 500

View File

@ -95,28 +95,38 @@ char *getFileLocation(char *filename)
char **getFileList(char *dir, int *count) char **getFileList(char *dir, int *count)
{ {
DIR *d; DIR *d;
struct dirent *ent;
int i; int i;
struct dirent *ent;
char **filenames; char **filenames;
filenames = malloc(sizeof(char*) * MAX_LISTED_FILES);
memset(filenames, 0, sizeof(char*) * MAX_LISTED_FILES);
i = 0;
if ((d = opendir(dir)) != NULL) if ((d = opendir(dir)) != NULL)
{ {
while ((ent = readdir(d)) != NULL) while ((ent = readdir(d)) != NULL)
{ {
if (ent->d_name[0] != '.') if (ent->d_name[0] != '.')
{ {
filenames[i] = malloc(sizeof(char) * MAX_FILENAME_LENGTH); i++;
}
STRNCPY(filenames[i], ent->d_name, MAX_FILENAME_LENGTH); }
if (++i >= MAX_LISTED_FILES) if (i > 0)
{
filenames = malloc(sizeof(char*) * i);
memset(filenames, 0, sizeof(char*) * i);
rewinddir(d);
i = 0;
while ((ent = readdir(d)) != NULL)
{
if (ent->d_name[0] != '.')
{ {
break; filenames[i] = malloc(sizeof(char) * MAX_FILENAME_LENGTH);
STRNCPY(filenames[i], ent->d_name, MAX_FILENAME_LENGTH);
i++;
} }
} }
} }