Attempt to run a process if command isn't a builtin

This commit is contained in:
pjht 2024-11-08 14:58:58 -06:00
parent 7eaf3b933c
commit 6500677f35
Signed by: pjht
GPG Key ID: CA239FC6934E6F3A

View File

@ -1,6 +1,6 @@
use std::{ use std::{
io::{self, Write}, io::{self, Write},
path::{Component, Path, PathBuf}, path::{Component, Path, PathBuf}, process::Command,
}; };
use clap::Parser; use clap::Parser;
@ -157,7 +157,21 @@ fn main() {
println!("exit"); println!("exit");
println!("help"); println!("help");
} }
_ => println!("psh: {}: command not found", cmd[0]), _ => {
let path = if cmd[0].starts_with(".") || cmd[0].starts_with("~") || cmd[0].starts_with("/") {
cmd[0].to_string()
} else {
format!("/bin/{}", cmd[0])
};
let mut proc = match Command::new(path).spawn() {
Ok(x) => x,
Err(e) => {
println!("psh: {}: {}", cmd[0], e);
continue;
}
};
let _ = proc.wait().unwrap();
}
} }
} }
} }