]> gitweb.hhaalo.de Git - mv_none_space.git/commitdiff
change directory check extra method
authorBastian Dehn <hhaalo@arcor.de>
Thu, 2 Oct 2025 08:04:53 +0000 (10:04 +0200)
committerBastian Dehn <hhaalo@arcor.de>
Thu, 2 Oct 2025 08:04:53 +0000 (10:04 +0200)
src/main.c

index 37bc8d308504c53f00e67f37d41703d43265172f..16b89c18aeb21c2b02e81a6332bfad2f384283c4 100644 (file)
@@ -3,6 +3,7 @@
 #include <stdlib.h>
 #include <glob.h>
 #include <string.h>
+#include <stdbool.h>
 #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);