From 7fcd3816de0227523fa32c1236fda460fbf26fbd Mon Sep 17 00:00:00 2001 From: Bastian Dehn Date: Tue, 16 Jul 2024 14:56:52 +0200 Subject: [PATCH] add rename main logic --- src/CMakeLists.txt | 2 +- src/main.c | 44 +++++++++++++++++++++++++++++++++----------- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 748fdc6..b84ee27 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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 diff --git a/src/main.c b/src/main.c index f4aa356..6f58bab 100644 --- a/src/main.c +++ b/src/main.c @@ -1,30 +1,52 @@ #include #include +#include #include +#include +#include "rename.h" -int main(int argc, char* argv[]) +void rename_files(const char* dir_path) { - if (argc < 2) { - printf("ERROR: %s \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 \n", argv[0]); + return 1; + } + + rename_files(argv[1]); + return 0; } \ No newline at end of file -- 2.39.5