diff options
author | sinanmohd <sinan@sinanmohd.com> | 2023-12-27 16:05:41 +0530 |
---|---|---|
committer | sinanmohd <sinan@sinanmohd.com> | 2023-12-29 23:22:06 +0530 |
commit | 438ad16d03f38e0e444f6ad575078ee949679a86 (patch) | |
tree | 0688901d51b9b1736679ac92af5a4636d2553bed /util.c |
repo: init
Diffstat (limited to 'util.c')
-rw-r--r-- | util.c | 33 |
1 files changed, 33 insertions, 0 deletions
@@ -0,0 +1,33 @@ +#include <sys/stat.h> +#include <string.h> +#include <errno.h> +#include <linux/limits.h> + +#include "util.h" + +int r_mkdir(const char *path, mode_t mode) +{ + int r; + size_t len; + char *p; + char tmp[NAME_MAX + 1]; + + strncpy(tmp, path, sizeof(tmp) - 1); + len = strlen(tmp); + if(tmp[len - 1] == '/') + tmp[len - 1] = '\0'; + + for (p = tmp + 1; *p; ++p) { + if (*p == '/') { + *p = '\0'; + + r = mkdir(tmp, mode); + if (r && !(errno & EEXIST)) + return r; + + *p = '/'; + } + } + + return mkdir(path, mode); +} |