feat: add get_username to user

This commit is contained in:
Teesh 2025-03-30 13:29:10 +03:00
parent 900f84f7ec
commit 51659bde16
3 changed files with 15 additions and 7 deletions

View file

@ -1,6 +1,6 @@
use boxutils::args::ArgParser;
use boxutils::commands::Command;
use std::env;
use boxutils::cross::user;
pub struct WhoAmI;
@ -13,9 +13,7 @@ impl Command for WhoAmI {
return;
}
let username = env::var("USER") // Unix
.or_else(|_| env::var("USERNAME")) // Windows
.unwrap_or_else(|_| "unknown".to_string());
let username = user::get_username().unwrap_or_else(|| "unknown".to_string());
println!("{}", username);
}

View file

@ -35,3 +35,7 @@ impl UserPrivileges {
pub fn is_admin() -> bool {
UserPrivileges::is_root()
}
pub fn get_username() -> Option<String> {
std::env::var("USER").ok()
}

View file

@ -1,10 +1,10 @@
#![allow(dead_code)]
#![cfg(windows)]
use std::ptr;
use std::ffi::c_void;
use std::mem::{size_of, zeroed};
use std::os::raw::c_ulong;
use std::ptr;
#[link(name = "advapi32")]
unsafe extern "system" {
@ -44,7 +44,9 @@ impl AccessToken {
return None;
}
Some(Self { handle: token_handle })
Some(Self {
handle: token_handle,
})
}
/// Checks if the token is elevated (i.e., running as an administrator).
@ -80,4 +82,8 @@ pub fn is_admin() -> bool {
} else {
false
}
}
}
pub fn get_username() -> Option<String> {
std::env::var("USERNAME").ok()
}