diff options
author | Christian C <cc@localhost> | 2025-03-04 17:28:07 -0800 |
---|---|---|
committer | Christian C <cc@localhost> | 2025-03-04 17:28:59 -0800 |
commit | 49412195efd93573eb96ddbae6e41099964fdb65 (patch) | |
tree | d4b9e6ceb5f7c44cfa08b161d571f4f6e7165839 | |
parent | 07fe1f42a98084ce5c3b9425f002546f27acd8ba (diff) |
Validate directory
-rw-r--r-- | src/main.c | 35 |
1 files changed, 25 insertions, 10 deletions
@@ -3,6 +3,7 @@ #include <time.h> #include <assert.h> #include <dirent.h> +#include <sys/stat.h> #include <string.h> #include <raylib.h> @@ -20,6 +21,18 @@ #define TRUE 1 //----------------------------------------------- +// Is directory +bool_t is_directory(char* dirname) { + struct stat st; + if (stat(dirname, &st) == 0) { + if (S_ISDIR(st.st_mode)) { + return TRUE; + } + } + return FALSE; +} + +//----------------------------------------------- // List directory char** lsdir(char* dirname) { DIR *d; @@ -145,18 +158,20 @@ int main(int argc, char** argv) free(file_list); } if (argc > 1) { - file_list = lsdir(argv[1]); - if (file_list) { - size_t index = 0; - while (1) { - char* fname = file_list[index]; - if (fname == NULL) { - break; + if (is_directory(argv[1])) { + file_list = lsdir(argv[1]); + if (file_list) { + size_t index = 0; + while (1) { + char* fname = file_list[index]; + if (fname == NULL) { + break; + } + printf("%s\n", fname); + free(file_list[index++]); } - printf("%s\n", fname); - free(file_list[index++]); + free(file_list); } - free(file_list); } } //----------------------------------------------- |