2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
16 * Emulates mkdir -p (creates any missing folders)
21 int mkdirp(const char *path, mode_t mode) {
22 if (mkdir(path, mode) == 0)
24 if (errno == EEXIST) {
26 /* Check that the named file actually is a directory. */
27 if (stat(path, &st)) {
28 ELOG("stat(%s) failed: %s\n", path, strerror(errno));
31 if (!S_ISDIR(st.st_mode)) {
32 ELOG("mkdir(%s) failed: %s\n", path, strerror(ENOTDIR));
36 } else if (errno != ENOENT) {
37 ELOG("mkdir(%s) failed: %s\n", path, strerror(errno));
40 char *copy = sstrdup(path);
41 /* strip trailing slashes, if any */
42 while (copy[strlen(copy) - 1] == '/')
43 copy[strlen(copy) - 1] = '\0';
45 char *sep = strrchr(copy, '/');
55 if (mkdirp(copy, mode) == 0)
56 result = mkdirp(path, mode);