Skip to content

Instantly share code, notes, and snippets.

@sw17ch
Created May 15, 2015 03:25
Show Gist options
  • Save sw17ch/4f5a95a31efcabed7108 to your computer and use it in GitHub Desktop.
Save sw17ch/4f5a95a31efcabed7108 to your computer and use it in GitHub Desktop.
int recursive_remove(const char * const path) {
int e = 0;
const char * const ptrs[] = { path, 0 };
FTS * fts = fts_open((char * const *)ptrs, FTS_NOCHDIR | FTS_PHYSICAL | FTS_XDEV, NULL);
if (0 != errno) {
e = errno;
fprintf(stderr, "ERROR: %s\n", strerror(e));
} else {
FTSENT * ent;
while((ent = fts_read(fts))) {
switch(ent->fts_info) {
case FTS_NS: case FTS_DNR: case FTS_ERR:
fprintf(stderr, "%s: fts_read error: %s\n",
ent->fts_accpath, strerror(ent->fts_errno));
e = ent->fts_errno;
goto finish;
break;
case FTS_D: case FTS_DC: case FTS_DOT: case FTS_NSOK:
break;
case FTS_DP: case FTS_F: case FTS_SL: case FTS_SLNONE: case FTS_DEFAULT:
if (0 != remove(ent->fts_accpath)) {
fprintf(stderr, "Unable to remove %s. %s\n",
ent->fts_accpath,
strerror(ent->fts_errno));
e = ent->fts_errno;
goto finish;
}
break;
}
}
}
finish:
fts_close(fts);
return e;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment