Created
August 16, 2018 19:03
-
-
Save blippy/187b15228b85a694b6eb8eacb7c1ea08 to your computer and use it in GitHub Desktop.
Remove a direcotry recursively
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // http://www.linuxquestions.org/questions/programming-9/deleting-a-directory-using-c-in-linux-248696/ | |
| // remove directory recursively | |
| // include dirent.h sys/types.h | |
| int rmdir(const char *dirname) | |
| { | |
| DIR *dir; | |
| struct dirent *entry; | |
| char path[PATH_MAX]; | |
| if (path == NULL) { | |
| fprintf(stderr, "Out of memory error\n"); | |
| return 0; | |
| } | |
| dir = opendir(dirname); | |
| if (dir == NULL) { | |
| perror("Error opendir()"); | |
| return 0; | |
| } | |
| while ((entry = readdir(dir)) != NULL) { | |
| if (strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")) { | |
| snprintf(path, (size_t) PATH_MAX, "%s/%s", dirname, entry->d_name); | |
| if (entry->d_type == DT_DIR) { | |
| rmdir(path); | |
| } | |
| remove(path); | |
| } | |
| } | |
| closedir(dir); | |
| remove(dirname); | |
| return 1; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment