summaryrefslogtreecommitdiffhomepage
path: root/lib
diff options
context:
space:
mode:
authorPetr Štetiar <ynezz@true.cz>2023-01-09 14:53:16 +0100
committerPetr Štetiar <ynezz@true.cz>2023-01-09 15:13:32 +0100
commitbe30472bfdbbb410e8934b48a56d26c5c630d0f1 (patch)
tree0f4a75a4f1e9587f4d55640b2b58d2f0ca15c6e1 /lib
parent1e4d20932646f90523d21ea358c72901e3ee689e (diff)
fs: add `isatty()` function
Expose the `isatty(3)` libc function in the fs module to allow checking whether a file descriptor refers to a terminal. Signed-off-by: Petr Štetiar <ynezz@true.cz>
Diffstat (limited to 'lib')
-rw-r--r--lib/fs.c18
1 files changed, 18 insertions, 0 deletions
diff --git a/lib/fs.c b/lib/fs.c
index af8b8e9..0f24714 100644
--- a/lib/fs.c
+++ b/lib/fs.c
@@ -353,6 +353,23 @@ uc_fs_tell(uc_vm_t *vm, size_t nargs)
}
static uc_value_t *
+uc_fs_isatty(uc_vm_t *vm, size_t nargs)
+{
+ FILE **fp = uc_fn_this("fs.file");
+ int fd;
+
+ if (!fp || !*fp)
+ err_return(EBADF);
+
+ fd = fileno(*fp);
+
+ if (fd == -1)
+ err_return(errno);
+
+ return ucv_boolean_new(isatty(fd));
+}
+
+static uc_value_t *
uc_fs_flush(uc_vm_t *vm, size_t nargs)
{
return uc_fs_flush_common(vm, nargs, "fs.file");
@@ -1356,6 +1373,7 @@ static const uc_function_list_t file_fns[] = {
{ "flush", uc_fs_flush },
{ "fileno", uc_fs_fileno },
{ "error", uc_fs_error },
+ { "isatty", uc_fs_isatty },
};
static const uc_function_list_t dir_fns[] = {