Commit e15955c2c70a41fac809f1df5a43899ffcc5b7df

Thomas de Grivel 2024-11-18T21:14:18

fix paths with double slash (//)

diff --git a/libkc3/env.c b/libkc3/env.c
index d810c67..191f0a8 100644
--- a/libkc3/env.c
+++ b/libkc3/env.c
@@ -2705,8 +2705,7 @@ s_env * env_init_args (s_env *env, int *argc, char ***argv)
     str_init_1(&argv0, NULL, env->argv[0]);
     file_dirname(&argv0, &dir);
     buf_init(&buf, false, sizeof(a), a);
-    if ((r = buf_write_str(&buf, &dir)) < 0 ||
-        (r = buf_write_u8(&buf, '/') < 0)) {
+    if ((r = buf_write_str(&buf, &dir)) < 0) {
       str_clean(&dir);
       goto ko;
     }
@@ -2868,6 +2867,11 @@ bool env_load (s_env *env, const s_str *path)
   s_tag tmp = {0};
   assert(env);
   assert(path);
+  if (true) {
+    err_write_1("env_load: ");
+    err_inspect_str(path);
+    err_write_1("\n");
+  }
   if (! buf_init_alloc(&buf, BUF_SIZE))
     return false;
   if (! buf_getc_open_r(&buf, path->ptr.pchar)) {
diff --git a/libkc3/file.c b/libkc3/file.c
index 7562719..d91fc8b 100644
--- a/libkc3/file.c
+++ b/libkc3/file.c
@@ -432,7 +432,7 @@ s_str * file_search (const s_str *suffix, const s_sym *mode,
   s_buf_save save;
   const s_str *str;
   s_str tmp = {0};
-  buf_init(&buf, false, PATH_MAX, buf_s);
+  buf_init(&buf, false, sizeof(buf_s), buf_s);
   if ((r = buf_write_str(&buf, &g_kc3_env.argv0_dir)) < 0)
     return NULL;
   buf_save_init(&buf, &save);
@@ -442,10 +442,15 @@ s_str * file_search (const s_str *suffix, const s_sym *mode,
       buf_save_restore_rpos(&buf, &save);
       buf_save_restore_wpos(&buf, &save);
       str = &path->tag.data.str;
-      if ((r = buf_write_str(&buf, str)) < 0 ||
-          (str->ptr.pchar[str->size - 1] != '/' &&
-           (r = buf_write_1(&buf, "/")) < 0) ||
-          (r = buf_write_str(&buf, suffix)) < 0)
+      if ((r = buf_write_str(&buf, str)) < 0)
+        return NULL;
+      if (str->size > 0 &&
+          str->ptr.pchar[str->size - 1] != '/' &&
+          suffix->size > 0 &&
+          suffix->ptr.pchar[0] != '/' &&
+          (r = buf_write_1(&buf, "/")) < 0)
+        return NULL;
+      if ((r = buf_write_str(&buf, suffix)) < 0)
         return NULL;
       buf_read_to_str(&buf, &tmp);
       //io_inspect_str(&tmp);
diff --git a/libkc3/kc3.c b/libkc3/kc3.c
index f0bd8c9..f1cf323 100644
--- a/libkc3/kc3.c
+++ b/libkc3/kc3.c
@@ -430,11 +430,6 @@ void kc3_license (void)
 
 bool kc3_load (const s_str *path)
 {
-  if (true) {
-    err_write_1("kc3_load: ");
-    err_inspect_str(path);
-    err_write_1("\n");
-  }
   return env_load(&g_kc3_env, path);
 }
 
diff --git a/libkc3/module.c b/libkc3/module.c
index e23b124..6e48f88 100644
--- a/libkc3/module.c
+++ b/libkc3/module.c
@@ -65,18 +65,17 @@ bool module_maybe_reload (const s_sym *module)
 s_str * module_path (const s_sym *module, const s_str *prefix,
                      const char *ext, s_str *dest)
 {
+  char a[BUF_SIZE];
   character b = 0;
   character c;
   s_buf in;
   s_buf out;
-  sw out_size;
   sw r;
   s_str *result;
   assert(dest);
   assert(module);
   buf_init_str(&in, false, (s_str *) &module->str);
-  out_size = module_path_size(module, prefix, ext);
-  buf_init_alloc(&out, out_size);
+  buf_init(&out, false, sizeof(a), a);
   if ((r = buf_write_str(&out, prefix)) < 0)
     goto error;
   while ((r = buf_read_character_utf8(&in, &c)) > 0) {
@@ -99,7 +98,9 @@ s_str * module_path (const s_sym *module, const s_str *prefix,
   }
   if ((r = buf_write_1(&out, ext)) < 0)
     goto error;
-  result = buf_to_str(&out, dest);
+  if (! (result = buf_read_to_str(&out, dest)))
+    goto error;
+  buf_clean(&out);
   return result;
  error:
   buf_clean(&out);
@@ -107,31 +108,3 @@ s_str * module_path (const s_sym *module, const s_str *prefix,
   assert(! "module_path: error");
   return NULL;
 }
-
-sw module_path_size (const s_sym *module, const s_str *prefix, const char *ext)
-{
-  character b = 0;
-  character c;
-  s_buf in;
-  sw r;
-  sw result;
-  assert(prefix);
-  assert(module);
-  buf_init_str(&in, false, (s_str *) &module->str);
-  result = prefix->size;
-  while ((r = buf_read_character_utf8(&in, &c)) > 0) {
-    if (c == '.')
-      c = '/';
-    else if (character_is_uppercase(c)) {
-      if (character_is_lowercase(b))
-        result += strlen("_");
-      b = c;
-      c = character_to_lower(c);
-    }
-    else
-      b = c;
-    result += character_utf8_size(c);
-  }
-  result += strlen(ext);
-  return result;
-}