--- /dev/null
+// #![feature(vec_push_within_capacity)]
+use std::env;
+use std::fs;
+
+fn main() {
+ // Get args from command line
+ let args: Vec<String> = env::args().collect();
+ dbg!(&args);
+
+ // Ensure user is supplied with command
+ if args.len() < 2 {
+ println!("");
+ std::process::exit(1);
+ }
+
+ // read passwd file and make list of users
+ let passwd = match fs::read_to_string("/etc/passwd") {
+ Ok(file) => file,
+ Err(_) => {
+ eprintln!("Unable to read passwd file.");
+ std::process::exit(1);
+ }
+ };
+
+ let users: Vec<&str> = passwd.split('\n').collect();
+
+ dbg!(&users);
+
+ // finds a matching line for a user
+ let user = users.iter().find(|l| -> bool {
+ match l.split(':').collect::<Vec<&str>>() {
+ u if u.len() < 7 => false,
+ u if u[2].parse::<u32>().unwrap() < 1000 => false,
+ u if u[0] != &args[1] => false,
+ _ => true,
+ }
+ });
+
+ dbg!(&user);
+
+ match user {
+ Some(u) => {
+ format_user(u);
+ }
+ None => println!("User not found"),
+ }
+}
+
+fn format_user(user: &str) -> () {
+ let u: Vec<&str> = user.split(':').collect();
+ let mut gecos: Vec<&str> = Vec::with_capacity(5);
+ for g in u[4].split(',') {
+ gecos.push(g);
+ }
+
+ for _ in gecos.len()..5 {
+ gecos.push("");
+ }
+
+ // read passwd file and make list of users
+ let plan = match fs::read_to_string(format!("{}/.plan", u[5])) {
+ Ok(file) => file,
+ Err(_) => {
+ format!("{} has no plan", u[5])
+ }
+ };
+
+ dbg!(&gecos);
+
+ {
+ let mut line = format!("User: {}", u[0]);
+ line = fill_till_len(line.as_str(), 40);
+
+ if !gecos[0].is_empty() {
+ line.push_str(format!("Name: {}", gecos[0]).as_str());
+ }
+
+ println!("{}", line);
+ }
+
+ {
+ println!("Homepage: https://t-ch.net/~/{}", u[0]);
+ }
+
+ {
+ let mut line = String::new();
+ if !gecos[1].is_empty() {
+ line.push_str(format!("Office Location: {}", gecos[1]).as_str());
+ line = fill_till_len(line.as_str(), 40);
+ }
+
+ if !gecos[2].is_empty() {
+ line.push_str(format!("#: {}", gecos[2]).as_str());
+ }
+
+ if !line.is_empty() {
+ println!("{}", line);
+ }
+
+ println!("{}", plan)
+ }
+}
+
+fn fill_till_len(s: &str, len: usize) -> String {
+ let mut line = String::from(s);
+ for _ in line.len()..len {
+ line.push(' ');
+ }
+ line
+}