From 4128868692b1d245423a22c4c6832b7c576743ed Mon Sep 17 00:00:00 2001 From: Steve Date: Sun, 21 Feb 2016 17:24:21 +0000 Subject: [PATCH] Dynamically allocate number of files in list. --- src/defs.h | 2 -- src/system/io.c | 34 ++++++++++++++++++++++------------ 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/defs.h b/src/defs.h index aa9f01f..e5576d4 100644 --- a/src/defs.h +++ b/src/defs.h @@ -48,8 +48,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #define MAX_DESCRIPTION_LENGTH 512 #define MAX_FILENAME_LENGTH 1024 -#define MAX_LISTED_FILES 64 - #define NUM_TEXTURE_BUCKETS 32 #define MAX_STARS 500 diff --git a/src/system/io.c b/src/system/io.c index 02398c1..4096232 100644 --- a/src/system/io.c +++ b/src/system/io.c @@ -95,28 +95,38 @@ char *getFileLocation(char *filename) char **getFileList(char *dir, int *count) { DIR *d; - struct dirent *ent; int i; + struct dirent *ent; char **filenames; - filenames = malloc(sizeof(char*) * MAX_LISTED_FILES); - memset(filenames, 0, sizeof(char*) * MAX_LISTED_FILES); - - i = 0; - if ((d = opendir(dir)) != NULL) { while ((ent = readdir(d)) != NULL) { if (ent->d_name[0] != '.') { - filenames[i] = malloc(sizeof(char) * MAX_FILENAME_LENGTH); - - STRNCPY(filenames[i], ent->d_name, MAX_FILENAME_LENGTH); - - if (++i >= MAX_LISTED_FILES) + i++; + } + } + + 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++; } } }