Skip to content
This repository has been archived by the owner on Nov 29, 2024. It is now read-only.

Commit

Permalink
sys_info: add username
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Rozman <[email protected]>
  • Loading branch information
rozmansi committed Sep 30, 2024
1 parent 9fb8998 commit 8844c40
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions include/stdex/sys_info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ namespace stdex
///
bool elevated;

///
/// Currently signed in user
///
sstring username;

sys_info_t() :
os_platform(platform_id::unknown),
#ifdef _WIN32
Expand Down Expand Up @@ -225,6 +230,27 @@ namespace stdex
// TODO: Set admin.
elevated = geteuid() == 0;
#endif

#ifdef _WIN32
{
TCHAR szStackBuffer[0x100];
ULONG ulSize = _countof(szStackBuffer);
if (GetUserNameEx(NameFormat, szStackBuffer, &ulSize))
username.assign(szStackBuffer, ulSize);
if (GetLastError() == ERROR_MORE_DATA) {
// Allocate buffer on heap and retry.
username.resize(ulSize - 1);
if (!GetUserNameEx(NameFormat, &username[0], &ulSize))
username.clear();
}
}
#else
{
struct passwd *pw = getpwuid(geteuid());
if (pw && pw->pw_name)
username = pw->pw_name;
}
#endif
}

///
Expand Down

0 comments on commit 8844c40

Please sign in to comment.