From 3c7cea2fcd938367dbbaf1e262ec8655c8c0bdfe Mon Sep 17 00:00:00 2001 From: Bastian Dehn Date: Thu, 2 Oct 2025 10:04:53 +0200 Subject: [PATCH] change directory check extra method --- src/main.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/main.c b/src/main.c index 37bc8d3..16b89c1 100644 --- a/src/main.c +++ b/src/main.c @@ -3,6 +3,7 @@ #include #include #include +#include #include "rename.h" #include "config.h" @@ -19,9 +20,21 @@ glob_t* get_files(const char* dir_path) return files; } -void rename_files(const char* dir_path, rename_func_ptr renamefunc) +bool is_dir(const char* path) { struct stat* statbuf = malloc(sizeof(struct stat)); + + stat(path, statbuf); + bool isdir = S_ISDIR(statbuf->st_mode); + + free(statbuf); + statbuf = NULL; + + return isdir; +} + +void rename_files(const char* dir_path, rename_func_ptr renamefunc) +{ char* input_filename = malloc(sizeof(char) * STR_MAX_LENGTH); memset(input_filename, 0, STR_MAX_LENGTH); char* output_filename = malloc(sizeof(char) * STR_MAX_LENGTH); @@ -30,14 +43,13 @@ void rename_files(const char* dir_path, rename_func_ptr renamefunc) glob_t* files = get_files(dir_path); for (int i = 0; i < files->gl_pathc; i++) { - stat(files->gl_pathv[i], statbuf); renamefunc(files->gl_pathv[i], output_filename); if (strcmp(files->gl_pathv[i], output_filename) != 0) { printf("%s -> %s\n", files->gl_pathv[i], output_filename); rename(files->gl_pathv[i], output_filename); } - if (S_ISDIR(statbuf->st_mode)) { + if (is_dir(files->gl_pathv[i])) { escape_str(output_filename, input_filename); rename_files(input_filename, renamefunc); } @@ -48,8 +60,7 @@ void rename_files(const char* dir_path, rename_func_ptr renamefunc) globfree(files); files = NULL; - free(statbuf); - statbuf = NULL; + free(input_filename); input_filename = NULL; free(output_filename); -- 2.47.3