8 * Emulates mkdir -p (creates any missing folders)
13 int mkdirp(const char *path, mode_t mode) {
14 if (mkdir(path, mode) == 0)
16 if (errno == EEXIST) {
18 /* Check that the named file actually is a directory. */
19 if (stat(path, &st)) {
20 ELOG("stat(%s) failed: %s\n", path, strerror(errno));
23 if (!S_ISDIR(st.st_mode)) {
24 ELOG("mkdir(%s) failed: %s\n", path, strerror(ENOTDIR));
28 } else if (errno != ENOENT) {
29 ELOG("mkdir(%s) failed: %s\n", path, strerror(errno));
32 char *copy = sstrdup(path);
33 /* strip trailing slashes, if any */
34 while (copy[strlen(copy) - 1] == '/')
35 copy[strlen(copy) - 1] = '\0';
37 char *sep = strrchr(copy, '/');
47 if (mkdirp(copy, mode) == 0)
48 result = mkdirp(path, mode);