From 5ce212ed0b7abbbb862df0cf436688046a012da2 Mon Sep 17 00:00:00 2001 From: Abdellah El Morabit Date: Thu, 27 Feb 2025 23:17:17 +0100 Subject: fixed the recursive folder deletion, almost everything works now, execept for the copy i think, but will look at that in the furture --- main.c | 51 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 21 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index f0f2e94..ed2aada 100644 --- a/main.c +++ b/main.c @@ -171,36 +171,45 @@ int if_directory(const char *path) { perror("failed to get info stat about the given file"); }; - return S_ISDIR(info.st_mode) ? 1 : 0; + return S_ISDIR(info.st_mode) ? 0 : 1; } void remove_dir(const char *path) { - if (rmdir(path) == 0) { - printf("successfully deleted the directory"); - } else { - DIR *dP = opendir(path); + DIR *dP = opendir(path); + + if (dP == NULL) { + perror("failed to open the directory to delete the files recursively"); + return; + } else + { printf("opened the directory"); + } + struct dirent *entry; + + while ((entry = readdir(dP)) != NULL) { + printf("%s\n", entry->d_name); - if (dP == NULL) { - perror("failed to open the directory to delete the files recursively"); - } else { - struct dirent *entry; - while ((entry = readdir(dP)) != NULL) { - const char *new_path = entry->d_name; - if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) { - continue; - } else if ((if_directory(entry->d_name) == 0)) { - remove_dir(new_path); - } else if (unlink(entry->d_name) != 0) { - perror("failed to remove the file 003"); - } - } - closedir(dP); + char full_path[MAX_PATH_LENGTH]; + if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) { + continue; } - printf("successfully finished the removal of %s", path); + snprintf(full_path, sizeof(full_path), "%s/%s", path, entry->d_name); + if (if_directory(full_path) == 0) { + remove_dir(full_path); + } else if (remove(full_path) != 0) { + perror("failed to remove the file 003"); + } + } + closedir(dP); + + if (rmdir(path) != 0) { + perror("failed to remove the directory"); + } else { + printf("successfully removed the directory"); } } + void make_dir(const char *path) { if (mkdir(path, 0755) != 0) perror("failed to make directory"); -- cgit v1.2.3-70-g09d2