#include <stdlib.h>
#include <glob.h>
#include <string.h>
+#include <stdbool.h>
#include "rename.h"
#include "config.h"
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);
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);
}
globfree(files);
files = NULL;
- free(statbuf);
- statbuf = NULL;
+
free(input_filename);
input_filename = NULL;
free(output_filename);