blob: 8cc55021c1cce06f2d22bedc2c804e07165632fd (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
#include <assert.h>
#include <errno.h>
#include <libgen.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include "util.h"
#include <stdio.h>
static void
posix_dirname(char *path, char *dname)
{
char p[PATH_MAX+1];
char *t;
assert(strlen(path) <= PATH_MAX);
strcpy(p, path);
t = dirname(path);
memmove(dname, t, strlen(t) + 1);
/* restore the path if dirname worked in-place */
if (t == path && path != dname) {
strcpy(path, p);
}
}
/** Make directory and all of its parents */
int
mkdirs(char *path, mode_t mode)
{
if (strcmp(path, "/") == 0 || strcmp(path, ".") == 0) {
return 0;
}
char dname[PATH_MAX + 1];
posix_dirname(path, dname);
if (mkdirs(dname, mode) != 0) {
return -1;
}
if (mkdir(path, mode) != 0 && errno != EEXIST) {
return -1;
}
errno = 0;
return 0;
}
|