mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-30 12:37:49 +00:00
whoami: migrate winapi
to windows-sys
crate
This commit is contained in:
parent
f95a14537b
commit
0eae0fdb74
3 changed files with 6 additions and 8 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -3147,7 +3147,7 @@ dependencies = [
|
|||
"clap 4.0.17",
|
||||
"libc",
|
||||
"uucore",
|
||||
"winapi",
|
||||
"windows-sys 0.42.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
|
@ -19,7 +19,7 @@ clap = { version = "4.0", features = ["wrap_help", "cargo"] }
|
|||
uucore = { version=">=0.0.16", package="uucore", path="../../uucore", features=["entries"] }
|
||||
|
||||
[target.'cfg(target_os = "windows")'.dependencies]
|
||||
winapi = { version = "0.3", features = ["lmcons"] }
|
||||
windows-sys = { version = "0.42.0", default-features = false, features = ["Win32_NetworkManagement_NetManagement", "Win32_System_WindowsProgramming", "Win32_Foundation"] }
|
||||
|
||||
[target.'cfg(unix)'.dependencies]
|
||||
libc = "0.2.135"
|
||||
|
|
|
@ -10,17 +10,15 @@
|
|||
use std::ffi::OsString;
|
||||
use std::io;
|
||||
use std::os::windows::ffi::OsStringExt;
|
||||
|
||||
use winapi::shared::lmcons;
|
||||
use winapi::shared::minwindef::DWORD;
|
||||
use winapi::um::winbase;
|
||||
use windows_sys::Win32::NetworkManagement::NetManagement::UNLEN;
|
||||
use windows_sys::Win32::System::WindowsProgramming::GetUserNameW;
|
||||
|
||||
pub fn get_username() -> io::Result<OsString> {
|
||||
const BUF_LEN: DWORD = lmcons::UNLEN + 1;
|
||||
const BUF_LEN: u32 = UNLEN + 1;
|
||||
let mut buffer = [0_u16; BUF_LEN as usize];
|
||||
let mut len = BUF_LEN;
|
||||
// SAFETY: buffer.len() == len
|
||||
if unsafe { winbase::GetUserNameW(buffer.as_mut_ptr(), &mut len) } == 0 {
|
||||
if unsafe { GetUserNameW(buffer.as_mut_ptr(), &mut len) } == 0 {
|
||||
return Err(io::Error::last_os_error());
|
||||
}
|
||||
Ok(OsString::from_wide(&buffer[..len as usize - 1]))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue