1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
pub struct User {
pub env: &'static str,
pub host: &'static str,
pub hostname: &'static str,
pub os: &'static str,
pub platform: &'static str,
pub user: &'static str,
pub username: &'static str,
}
struct UserInfo {
pub env: String,
pub host: String,
pub hostname: String,
pub os: String,
pub platform: String,
pub user: String,
pub username: String,
}
static mut USER_INFO: FakeUserInfo = FakeUserInfo([0; std::mem::size_of::<UserInfo>()]);
#[repr(align(8))]
struct FakeUserInfo([u8; std::mem::size_of::<UserInfo>()]);
pub(crate) fn initialize_user_io() {
use whoami::*;
unsafe {
let user_info = &mut USER_INFO as *mut _ as *mut UserInfo;
*user_info = UserInfo {
env: env().to_string(),
host: host(),
hostname: hostname(),
os: os(),
platform: platform().to_string(),
user: whoami::user(),
username: username(),
};
}
}
pub fn user() -> User {
let userinfo = unsafe { &USER_INFO as *const _ as *const UserInfo };
User {
env: unsafe { &(*userinfo).env },
host: unsafe { &(*userinfo).host },
hostname: unsafe { &(*userinfo).hostname },
os: unsafe { &(*userinfo).os },
platform: unsafe { &(*userinfo).platform },
user: unsafe { &(*userinfo).user },
username: unsafe { &(*userinfo).username },
}
}
impl std::fmt::Display for User {
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
writeln!(f, "User {{")?;
writeln!(f, " env: \"{}\"", self.env)?;
writeln!(f, " host: \"{}\"", self.host)?;
writeln!(f, " hostname: \"{}\"", self.hostname)?;
writeln!(f, " os: \"{}\"", self.os)?;
writeln!(f, " platform: \"{}\"", self.platform)?;
writeln!(f, " user: \"{}\"", self.user)?;
writeln!(f, " username: \"{}\"", self.username)?;
write!(f, "}}")
}
}