From 52a5a9d9bd8d767bd76c02f20668e1c7d92e33f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20L=2E=20=C5=A0ijanec?= Date: Wed, 29 Apr 2020 22:38:41 +0200 Subject: setting up folder structure --- lib/mkdirp.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 lib/mkdirp.c (limited to 'lib/mkdirp.c') diff --git a/lib/mkdirp.c b/lib/mkdirp.c new file mode 100644 index 0000000..8c79cf4 --- /dev/null +++ b/lib/mkdirp.c @@ -0,0 +1,46 @@ +// borrowed from https://gist.github.com/JonathonReinhart/8c0d90191c38af2dcadb102c4e202950 + +#include +#include /* PATH_MAX */ +#include /* mkdir(2) */ +#include +#ifndef PATH_MAX +#define PATH_MAX 255 +#endif +int mkdir_p(const char *path) { + /* Adapted from http://stackoverflow.com/a/2336245/119527 */ + const size_t len = strlen(path); + char _path[PATH_MAX]; + char *p; + + errno = 0; + + /* Copy string so its mutable */ + if (len > sizeof(_path)-1) { + errno = ENAMETOOLONG; + return -1; + } + strcpy(_path, path); + + /* Iterate the string */ + for (p = _path + 1; *p; p++) { + if (*p == '/') { + /* Temporarily truncate */ + *p = '\0'; + + if (mkdir(_path, S_IRWXU) != 0) { + if (errno != EEXIST) + return -1; + } + + *p = '/'; + } + } + + if (mkdir(_path, S_IRWXU) != 0) { + if (errno != EEXIST) + return -1; + } + + return 0; +} -- cgit v1.2.3