From ae5e7d50c575b7b376b8e53f7f12c1fa03db7853 Mon Sep 17 00:00:00 2001 From: Bastian Dehn Date: Wed, 4 Mar 2026 20:20:48 +0100 Subject: [PATCH] return glob file on stack --- src/main.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/main.c b/src/main.c index 11f8573..46976a9 100644 --- a/src/main.c +++ b/src/main.c @@ -7,13 +7,13 @@ #include "rename.h" #include "config.h" -glob_t* get_files(const char* dir_path) +glob_t get_files(const char* dir_path) { - glob_t* files = malloc(sizeof(glob_t)); + glob_t files; char* pattern = malloc(sizeof(char) * strlen(dir_path) + 3); sprintf(pattern, "%s/*", dir_path); - glob(pattern, 0, NULL, files); + glob(pattern, 0, NULL, &files); free(pattern); pattern = NULL; @@ -41,11 +41,11 @@ void rename_file(const char* input, const char* output) void rename_files(const char* dir_path, rename_ptr renamefunc) { - glob_t* files = get_files(dir_path); - for (size_t i = 0; i < files->gl_pathc; i++) { - char* output = strdup(files->gl_pathv[i]); + glob_t files = get_files(dir_path); + for (size_t i = 0; i < files.gl_pathc; i++) { + char* output = strdup(files.gl_pathv[i]); output = renamefunc(&output); - rename_file(files->gl_pathv[i], output); + rename_file(files.gl_pathv[i], output); if (is_dir(output)) { output = escape(&output); @@ -56,9 +56,7 @@ void rename_files(const char* dir_path, rename_ptr renamefunc) output = NULL; } - globfree(files); - free(files); - files = NULL; + globfree(&files); } int main(int argc, char* argv[]) -- 2.47.3