From ce10fb574366d420faf4d581cbd2d08b06200d1c Mon Sep 17 00:00:00 2001 From: apio Date: Mon, 17 Oct 2022 17:01:22 +0200 Subject: [PATCH] fcntl(F_DUPFD): Allocate any file descriptor greater than or equal to arg --- kernel/src/sys/stdio.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/kernel/src/sys/stdio.cpp b/kernel/src/sys/stdio.cpp index a32e8d55..9ad01b8f 100644 --- a/kernel/src/sys/stdio.cpp +++ b/kernel/src/sys/stdio.cpp @@ -20,7 +20,7 @@ #define FNCTL_DUPFD 0 -void sys_fcntl(Context* context, int fd, int command, [[maybe_unused]] uintptr_t arg) +void sys_fcntl(Context* context, int fd, int command, uintptr_t arg) { if (fd >= TASK_MAX_FDS || fd < 0) { @@ -34,10 +34,14 @@ void sys_fcntl(Context* context, int fd, int command, [[maybe_unused]] uintptr_t return; } Descriptor& file = current_task->files[fd]; - if (command == FNCTL_DUPFD) // FIXME: If arg is greater than 0, return the lowest numbered available file descriptor - // greater than or equal to arg. + if (command == FNCTL_DUPFD) { - int dupfd = current_task->alloc_fd(); + if ((int)arg < 0 || (int)arg >= TASK_MAX_FDS) + { + context->rax = -EINVAL; + return; + } + int dupfd = current_task->alloc_fd_greater_than_or_equal((int)arg); if (dupfd < 0) { context->rax = -EMFILE;