From 028330ab9215d136d3b48c240fb52ba921cc6eca Mon Sep 17 00:00:00 2001 From: Atsukoro1 Date: Thu, 1 Dec 2022 12:01:17 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20get=20shell?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.rs | 2 +- src/system/host.rs | 25 ++++++++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5c385f5..e66ca52 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ mod system; fn main() { - println!("{}", system::host::get_uptime()) + println!("{}", system::host::get_shell()); } diff --git a/src/system/host.rs b/src/system/host.rs index c6f0f6c..0076ca2 100644 --- a/src/system/host.rs +++ b/src/system/host.rs @@ -21,7 +21,9 @@ pub fn get_user() -> String { .stdout .iter() .map(|&c| c as char) - .collect() + .collect::() + .trim() + .to_string() } #[cfg(target_os = "linux")] @@ -74,4 +76,25 @@ pub fn get_uptime() -> String { let seconds = uptime % 60; format!("{}d {}h {}m {}s", days, hours, minutes, seconds) +} + +#[cfg(target_os = "linux")] +pub fn get_shell() -> String { + let mut final_str = String::new(); + let mut temp_buf: String = String::new(); + + let mut f = File::open("/etc/passwd").unwrap(); + f.read_to_string(&mut temp_buf).unwrap(); + + let lines: &Vec<&str> = &temp_buf.lines().collect(); + + lines.into_iter().for_each(|line| { + if line.contains(&get_user()) { + final_str = line.split(":") + .collect::>()[6] + .to_string(); + } + }); + + final_str } \ No newline at end of file