Userspace: Add basic whoami command

This commit is contained in:
Bananymous 2023-08-15 09:03:51 +03:00
parent ba37183c9c
commit b30f4cbfb5
3 changed files with 33 additions and 0 deletions

View File

@ -14,6 +14,7 @@ set(USERSPACE_PROJECTS
test
touch
u8sum
whoami
yes
)

View File

@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.26)
project(whoami CXX)
set(SOURCES
main.cpp
)
add_executable(whoami ${SOURCES})
target_compile_options(whoami PUBLIC -O2 -g)
target_link_libraries(whoami PUBLIC libc ban)
add_custom_target(whoami-install
COMMAND cp ${CMAKE_CURRENT_BINARY_DIR}/whoami ${BANAN_BIN}/
DEPENDS whoami
)

16
userspace/whoami/main.cpp Normal file
View File

@ -0,0 +1,16 @@
#include <pwd.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
auto* pw = getpwuid(geteuid());
if (pw == nullptr)
{
printf("unknown user %d\n", geteuid());
return 1;
}
printf("%s\n", pw->pw_name);
endpwent();
return 0;
}