diff --git a/sortix/include/sortix/fcntl.h b/sortix/include/sortix/fcntl.h index c787882e..91251250 100644 --- a/sortix/include/sortix/fcntl.h +++ b/sortix/include/sortix/fcntl.h @@ -88,6 +88,12 @@ __BEGIN_DECLS #define F_GETFL_NUM 3 #define F_GETFL F_ENCODE_CMD(F_GETFL_NUM, F_TYPE_VOID) +/* Duplicates the file descriptor with at least the given index. */ +#define F_DUPFD_NUM 4 +#define F_DUPFD F_ENCODE_CMD(F_ENCODE(F_DUPFD_NUM, 0), F_TYPE_INT) +#define F_DUPFD_CLOEXEC F_ENCODE_CMD(F_ENCODE(F_DUPFD_NUM, FD_CLOEXEC), F_TYPE_INT) +#define F_DUPFD_CLOFORK F_ENCODE_CMD(F_ENCODE(F_DUPFD_NUM, FD_CLOFORK), F_TYPE_INT) + #define AT_FDCWD (-100) #define AT_REMOVEDIR (1<<0) #define AT_EACCESS (1<<1) diff --git a/sortix/io.cpp b/sortix/io.cpp index 51deec31..d9a72bdf 100644 --- a/sortix/io.cpp +++ b/sortix/io.cpp @@ -328,6 +328,11 @@ static int sys_fcntl(int fd, int cmd, uintptr_t arg) return dtable->GetFlags(fd); // Operations on the file description. + if ( F_DECODE_CMD(F_DECODE_CMD_RAW(cmd)) == F_DUPFD_NUM ) + { + int fd_flags = F_DECODE_FLAGS(F_DECODE_CMD_RAW(cmd)); + return dtable->Allocate(fd, fd_flags, (int) arg); + } Ref desc = dtable->Get(fd); if ( !desc ) return -1;