From bef91584508f9e381493ce38a21ec617d526f3e2 Mon Sep 17 00:00:00 2001 From: apio Date: Fri, 21 Oct 2022 18:34:31 +0200 Subject: [PATCH] Kernel, libc: Add isatty() and F_ISTTY to fcntl() --- kernel/include/fs/FileDescriptor.h | 5 +++++ kernel/include/fs/VFS.h | 1 + kernel/src/fs/devices/Console.cpp | 1 + kernel/src/fs/devices/Keyboard.cpp | 1 + kernel/src/sys/stdio.cpp | 13 +++++++++++-- libs/libc/include/fcntl.h | 2 ++ libs/libc/include/unistd.h | 3 +++ libs/libc/src/unistd.cpp | 7 +++++++ 8 files changed, 31 insertions(+), 2 deletions(-) diff --git a/kernel/include/fs/FileDescriptor.h b/kernel/include/fs/FileDescriptor.h index b6ee909b..c7f0b62c 100644 --- a/kernel/include/fs/FileDescriptor.h +++ b/kernel/include/fs/FileDescriptor.h @@ -24,6 +24,11 @@ struct Descriptor m_is_open = false; } + VFS::Node* node() + { + return m_node; + } + ssize_t read(size_t size, char* buffer); ssize_t write(size_t size, const char* buffer); diff --git a/kernel/include/fs/VFS.h b/kernel/include/fs/VFS.h index 6519ad59..c159fb11 100644 --- a/kernel/include/fs/VFS.h +++ b/kernel/include/fs/VFS.h @@ -30,6 +30,7 @@ namespace VFS node_finddir find_func; node_mkdir mkdir_func; node_write write_func; + int tty = 0; Node* link; }; diff --git a/kernel/src/fs/devices/Console.cpp b/kernel/src/fs/devices/Console.cpp index a4c73ca0..d0157542 100644 --- a/kernel/src/fs/devices/Console.cpp +++ b/kernel/src/fs/devices/Console.cpp @@ -13,6 +13,7 @@ VFS::Node* ConsoleDevice::create_new(const char* devname) dev->length = 0; dev->type = VFS_DEVICE; dev->flags = 0; + dev->tty = 1; strncpy(dev->name, devname, sizeof(dev->name)); return dev; } diff --git a/kernel/src/fs/devices/Keyboard.cpp b/kernel/src/fs/devices/Keyboard.cpp index 1a4af295..6907042c 100644 --- a/kernel/src/fs/devices/Keyboard.cpp +++ b/kernel/src/fs/devices/Keyboard.cpp @@ -19,6 +19,7 @@ VFS::Node* KeyboardDevice::create_new(const char* devname) dev->length = 0; dev->type = VFS_DEVICE; dev->flags = 0; + dev->tty = 1; strncpy(dev->name, devname, sizeof(dev->name)); return dev; } diff --git a/kernel/src/sys/stdio.cpp b/kernel/src/sys/stdio.cpp index 9f041a66..a83c18d5 100644 --- a/kernel/src/sys/stdio.cpp +++ b/kernel/src/sys/stdio.cpp @@ -19,7 +19,8 @@ #define SEEK_CUR 1 #define SEEK_END 2 -#define FNCTL_DUPFD 0 +#define FCNTL_DUPFD 0 +#define FCNTL_ISTTY 1 void sys_fcntl(Context* context, int fd, int command, uintptr_t arg) { @@ -35,7 +36,7 @@ void sys_fcntl(Context* context, int fd, int command, uintptr_t arg) return; } Descriptor& file = current_task->files[fd]; - if (command == FNCTL_DUPFD) + if (command == FCNTL_DUPFD) { if ((int)arg < 0 || (int)arg >= TASK_MAX_FDS) { @@ -53,6 +54,14 @@ void sys_fcntl(Context* context, int fd, int command, uintptr_t arg) kdbgln("fcntl(F_DUPFD): duplicated fd %d, result is %d", fd, dupfd); return; } + else if (command == FCNTL_ISTTY) + { + VFS::Node* node = file.node(); + if (node->tty) { context->rax = 1; } + else + context->rax = -ENOTTY; + return; + } else { context->rax = -EINVAL; diff --git a/libs/libc/include/fcntl.h b/libs/libc/include/fcntl.h index 1f4b2715..fb358446 100644 --- a/libs/libc/include/fcntl.h +++ b/libs/libc/include/fcntl.h @@ -10,6 +10,8 @@ /* Duplicate a file descriptor. */ #define F_DUPFD 0 +/* Is a file descriptor a TTY? */ +#define F_ISTTY 1 #ifdef __cplusplus extern "C" diff --git a/libs/libc/include/unistd.h b/libs/libc/include/unistd.h index 54358f21..942302ca 100644 --- a/libs/libc/include/unistd.h +++ b/libs/libc/include/unistd.h @@ -66,6 +66,9 @@ extern "C" /* Checks if the current program can access the file or directory at path. */ int access(const char* path, int amode); + /* Checks if the file descriptor fd refers to a terminal. */ + int isatty(int fd); + #ifdef __cplusplus } #endif diff --git a/libs/libc/src/unistd.cpp b/libs/libc/src/unistd.cpp index 43d9b5b3..2f64ef22 100644 --- a/libs/libc/src/unistd.cpp +++ b/libs/libc/src/unistd.cpp @@ -74,4 +74,11 @@ extern "C" { return (int)syscall(SYS_access, path, amode); } + + int isatty(int fd) + { + int result = fcntl(fd, F_ISTTY); + if (result < 0) return 0; + return 1; + } } \ No newline at end of file