summaryrefslogtreecommitdiff
path: root/misc.c
diff options
context:
space:
mode:
Diffstat (limited to 'misc.c')
-rw-r--r--misc.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/misc.c b/misc.c
index 97333eb..4c0cab0 100644
--- a/misc.c
+++ b/misc.c
@@ -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);
+}