]> gitweb.hhaalo.de Git - mv_none_space.git/commitdiff
add rename main logic
authorBastian Dehn <hhaalo@arcor.de>
Tue, 16 Jul 2024 12:56:52 +0000 (14:56 +0200)
committerBastian Dehn <hhaalo@arcor.de>
Tue, 16 Jul 2024 12:56:52 +0000 (14:56 +0200)
src/CMakeLists.txt
src/main.c

index 748fdc6da61ea9af6c44eb4115284d497d256447..b84ee2730951edee90772dbc973953ecf393fafe 100644 (file)
@@ -1,3 +1,3 @@
 CMAKE_MINIMUM_REQUIRED(VERSION 3.25.1)
 
-ADD_EXECUTABLE(${PROJECT_NAME} main.c)
\ No newline at end of file
+ADD_EXECUTABLE(${PROJECT_NAME} main.c rename.c)
\ No newline at end of file
index f4aa35635678f4a830e3db4a1b05ff096e47fad5..6f58bab6ec4d6e406556bc6625c77052ae65896e 100644 (file)
@@ -1,30 +1,52 @@
 #include <stdio.h>
 #include <sys/stat.h>
+#include <stdlib.h>
 #include <glob.h>
+#include <string.h>
+#include "rename.h"
 
-int main(int argc, char* argv[])
+void rename_files(const char* dir_path)
 {
-       if (argc < 2) {
-               printf("ERROR: %s <dir>\n", argv[0]);
-               return 1;
-       }
-
+       char* output_filename = malloc(sizeof(char) * STR_MAX_LENGTH);
+       memset(output_filename, 0, STR_MAX_LENGTH);
        glob_t files;
        struct stat statbuf;
-       glob("*", 0, NULL, &files);
+
+       int len = strlen(dir_path);
+       char* pattern = malloc(sizeof(char) * len + 3);
+       sprintf(pattern, "%s/*", dir_path);
+
+       glob(pattern, 0, NULL, &files);
 
        printf("count: %ld\n", files.gl_pathc);
 
        for (int i = 0; i < files.gl_pathc; i++) {
                stat(files.gl_pathv[i], &statbuf);
-               if (S_ISREG(statbuf.st_mode)) {
-                       printf("filename: %s\n", files.gl_pathv[i]);
-               }
+               rename_lower_str(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);
                if (S_ISDIR(statbuf.st_mode)) {
-                       printf("directory: %s\n", files.gl_pathv[i]);
+                       rename_files(files.gl_pathv[i]);
                }
+
+               memset(output_filename, 0, STR_MAX_LENGTH);
        }
 
        globfree(&files);
+       free(output_filename);
+       output_filename = NULL;
+       free(pattern);
+       pattern = NULL;
+}
+
+int main(int argc, char* argv[])
+{
+       if (argc < 2) {
+               printf("ERROR: %s <dir>\n", argv[0]);
+               return 1;
+       }
+
+       rename_files(argv[1]);
+
        return 0;
 }
\ No newline at end of file