summaryrefslogtreecommitdiffhomepage
path: root/lib/fs.c
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2023-01-20 11:52:57 +0100
committerJo-Philipp Wich <jo@mein.io>2023-01-20 11:52:57 +0100
commit48a6eac1da1584ea784fd7d53f761baa87ae84d2 (patch)
tree21088c8e9b3a79582fc96031e13398b55d402204 /lib/fs.c
parent1c8df08824ef80eef4c2a4681ac2d2a1d43e7336 (diff)
fs: implement `fs.pipe()`
The `pipe()` function takes no arguments and will return a two element array containing open read- and write file descriptors for a newly created pipe. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'lib/fs.c')
-rw-r--r--lib/fs.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/lib/fs.c b/lib/fs.c
index 0f24714..831137d 100644
--- a/lib/fs.c
+++ b/lib/fs.c
@@ -1354,6 +1354,42 @@ uc_fs_realpath(uc_vm_t *vm, size_t nargs)
return rv;
}
+static uc_value_t *
+uc_fs_pipe(uc_vm_t *vm, size_t nargs)
+{
+ int pfds[2], err;
+ FILE *rfp, *wfp;
+ uc_value_t *rv;
+
+ if (pipe(pfds) == -1)
+ err_return(errno);
+
+ rfp = fdopen(pfds[0], "r");
+
+ if (!rfp) {
+ err = errno;
+ close(pfds[0]);
+ close(pfds[1]);
+ err_return(err);
+ }
+
+ wfp = fdopen(pfds[1], "w");
+
+ if (!wfp) {
+ err = errno;
+ fclose(rfp);
+ close(pfds[1]);
+ err_return(err);
+ }
+
+ rv = ucv_array_new_length(vm, 2);
+
+ ucv_array_push(rv, uc_resource_new(file_type, rfp));
+ ucv_array_push(rv, uc_resource_new(file_type, wfp));
+
+ return rv;
+}
+
static const uc_function_list_t proc_fns[] = {
{ "read", uc_fs_pread },
@@ -1411,6 +1447,7 @@ static const uc_function_list_t global_fns[] = {
{ "readfile", uc_fs_readfile },
{ "writefile", uc_fs_writefile },
{ "realpath", uc_fs_realpath },
+ { "pipe", uc_fs_pipe },
};