Yes, you are using stat
incorrectly because you aren't checking the return code for errors. If you did you will see that stat
is going to fail because a file that doesn't yet exist is not going pass muster (ENOENT) with stat
. So stat
will fail before you get to the S_ISDIR check.
You want something more along these lines:
struct stat mystat;
int ret = stat(dir.c_str(), &mystat);
if (ret == -1)
if (errno == ENOENT)
if ((ret = mkdir(dir.c_str(), 0700)) != -1)
isdir = false;
else
{
perror("mkdir");
exit(1);
}
else
{
perror("stat");
exit(1);
}