#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
const char *directory = "data";
//mkdir is POSIX (on Windows _mkdir from direct.h);
//0755: the owner may write, everyone may read
bool created1 = mkdir(directory, 0755) == 0;
//created1 is true
//the directory is already there, and errno tells it
bool created2 = mkdir(directory, 0755) == 0;
bool exists = errno == EEXIST;
//created2 is false, exists is true
//mkdir makes one level only: "data/sub/inner" needs "data/sub" first
bool created3 = mkdir("data/sub/inner", 0755) == 0;
//created3 is false
mkdir("data/sub", 0755);
bool created4 = mkdir("data/sub/inner", 0755) == 0;
//created4 is true
printf("created1 is %s\n", created1 ? "true" : "false");
printf("created2 is %s\n", created2 ? "true" : "false");
printf("exists is %s\n", exists ? "true" : "false");
printf("created3 is %s\n", created3 ? "true" : "false");
printf("created4 is %s\n", created4 ? "true" : "false");
rmdir("data/sub/inner");
rmdir("data/sub");
rmdir(directory);