Browse Source

Introduce IS_DIR(path)

rexim 4 years ago
parent
commit
eff25df9fd
4 changed files with 33 additions and 1 deletions
  1. 1 0
      ChangeLog.md
  2. 2 1
      examples/.gitignore
  3. 12 0
      examples/file.c
  4. 18 0
      nobuild.h

+ 1 - 0
ChangeLog.md

@@ -5,6 +5,7 @@
 *Not Released Yet*
 
 - Introduced `ENDS_WITH(str, postfix)` function that checks if `str` ends with `postfix`.
+- Introduced `IS_DIR(path)` function that checks if `path`is a directory.
 
 # 0.1.0 — Cleaning Things Up. Figuring Out Conventions.
 

+ 2 - 1
examples/.gitignore

@@ -1,3 +1,4 @@
 string
 foreach
-logging
+logging
+file

+ 12 - 0
examples/file.c

@@ -0,0 +1,12 @@
+#define NOBUILD_IMPLEMENTATION
+#include "../nobuild.h"
+
+#define DEMO(expr)                              \
+    INFO(#expr" == %d", expr)
+
+int main(int argc, char *argv[])
+{
+    DEMO(IS_DIR("./nobuild.c"));
+    DEMO(IS_DIR("./examples"));
+    return 0;
+}

+ 18 - 0
nobuild.h

@@ -158,6 +158,7 @@ const char *concat_impl(int ignore, ...);
 const char *concat_sep_impl(const char *sep, ...);
 const char *build__join(const char *sep, ...);
 int nobuild__ends_with(const char *str, const char *postfix);
+int nobuild__is_dir(const char *path);
 void mkdirs_impl(int ignore, ...);
 void cmd_impl(int ignore, ...);
 void nobuild_exec(const char **argv);
@@ -171,6 +172,7 @@ char *shift(int *argc, char ***argv);
 #define MKDIRS(...) mkdirs_impl(69, __VA_ARGS__, NULL)
 #define NOEXT(path) nobuild__remove_ext(path)
 #define ENDS_WITH(str, postfix) nobuild__ends_with(str, postfix)
+#define IS_DIR(path) nobuild__is_dir(path)
 
 void nobuild_log(FILE *stream, const char *tag, const char *fmt, ...);
 void nobuild_vlog(FILE *stream, const char *tag, const char *fmt, va_list args);
@@ -590,4 +592,20 @@ int nobuild__ends_with(const char *str, const char *postfix)
     return postfix_n <= str_n && strcmp(str + str_n - postfix_n, postfix) == 0;
 }
 
+int nobuild__is_dir(const char *path)
+{
+#ifdef _WIN32
+#error "IS_DIR is not implemented for this platform"
+#else
+    struct stat statbuf = {0};
+    if (stat(path, &statbuf) < 0) {
+        ERRO("could not retrieve information about file %s: ",
+             path, strerror(errno));
+        exit(1);
+    }
+
+    return (statbuf.st_mode & S_IFMT) == S_IFDIR;
+#endif // _WIN32
+}
+
 #endif // NOBUILD_IMPLEMENTATION