Make dir in C, linux doesnt create sub-dirs (dir tree) - why?
I want to create a dir tree in C on linux. I wrote that code:
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
static int dirExists(const char *path)
{
struct stat info;
if(stat( path, &info ) != 0)
return 0;
else if(info.st_mode & S_IFDIR)
return 1;
else
return 0;
}
int main(int argc, char **argv)
{
const char *path = "./mydir/firstdir/";
if(!dirExists(path))
{
mode_t mask = umask(0);
if(mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO) == -1)
exit(-1);
umask(mask);
}
printf("%d\n", dirExists(path));
return 0;
}
Its ok when a path is a single dir, lets say, path = "./mydir" but when I
want to create a dir tree, for example: path = "./mydir/a/b/c/d/" dirs are
not created. Why?
No comments:
Post a Comment