fix compilation
[libjh.git] / fs.c
diff --git a/fs.c b/fs.c
index 1e8a31f..ebe8730 100644 (file)
--- a/fs.c
+++ b/fs.c
@@ -1,9 +1,9 @@
 // Copyright (2013) Jann Horn <jann@thejh.net>
-// This code is licensed under the AGPLv3.
 
 HEADER #include <stdio.h>
 HEADER #include <sys/types.h>
 HEADER #include <dirent.h>
+#include <sys/stat.h>
 
 #include <errno.h>
 
@@ -30,3 +30,24 @@ PUBLIC_FN int dir_foreach(char *path, dir_iterator *iter, void *data) {
   errno = errno_;
   return feach_res;
 }
+
+PUBLIC_FN int mkdir_maybe(const char *path, mode_t mode) {
+  int r = mkdir(path, mode);
+  if (r && errno != EEXIST) {
+    return r;
+  }
+  return 0;
+}
+
+HEADER // requires stdio.h
+HEADER #define FDIR_FOREACH(dir, element_name, code) {                                                     \
+HEADER   struct dirent element_name;                                                                       \
+HEADER   struct dirent *__jh_fdir_foreach_res;                                                             \
+HEADER   while (1) {                                                                                       \
+HEADER     int __jh_fdir_foreach_rd_res = readdir_r(dir, &element_name, &__jh_fdir_foreach_res);           \
+HEADER     if(__jh_fdir_foreach_rd_res) {*(char*)0 = 0; /*crash, boom – EBADF happened (probably)*/}       \
+HEADER     if (__jh_fdir_foreach_res == NULL) goto end;                                                    \
+HEADER     {code}                                                                                          \
+HEADER   }                                                                                                 \
+HEADER   end:;                                                                                             \
+HEADER }