26 lines
400 B
C
26 lines
400 B
C
#include <unistd.h>
|
|
#include <sys/stat.h>
|
|
#include <fcntl.h>
|
|
#include <string.h>
|
|
#include <errno.h>
|
|
|
|
|
|
int makeFileAndParents(const char *path) {
|
|
char buf[4096];
|
|
|
|
strcpy(buf, path);
|
|
|
|
for (char *p = buf + 1; *p; p++) {
|
|
if (*p == '/') {
|
|
*p = '\0';
|
|
|
|
if (mkdir(buf, 0755) == -1 && errno != EEXIST)
|
|
return -1;
|
|
|
|
*p = '/';
|
|
}
|
|
}
|
|
|
|
return open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
|
}
|
|
|