Commit 7ad7d40744b1e90d41e4b51b13c08279c99b913c

Baptiste 2024-08-06T17:28:47

file exist in kc3 and c

diff --git a/lib/kc3/0.1/file.kc3 b/lib/kc3/0.1/file.kc3
new file mode 100644
index 0000000..2496ef9
--- /dev/null
+++ b/lib/kc3/0.1/file.kc3
@@ -0,0 +1,5 @@
+defmodule File do
+
+  def exists? = cfn Bool "file_exists" (Str, Result)
+
+end
diff --git a/libkc3/file.c b/libkc3/file.c
index 7129e9d..05589a2 100644
--- a/libkc3/file.c
+++ b/libkc3/file.c
@@ -14,6 +14,7 @@
 #include <fcntl.h>
 #include <errno.h>
 #include <string.h>
+#include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
 #include "buf.h"
@@ -121,6 +122,15 @@ s_str * file_dirname (const s_str *path, s_str *dest)
   return str_init_slice(dest, path, 0, dirsep_pos + 1);
 }
 
+bool * file_exists (const s_str *path, bool *dest)
+{
+  struct stat sb;
+  assert(path);
+  assert(dest);
+  *dest = ! stat(path->ptr.pchar, &sb);
+  return dest;
+}
+
 s_tag * file_mtime (const s_str *path, s_tag *dest)
 {
   s32 e;