From 9dc31cc1ce91986d45a10edba967bfb1c890797a Mon Sep 17 00:00:00 2001 From: Roy Ivy III Date: Sat, 6 Oct 2018 20:03:27 -0500 Subject: [PATCH] env: enhance support for windows commands (BAT/CMD, builtins) .# Discussion `env`/`uutils env` didn't support CMD built-in commands (dir, echo, erase, ...), BAT/CMD files, nor the usual semantics for command search and execution via PATHEXT (eg, it wouldn't find/execute `batch.BAT` when given just `batch`). This patch executes the commands via a CMD subshell and fixes all of those issues. --- src/env/env.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/env/env.rs b/src/env/env.rs index 931277bb6..2846d737e 100644 --- a/src/env/env.rs +++ b/src/env/env.rs @@ -40,6 +40,17 @@ fn print_env(null: bool) { } } +#[cfg(not(windows))] +fn build_command(mut args: Vec) -> (String, Vec) { + (args.remove(0), args) +} + +#[cfg(windows)] +fn build_command(mut args: Vec) -> (String, Vec) { + args.insert(0, "/d/c".to_string()); + (env::var("ComSpec").unwrap_or("cmd".to_string()), args) +} + pub fn uumain(args: Vec) -> i32 { let mut core_opts = new_coreopts!(SYNTAX, SUMMARY, LONG_HELP); core_opts @@ -198,8 +209,7 @@ pub fn uumain(args: Vec) -> i32 { } if !opts.program.is_empty() { - let prog = opts.program[0].clone(); - let args = &opts.program[1..]; + let (prog, args) = build_command(opts.program); match Command::new(prog).args(args).status() { Ok(exit) => { return if exit.success() {