mkpath.c - smdev - suckless mdev
(HTM) git clone git://git.suckless.org/smdev
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
mkpath.c (510B)
---
1 #include <sys/stat.h>
2 #include <errno.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <limits.h>
6
7 int
8 mkpath(const char *path, mode_t mode)
9 {
10 char tmp[PATH_MAX];
11 char *p = NULL;
12 size_t len;
13
14 snprintf(tmp, sizeof(tmp),"%s",path);
15 len = strlen(tmp);
16 if(tmp[len - 1] == '/')
17 tmp[len - 1] = 0;
18 for(p = tmp + 1; *p; p++)
19 if(*p == '/') {
20 *p = 0;
21 if (mkdir(tmp, mode) < 0 && errno != EEXIST)
22 return -1;
23 *p = '/';
24 }
25 if (mkdir(tmp, mode) < 0 && errno != EEXIST)
26 return -1;
27 return 0;
28 }
29