Commit 4ddf919ab260a0b76fe2738c48d7a442ee591913

Thomas de Grivel 2023-12-12T03:46:19

wip win32

diff --git a/libc3/f64.c b/libc3/f64.c
index 4135def..6482c7f 100644
--- a/libc3/f64.c
+++ b/libc3/f64.c
@@ -93,6 +93,6 @@ f64 * f64_init_copy (f64 *x, const f64 *src)
 
 f64 * f64_random (f64 *dest)
 {
-  *dest = (f64) random() / (((u32) 1 << 31) - 1);
+  *dest = (f64) arc4random() / U32_MAX;
   return dest;
 }
diff --git a/libc3/file.c b/libc3/file.c
index dd401f6..3e29edb 100644
--- a/libc3/file.c
+++ b/libc3/file.c
@@ -25,6 +25,7 @@
 #include "str.h"
 #include "sym.h"
 #include "time.h"
+#include "config.h"
 
 bool * file_access (const s_str *path, const s_sym *mode,
                     bool *dest)
@@ -116,7 +117,14 @@ s_tag * file_mtime (const s_str *path, s_tag *dest)
     warn("file_mtime: %s", path->ptr.ps8);
     return NULL;
   }
+#if HAVE_STAT_MTIM
   return time_to_tag(&sb.st_mtim, dest);
+#else
+  s_time tmp;
+  tmp.tv_sec = sb.st_mtime;
+  tmp.tv_nsec = 0;
+  return time_to_tag(&tmp, dest);
+#endif
 }
 
 s_str * file_search (const s_str *suffix, const s_sym *mode,
diff --git a/libc3/fn.c b/libc3/fn.c
index 81bcfba..d345f6f 100644
--- a/libc3/fn.c
+++ b/libc3/fn.c
@@ -45,11 +45,16 @@ s_fn * fn_init (s_fn *fn)
 s_fn * fn_init_1 (s_fn *fn, s8 *p)
 {
   s_buf buf;
+  uw len;
   sw r;
   assert(fn);
-  buf_init_1(&buf, false, (s8 *) p);
-  if ((r = buf_parse_fn(&buf, fn)) != (sw) strlen(p))
-    errx(1, "fn_init_1: buf_parse_fn(%s) %ld != %lu", p, r, strlen(p));
+  assert(p);
+  len = strlen(p);
+  buf_init(&buf, false, len, (s8 *) p);
+  buf.wpos = len;
+  r = buf_parse_fn(&buf, fn);
+  if (r < 0 || (uw) r != len)
+    errx(1, "fn_init_1: buf_parse_fn(%s): %ld != %lu", p, r, len);
   return fn;
 }
 
diff --git a/libc3/skiplist.c.in b/libc3/skiplist.c.in
index dd0ee0a..8ac66cf 100644
--- a/libc3/skiplist.c.in
+++ b/libc3/skiplist.c.in
@@ -183,7 +183,7 @@ skiplist_random_height___NAME$ (s_skiplist___NAME$ *skiplist)
   assert(skiplist);
   height_table = SKIPLIST_HEIGHT_TABLE___NAME$(skiplist);
   max = height_table[skiplist->max_height - 1];
-  k = random() % max;
+  k = arc4random_uniform(max);
   for (i = 0; i < skiplist->max_height && k > height_table[i]; i++)
     ;
   height = skiplist->max_height - i;
diff --git a/libc3/skiplist__fact.c b/libc3/skiplist__fact.c
index a39890f..b0de02d 100644
--- a/libc3/skiplist__fact.c
+++ b/libc3/skiplist__fact.c
@@ -183,7 +183,7 @@ skiplist_random_height__fact (s_skiplist__fact *skiplist)
   assert(skiplist);
   height_table = SKIPLIST_HEIGHT_TABLE__fact(skiplist);
   max = height_table[skiplist->max_height - 1];
-  k = random() % max;
+  k = arc4random_uniform(max);
   for (i = 0; i < skiplist->max_height && k > height_table[i]; i++)
     ;
   height = skiplist->max_height - i;
diff --git a/libc3/time.c b/libc3/time.c
index 5b4ae14..2449d2a 100644
--- a/libc3/time.c
+++ b/libc3/time.c
@@ -36,7 +36,8 @@ s_tag * time_to_tag (const s_time *time, s_tag *dest)
 {
   s_tag tmp;
   assert(time);
-  tag_init_tuple(&tmp, 2);
+  if (! tag_init_tuple(&tmp, 2))
+    return NULL;
   tag_init_s64(&tmp.data.tuple.tag[0], time->tv_sec);
   tag_init_s64(&tmp.data.tuple.tag[1], time->tv_nsec);
   *dest = tmp;