From 35633fc65f40759f2282209fcff48f04e07f685e Mon Sep 17 00:00:00 2001 From: apio Date: Sat, 16 Sep 2023 11:47:57 +0200 Subject: [PATCH] libc: Add pseudoterminal-related functions --- libc/include/stdlib.h | 12 ++++++++++++ libc/src/stdlib.cpp | 30 ++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/libc/include/stdlib.h b/libc/include/stdlib.h index b06e1861..35357d21 100644 --- a/libc/include/stdlib.h +++ b/libc/include/stdlib.h @@ -148,6 +148,18 @@ extern "C" /* Create a unique file from a template string whose last 6 bytes must be XXXXXX. */ int mkstemp(char* _template); + /* Create a new pseudoterminal pair. */ + int posix_openpt(int flags); + + /* Set the credentials of a pseudoterminal master. */ + int grantpt(int fd); + + /* Unlock a pseudoterminal master. */ + int unlockpt(int fd); + + /* Return the name of the slave associated with a pseudoterminal master. */ + char* ptsname(int fd); + #ifdef __cplusplus } #endif diff --git a/libc/src/stdlib.cpp b/libc/src/stdlib.cpp index b19b2a11..acbd7d9b 100644 --- a/libc/src/stdlib.cpp +++ b/libc/src/stdlib.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -7,8 +8,10 @@ #include #include #include +#include #include #include +#include #include #include #include @@ -300,4 +303,31 @@ extern "C" { return strtod(str, nullptr); } + + int posix_openpt(int flags) + { + return open("/dev/ptmx", flags); + } + + int grantpt(int) + { + return 0; + } + + int unlockpt(int) + { + return 0; + } + + char* ptsname(int fd) + { + static char buffer[4096]; + + int index; + if (ioctl(fd, TIOCGPTN, &index) < 0) return nullptr; + + snprintf(buffer, sizeof(buffer), "/dev/pts/%d", index); + + return buffer; + } }