Commit ea8436a6b6ced1d5c0aec8493f6c5da055130e01

Thomas de Grivel 2023-12-01T13:23:44

wip file_dirname

diff --git a/libc3/env.c b/libc3/env.c
index 36f0827..d5ce72d 100644
--- a/libc3/env.c
+++ b/libc3/env.c
@@ -778,16 +778,18 @@ s_env * env_init (s_env *env, int argc, s8 **argv)
 s_env * env_init_args (s_env *env, int argc, s8 **argv)
 {
   s8 a[PATH_MAX];
+  s_str argv0;
   s_buf buf;
-  s8 *dir;
+  s_str dir;
   sw r;
   assert(env);
   if (argc && argv) {
     env->argc = argc;
     env->argv = argv;
+    str_init_1(&argv0, NULL, argv[0]);
+    file_dirname(&dir, &argv0);
     buf_init(&buf, false, sizeof(a), a);
-    dir = dirname(argv[0]);
-    if ((r = buf_write_1(&buf, dir)) < 0 ||
+    if ((r = buf_write_str(&buf, &dir)) < 0 ||
         (r = buf_write_u8(&buf, '/') < 0))
       goto ko;
     buf_read_to_str(&buf, &env->argv0_dir);
diff --git a/libc3/file.c b/libc3/file.c
index 6de4296..dd401f6 100644
--- a/libc3/file.c
+++ b/libc3/file.c
@@ -101,10 +101,10 @@ s_str * file_dirname (const s_str *path, s_str *dest)
   assert(path);
   assert(dest);
   if (! str_rindex_character(path, '/', &dirsep_pos))
-    return str_init(dest, NULL, 1, ".");
+    return str_init(dest, NULL, 2, "./");
   if (! dirsep_pos)
     return str_init(dest, NULL, 1, "/");
-  return str_init_slice(dest, path, 0, dirsep_pos);
+  return str_init_slice(dest, path, 0, dirsep_pos + 1);
 }
 
 s_tag * file_mtime (const s_str *path, s_tag *dest)
diff --git a/libc3/file.h b/libc3/file.h
index adffc3b..9813e1e 100644
--- a/libc3/file.h
+++ b/libc3/file.h
@@ -28,6 +28,7 @@
 bool *  file_access (const s_str *path, const s_sym *mode,
                      bool *dest);
 sw      file_copy_1 (const char *from, const char *to);
+s_str * file_dirname (const s_str *path, s_str *dest);
 s_tag * file_mtime (const s_str *path, s_tag *dest);
 s_str * file_search (const s_str *suffix, const s_sym *mode,
                      s_str *dest);