1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

os: use C.GetUserNameW for os.loginname() instead + improvements os.uname() (#9872)

This commit is contained in:
Bastian Buck 2021-04-25 17:16:14 +02:00 committed by GitHub
parent bfe0a7887f
commit 160b605640
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 5 deletions

View File

@ -217,6 +217,8 @@ fn C.ExpandEnvironmentStringsW(lpSrc &u16, lpDst &u16, nSize u32) u32
fn C.GetComputerNameW(&u16, &u32) bool fn C.GetComputerNameW(&u16, &u32) bool
fn C.GetUserNameW(&u16, &u32) bool
[trusted] [trusted]
fn C.SendMessageTimeout() u32 fn C.SendMessageTimeout() u32

View File

@ -2,6 +2,7 @@ module os
import strings import strings
#flag windows -l advapi32
#include <process.h> #include <process.h>
pub const ( pub const (
@ -376,10 +377,9 @@ pub fn debugger_present() bool {
} }
pub fn uname() Uname { pub fn uname() Uname {
// TODO: use Win32 Api functions instead
sys_and_ver := execute('cmd /c ver').output.split('[') sys_and_ver := execute('cmd /c ver').output.split('[')
nodename := execute('cmd /c hostname').output nodename := hostname()
machine := execute('cmd /c echo %PROCESSOR_ARCHITECTURE%').output machine := getenv('PROCESSOR_ARCHITECTURE')
return Uname{ return Uname{
sysname: sys_and_ver[0].trim_space() sysname: sys_and_ver[0].trim_space()
nodename: nodename nodename: nodename
@ -400,8 +400,13 @@ pub fn hostname() string {
} }
pub fn loginname() string { pub fn loginname() string {
// TODO: use C.GetUserName(&char, u32) bool instead loginname := [255]u16{}
return execute('cmd /c echo %USERNAME%').output size := u32(255)
res := C.GetUserNameW(&loginname[0], &size)
if !res {
return error(get_error_msg(int(C.GetLastError())))
}
return unsafe { string_from_wide(&loginname[0]) }
} }
// `is_writable_folder` - `folder` exists and is writable to the process // `is_writable_folder` - `folder` exists and is writable to the process