From f9092e361420a44e6ed8e5a1fa13d3fad9f3d7de Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Wed, 6 Jan 2021 18:52:47 -0500 Subject: fix fs_list_directory --- filesystem-posix.c | 18 ++++++++---------- filesystem-win.c | 16 ++++++---------- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/filesystem-posix.c b/filesystem-posix.c index 4913e30..e2ff82e 100644 --- a/filesystem-posix.c +++ b/filesystem-posix.c @@ -12,7 +12,7 @@ static bool fs_file_exists(char const *path) { return S_ISREG(statbuf.st_mode); } -// Returns a NULL-terminated array of the files in this directory, or NULL if the directory does not exist. +// Returns a NULL-terminated array of the files/directories in this directory, or NULL if the directory does not exist. // When you're done with the file names, call free on each one, then on the array. // NOTE: The files aren't returned in any particular order! static char **fs_list_directory(char const *dirname) { @@ -29,15 +29,13 @@ static char **fs_list_directory(char const *dirname) { filenames = (char **)calloc(nentries+1, sizeof *filenames); while ((ent = readdir(dir))) { - if (ent->d_type == DT_REG) { - char const *filename = ent->d_name; - size_t len = strlen(filename); - char *filename_copy = (char *)malloc(len+1); - if (!filename_copy) break; - strcpy(filename_copy, filename); - if (filename_idx < nentries) // this could actually fail if someone creates files between calculating nentries and here. - filenames[filename_idx++] = filename_copy; - } + char const *filename = ent->d_name; + size_t len = strlen(filename); + char *filename_copy = (char *)malloc(len+1); + if (!filename_copy) break; + strcpy(filename_copy, filename); + if (filename_idx < nentries) // this could actually fail if someone creates files between calculating nentries and here. + filenames[filename_idx++] = filename_copy; } ret = filenames; closedir(dir); diff --git a/filesystem-win.c b/filesystem-win.c index 4a66c09..52dc64d 100644 --- a/filesystem-win.c +++ b/filesystem-win.c @@ -21,9 +21,7 @@ static char **fs_list_directory(char const *dirname) { int nfiles = 1, idx = 0; char **files; while (FindNextFile(fhandle, &find_data)) { - if (!(find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { - ++nfiles; - } + ++nfiles; } FindClose(fhandle); // now, fill out files array @@ -32,13 +30,11 @@ static char **fs_list_directory(char const *dirname) { fhandle = FindFirstFileA(file_pattern, &find_data); if (fhandle != INVALID_HANDLE_VALUE) { do { - if (!(find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { - if (idx < nfiles) { - char *dup = _strdup(find_data.cFileName); - if (dup) { - files[idx++] = dup; - } else break; // stop now - } + if (idx < nfiles) { + char *dup = _strdup(find_data.cFileName); + if (dup) { + files[idx++] = dup; + } else break; // stop now } } while (FindNextFile(fhandle, &find_data)); files[idx] = NULL; -- cgit v1.2.3