From 49412195efd93573eb96ddbae6e41099964fdb65 Mon Sep 17 00:00:00 2001 From: Christian C Date: Tue, 4 Mar 2025 17:28:07 -0800 Subject: Validate directory --- src/main.c | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index 4aaafac..3fd58a6 100644 --- a/src/main.c +++ b/src/main.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -19,6 +20,18 @@ #define FALSE 0 #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) { @@ -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); } } //----------------------------------------------- -- cgit v1.2.1