mkdirp.c - sbase - suckless unix tools
(HTM) git clone git://git.suckless.org/sbase
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
mkdirp.c (717B)
---
1 /* See LICENSE file for copyright and license details. */
2 #include <sys/stat.h>
3
4 #include <errno.h>
5 #include <limits.h>
6
7 #include "../util.h"
8
9 int
10 mkdirp(const char *path, mode_t mode, mode_t pmode)
11 {
12 char tmp[PATH_MAX], *p;
13 struct stat st;
14
15 if (stat(path, &st) == 0) {
16 if (S_ISDIR(st.st_mode))
17 return 0;
18 errno = ENOTDIR;
19 weprintf("%s:", path);
20 return -1;
21 }
22
23 estrlcpy(tmp, path, sizeof(tmp));
24 for (p = tmp + (tmp[0] == '/'); *p; p++) {
25 if (*p != '/')
26 continue;
27 *p = '\0';
28 if (mkdir(tmp, pmode) < 0 && errno != EEXIST) {
29 weprintf("mkdir %s:", tmp);
30 return -1;
31 }
32 *p = '/';
33 }
34 if (mkdir(tmp, mode) < 0 && errno != EEXIST) {
35 weprintf("mkdir %s:", tmp);
36 return -1;
37 }
38 return 0;
39 }