I have the following C code:
#include <ftw.h>
#define MAXPATHLEN 100
static time_t createtime = 0;
static char ftwpath[MAXPATHLEN];
[...]
ftw(somepath, get_writedir, 10);
if (ftwpath[0] == '\0') {
//Code assuming that the directory does not exist.
} else {
//Some code handeling
}
That is the method that ftw calls:
int get_writedir(const char *path, const struct stat *sb, int typeflag)
{
if (typeflag == FTW_D && sb->st_ctime > createtime) {
createtime = sb->st_ctime;
strlcpy(ftwpath, path, MAXPATHLEN);
}
return 0;
}
Generally speking this code works to some extend when typeflag is set to FTW_F, not FTW_D. When I do the latter, nothing happens. When I do the prior: I do not always get the "newest created directory". What am I doing wrong here?