summaryrefslogtreecommitdiffhomepage
path: root/lib/fs.c
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2022-11-29 15:24:02 +0100
committerGitHub <noreply@github.com>2022-11-29 15:24:02 +0100
commit3497da1448f9b1d602e23c10977db0ce0188c959 (patch)
tree91e67a1a7078e699881f15641a4dc2b6bee94686 /lib/fs.c
parent5cabda3496101c64c35cd7f19502f5bf77f1571e (diff)
parent3903b184fdb081202ac3d8f60f1aea1af7114c13 (diff)
Merge pull request #130 from jow-/fs-add-realpath
fs: add `realpath()` function
Diffstat (limited to 'lib/fs.c')
-rw-r--r--lib/fs.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/lib/fs.c b/lib/fs.c
index b0b085f..af8b8e9 100644
--- a/lib/fs.c
+++ b/lib/fs.c
@@ -1316,6 +1316,27 @@ uc_fs_writefile(uc_vm_t *vm, size_t nargs)
return ucv_uint64_new(wlen);
}
+static uc_value_t *
+uc_fs_realpath(uc_vm_t *vm, size_t nargs)
+{
+ uc_value_t *path = uc_fn_arg(0), *rv;
+ char *resolved;
+
+ if (ucv_type(path) != UC_STRING)
+ err_return(EINVAL);
+
+ resolved = realpath(ucv_string_get(path), NULL);
+
+ if (!resolved)
+ err_return(errno);
+
+ rv = ucv_string_new(resolved);
+
+ free(resolved);
+
+ return rv;
+}
+
static const uc_function_list_t proc_fns[] = {
{ "read", uc_fs_pread },
@@ -1371,6 +1392,7 @@ static const uc_function_list_t global_fns[] = {
{ "access", uc_fs_access },
{ "readfile", uc_fs_readfile },
{ "writefile", uc_fs_writefile },
+ { "realpath", uc_fs_realpath },
};