diff --git a/source/lib/sysdep/os/unix/unix.cpp b/source/lib/sysdep/os/unix/unix.cpp index bc6d729e9d..1d7e5d21c5 100644 --- a/source/lib/sysdep/os/unix/unix.cpp +++ b/source/lib/sysdep/os/unix/unix.cpp @@ -188,3 +188,19 @@ size_t sys_max_sector_size() cached_sector_size = sysconf(_SC_PAGE_SIZE); return cached_sector_size; } + +std::wstring sys_get_user_name() +{ + // Prefer LOGNAME, fall back on getlogin + + const char* logname = getenv("LOGNAME"); + if (logname && strcmp(logname, "") != 0) + return std::wstring(logname, logname + strlen(logname)); + // TODO: maybe we should do locale conversion? + + char buf[256]; + if (getlogin_r(buf, ARRAY_SIZE(buf)) == 0) + return std::wstring(buf, buf + strlen(buf)); + + return L""; +} diff --git a/source/lib/sysdep/os/win/wsysdep.cpp b/source/lib/sysdep/os/win/wsysdep.cpp index 0f79db3aa9..950889ba60 100644 --- a/source/lib/sysdep/os/win/wsysdep.cpp +++ b/source/lib/sysdep/os/win/wsysdep.cpp @@ -349,6 +349,14 @@ LibError sys_get_executable_name(fs::wpath& pathname) return INFO::OK; } +std::wstring sys_get_user_name() +{ + wchar_t usernameBuf[256]; + DWORD size = ARRAY_SIZE(usernameBuf); + if(!GetUserNameW(usernameBuf, &size)) + return L""; + return usernameBuf; +} // callback for shell directory picker: used to set starting directory // (for user convenience). diff --git a/source/lib/sysdep/sysdep.h b/source/lib/sysdep/sysdep.h index db8ed5becc..79d3044cfc 100644 --- a/source/lib/sysdep/sysdep.h +++ b/source/lib/sysdep/sysdep.h @@ -106,6 +106,13 @@ LibError sys_get_module_filename(void* addr, fs::wpath& pathname); **/ LIB_API LibError sys_get_executable_name(fs::wpath& pathname); +/** + * get the current user's login name. + * + * @return login name, or empty string on error + */ +extern std::wstring sys_get_user_name(); + /** * have the user choose a directory via OS dialog. *