LibC: Implement getlogin()

This commit is contained in:
Bananymous 2024-05-23 15:05:22 +03:00
parent 14dd9294aa
commit e22821799b
2 changed files with 14 additions and 0 deletions

View File

@ -59,6 +59,7 @@ __BEGIN_DECLS
#define OPEN_MAX 64
#define NAME_MAX 255
#define LOGIN_NAME_MAX 256
__END_DECLS

View File

@ -1,6 +1,7 @@
#include <BAN/Assert.h>
#include <kernel/Syscall.h>
#include <errno.h>
#include <pwd.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
@ -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;
}