diff options
Diffstat (limited to 'misc.c')
-rw-r--r-- | misc.c | 36 |
1 files changed, 36 insertions, 0 deletions
@@ -1,8 +1,11 @@ #include <ctype.h> +#include <dirent.h> #include <string.h> #include <stdarg.h> #include <stdio.h> #include <stdlib.h> +#include <sys/stat.h> +#include <unistd.h> #include "misc.h" char *read_file(const char *filename, long *size) { @@ -206,3 +209,36 @@ int format_len(const char *fmt, ...) { va_end(args); return len; } + +int is_dir(const char *path) { + DIR *dir; + + if (!access(path, F_OK)) { + if ((dir = opendir(path)) != NULL) { + closedir(dir); + return 1; + } else { + return 0; + } + } else { + return -1; + } +} + +void mkdirp(const char *path, int mode) { + char *str = make_str(path); + + for (char *p = find_delm(str, "/", *str == '/'); !is_empty(p); p = find_delm(p, "/", 1)) { + if (*p == '/') { + *p = '\0'; + } + if (is_dir(str) < 0 && mkdir(str, mode) < 0) { + break; + } + if (*p == '\0') { + *p = '/'; + } + } + + free(str); +} |