summaryrefslogtreecommitdiff
path: root/filesystem-win.c
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2021-04-13 12:29:32 -0400
committerLeo Tenenbaum <pommicket@gmail.com>2021-04-13 12:29:32 -0400
commit69789c042460e012ec3054cc2e6ceeff25e394b9 (patch)
tree0bb6fcd54a4e0f85ba430a2e3e2c54af258d797d /filesystem-win.c
parent2007d299916be6b079e6b97aea7c4d2835e23bd0 (diff)
change fs_list_directory to include entry types directly
Diffstat (limited to 'filesystem-win.c')
-rw-r--r--filesystem-win.c26
1 files changed, 17 insertions, 9 deletions
diff --git a/filesystem-win.c b/filesystem-win.c
index c6c64dd..d0aef37 100644
--- a/filesystem-win.c
+++ b/filesystem-win.c
@@ -25,9 +25,9 @@ bool fs_file_exists(char const *path) {
return fs_path_type(path) == FS_FILE;
}
-char **fs_list_directory(char const *dirname) {
+FsDirectoryEntry **fs_list_directory(char const *dirname) {
char file_pattern[256] = {0};
- char **ret = NULL;
+ FsDirectoryEntry **files = NULL;
WIN32_FIND_DATA find_data;
HANDLE fhandle;
assert(*dirname);
@@ -43,25 +43,33 @@ char **fs_list_directory(char const *dirname) {
}
FindClose(fhandle);
// now, fill out files array
- files = malloc((nfiles + 1) * sizeof *files);
+ files = calloc(nfiles + 1, sizeof *files);
if (files) {
fhandle = FindFirstFileA(file_pattern, &find_data);
if (fhandle != INVALID_HANDLE_VALUE) {
do {
if (idx < nfiles) {
- char *dup = _strdup(find_data.cFileName);
- if (dup) {
- files[idx++] = dup;
+ const char *filename = find_data.cFileName;
+ size_t len = strlen(filename);
+ FsDirectoryEntry *entry = calloc(1, sizeof *entry + len + 1);
+ if (entry) {
+ DWORD attrs = find_data.dwFileAttributes;
+ if (attrs & FILE_ATTRIBUTE_NORMAL)
+ entry->type = FS_FILE;
+ else if (attrs & FILE_ATTRIBUTE_DIRECTORY)
+ entry->type = FS_DIRECTORY;
+ else
+ entry->type = FS_OTHER;
+ memcpy(entry->name, filename, len);
+ files[idx++] = entry;
} else break; // stop now
}
} while (FindNextFile(fhandle, &find_data));
- files[idx] = NULL;
FindClose(fhandle);
- ret = files;
}
}
}
- return ret;
+ return files;
}
int fs_mkdir(char const *path) {