Skip to content

Instantly share code, notes, and snippets.

@kkrolikowski
Created July 21, 2016 08:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kkrolikowski/e0caae04454548a8e03f03ed2ee923f8 to your computer and use it in GitHub Desktop.
Save kkrolikowski/e0caae04454548a8e03f03ed2ee923f8 to your computer and use it in GitHub Desktop.
Purge directory with it's contents
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