From 15d5f00cd38f26cd88790167f631ea7c755d755b Mon Sep 17 00:00:00 2001 From: apio Date: Sun, 15 Oct 2023 11:09:08 +0200 Subject: [PATCH] libc: Add ctermid --- apps/su.cpp | 7 +++++-- libc/include/stdio.h | 5 +++++ libc/src/stdio.cpp | 11 +++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/apps/su.cpp b/apps/su.cpp index 64353719..33786533 100644 --- a/apps/su.cpp +++ b/apps/su.cpp @@ -23,10 +23,13 @@ void signal_handler(int signo) char* getpass() { - FILE* f = fopen("/dev/tty", "r"); + char ctty[L_ctermid]; + ctermid(ctty); + + FILE* f = fopen(ctty, "r"); if (!f) { - perror("Failed to open /dev/tty"); + perror("Failed to open controlling terminal"); return nullptr; } diff --git a/libc/include/stdio.h b/libc/include/stdio.h index a89ec591..d8b00e3a 100644 --- a/libc/include/stdio.h +++ b/libc/include/stdio.h @@ -12,6 +12,8 @@ #define FOPEN_MAX 64 // Make sure this value matches FD_MAX in the kernel source. +#define L_ctermid 9 // /dev/tty + a NULL byte + typedef struct { int _fd; // The underlying file descriptor. @@ -206,6 +208,9 @@ extern "C" /* Pipe a stream to or from a process. */ FILE* popen(const char* command, const char* type); + /* Return the path of the process's controlling terminal (always /dev/tty on Luna). */ + char* ctermid(char* s); + #ifdef __cplusplus } #endif diff --git a/libc/src/stdio.cpp b/libc/src/stdio.cpp index c6b15c3d..31d414ac 100644 --- a/libc/src/stdio.cpp +++ b/libc/src/stdio.cpp @@ -851,4 +851,15 @@ extern "C" return status; } + + char* ctermid(char* s) + { + static char path[] = "/dev/tty"; + if (s) + { + strncpy(s, path, L_ctermid); + return s; + } + return path; + } }