Created
July 21, 2016 08:01
-
-
Save kkrolikowski/e0caae04454548a8e03f03ed2ee923f8 to your computer and use it in GitHub Desktop.
Purge directory with it's contents
This file contains 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
void purgeDir(char * name) { | |
DIR * d; | |
struct dirent * entry; | |
const int pathlen = 256; | |
char buff[pathlen]; | |
memset(buff, '\0', pathlen); | |
strcpy(buff, name); | |
d = opendir(name); | |
while((entry = readdir(d)) != NULL) { | |
if(!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) | |
continue; | |
if(entry->d_type == 4) { | |
strcat(buff, entry->d_name); | |
strcat(buff, "/"); | |
purgeDir(buff); | |
} | |
else { | |
strcat(buff, entry->d_name); | |
unlink(buff); | |
} | |
memset(buff, '\0', pathlen); | |
strcpy(buff, name); | |
} | |
closedir(d); | |
rmdir(name); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment