Use the nftw() (File Tree Walk) function, with the FTW_DEPTH
flag. Provide a callback that just calls remove() on the passed file:
#define _XOPEN_SOURCE 500
#include
#include
#include
int unlink_cb(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf)
{
int rv = remove(fpath);
if (rv)
perror(fpath);
return rv;
}
int rmrf(char *path)
{
return nftw(path, unlink_cb, 64, FTW_DEPTH | FTW_PHYS);
}