run(1) works

This commit is contained in:
dtb 2024-07-24 23:39:47 -06:00
parent f7e0cb5c14
commit e0f9641e93
Signed by: trinity
GPG Key ID: 34C0543BBB6AF81B

View File

@ -38,23 +38,26 @@ fn error(p: &str, n: &str, e: Error) -> ExitCode {
} }
fn usage(s: &str) -> ExitCode { fn usage(s: &str) -> ExitCode {
eprintln!("Usage: {} command [argument...]"); eprintln!("Usage: {} command [argument...]", s);
ExitCode::from(EX_USAGE as u8) ExitCode::from(EX_USAGE as u8)
} }
fn main() -> ExitCode { fn main() -> ExitCode {
let argv = args().collect::<Vec<String>>(); let argv = args().collect::<Vec<String>>();
let mut optind = 0; let mut optind = 1;
let mut arg0: Option<String> = None; /* arg0 is None if no argv[0] is meant to be given (argc will be 0),
let mut dir = String::from("."); * however I don't know how to do this in Rust, so the option is currently
* meaningless. -z will set this to None. */
let mut arg0: Option<String> = Some(String::default());
let mut dir = String::from("."); // $PWD for command
let mut cmd; while let Some(opt) = argv.getopt("C:n:z") {
while let Some(opt) = argv.getopt("C:n") {
match opt.opt() { match opt.opt() {
// Consistent with precedent; git, make, tar, etc.
Ok("C") => dir = opt.arg().unwrap(), Ok("C") => dir = opt.arg().unwrap(),
Ok("n") => arg0 = Some(opt.arg().unwrap()), Ok("n") => arg0 = Some(opt.arg().unwrap()),
// Ok("z") => arg0 = None,
_ => { return usage(&argv[0]); } _ => { return usage(&argv[0]); }
} }
optind = opt.ind(); optind = opt.ind();
@ -62,11 +65,15 @@ fn main() -> ExitCode {
if argv.len() - optind < 1 { return usage(&argv[0]); } if argv.len() - optind < 1 { return usage(&argv[0]); }
cmd = Command::new(argv[optind].clone()).current_dir(argv[1].clone()); let mut cmd = Command::new(argv[optind].clone());
cmd.current_dir(dir.clone());
if let Some(a) = arg0 { cmd.arg0(a); } match arg0 {
Some(a) if a != String::default() => { cmd.arg0(a); },
_ => () // TODO: argv[0]==NULL support
}
if argv.len() - optind > 2 { if argv.len() - optind > 1 { // there are arguments
let cmdargs = argv let cmdargs = argv
.iter() .iter()
.clone() .clone()
@ -77,18 +84,16 @@ fn main() -> ExitCode {
match cmd.spawn() { match cmd.spawn() {
Ok(mut child) => match child.wait() { Ok(mut child) => match child.wait() {
Ok(status) => { Ok(status) => match status.code() {
match status.code() { Some(code) => ExitCode::from(code as u8),
Some(code) => ExitCode::from(code as u8), None => {
None => { eprintln!("{}: {}: process terminated by signal",
eprintln!("{}: {}: process terminated by signal", argv[0], argv[2]);
argv[0], argv[2]); ExitCode::FAILURE
ExitCode::FAILURE
}
} }
}, },
Err(e) => error(&argv[0], &argv[2], e) Err(e) => error(&argv[0], &argv[optind], e)
}, },
Err(e) => error(&argv[0], &argv[2], e) Err(e) => error(&argv[0], &argv[optind], e)
} }
} }