From e22821799be6c0a27bc26d9e7521b301cb7e7272 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Thu, 23 May 2024 15:05:22 +0300 Subject: [PATCH] LibC: Implement getlogin() --- libc/include/limits.h | 1 + libc/unistd.cpp | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/libc/include/limits.h b/libc/include/limits.h index 4df636d524..05a634ab5d 100644 --- a/libc/include/limits.h +++ b/libc/include/limits.h @@ -59,6 +59,7 @@ __BEGIN_DECLS #define OPEN_MAX 64 #define NAME_MAX 255 +#define LOGIN_NAME_MAX 256 __END_DECLS diff --git a/libc/unistd.cpp b/libc/unistd.cpp index bec5435ee0..689a19c0d0 100644 --- a/libc/unistd.cpp +++ b/libc/unistd.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -465,3 +466,15 @@ int tcsetpgrp(int fildes, pid_t pgid_id) { return syscall(SYS_TCSETPGRP, fildes, pgid_id); } + +char* getlogin(void) +{ + static char buffer[LOGIN_NAME_MAX]; + auto* pw = getpwuid(geteuid()); + if (pw == nullptr) + return nullptr; + strncpy(buffer, pw->pw_name, LOGIN_NAME_MAX - 1); + buffer[LOGIN_NAME_MAX - 1] = '\0'; + endpwent(); + return buffer; +}