2019-01-29 22:03:33 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <alloca.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2019-02-04 15:21:19 +01:00
|
|
|
#include "util/util.h"
|
2019-01-19 17:26:41 +01:00
|
|
|
|
|
|
|
int mkdir_p(const char *path)
|
|
|
|
{
|
|
|
|
size_t i, len;
|
|
|
|
char *buffer;
|
|
|
|
|
2019-02-03 13:21:22 +01:00
|
|
|
while (path[0] == '/' && path[1] == '/')
|
2019-01-19 17:26:41 +01:00
|
|
|
++path;
|
|
|
|
|
2019-02-03 13:21:22 +01:00
|
|
|
if (*path == '\0' || (path[0] == '/' && path[1] == '\0'))
|
2019-01-19 17:26:41 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
len = strlen(path) + 1;
|
|
|
|
buffer = alloca(len);
|
|
|
|
|
|
|
|
for (i = 0; i < len; ++i) {
|
2019-02-03 13:21:22 +01:00
|
|
|
if (i > 0 && (path[i] == '/' || path[i] == '\0')) {
|
2019-01-19 17:26:41 +01:00
|
|
|
buffer[i] = '\0';
|
|
|
|
|
|
|
|
if (mkdir(buffer, 0755) != 0) {
|
|
|
|
if (errno != EEXIST) {
|
|
|
|
fprintf(stderr, "mkdir %s: %s\n",
|
|
|
|
buffer, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer[i] = path[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|