diff --git a/src/libstd/sys/vxworks/ext/process.rs b/src/libstd/sys/vxworks/ext/process.rs index e535c4aa122f0..201e9bc658c6d 100644 --- a/src/libstd/sys/vxworks/ext/process.rs +++ b/src/libstd/sys/vxworks/ext/process.rs @@ -2,6 +2,7 @@ #![stable(feature = "rust1", since = "1.0.0")] +use crate::ffi::OsStr; use crate::io; use crate::process; use crate::sys; @@ -105,6 +106,15 @@ pub trait CommandExt { /// cross-platform `spawn` instead. #[stable(feature = "process_exec2", since = "1.9.0")] fn exec(&mut self) -> io::Error; + + /// Set executable argument + /// + /// Set the first process argument, `argv[0]`, to something other than the + /// default executable path. + #[unstable(feature = "process_set_argv0", issue = "66510")] + fn arg0(&mut self, arg: S) -> &mut process::Command + where + S: AsRef; } #[stable(feature = "rust1", since = "1.0.0")] @@ -130,6 +140,14 @@ impl CommandExt for process::Command { fn exec(&mut self) -> io::Error { self.as_inner_mut().exec(sys::process::Stdio::Inherit) } + + fn arg0(&mut self, arg: S) -> &mut process::Command + where + S: AsRef, + { + self.as_inner_mut().set_arg_0(arg.as_ref()); + self + } } /// Unix-specific extensions to [`process::ExitStatus`]. diff --git a/src/libstd/sys/vxworks/process/process_common.rs b/src/libstd/sys/vxworks/process/process_common.rs index a8139a27537a9..6d5506bec5f7d 100644 --- a/src/libstd/sys/vxworks/process/process_common.rs +++ b/src/libstd/sys/vxworks/process/process_common.rs @@ -90,8 +90,8 @@ impl Command { let program = os2c(program, &mut saw_nul); Command { argv: Argv(vec![program.as_ptr(), ptr::null()]), + args: vec![program.clone()], program, - args: Vec::new(), env: Default::default(), cwd: None, uid: None, @@ -104,11 +104,19 @@ impl Command { } } + pub fn set_arg_0(&mut self, arg: &OsStr) { + // Set a new arg0 + let arg = os2c(arg, &mut self.saw_nul); + debug_assert!(self.argv.0.len() > 1); + self.argv.0[0] = arg.as_ptr(); + self.args[0] = arg; + } + pub fn arg(&mut self, arg: &OsStr) { // Overwrite the trailing NULL pointer in `argv` and then add a new null // pointer. let arg = os2c(arg, &mut self.saw_nul); - self.argv.0[self.args.len() + 1] = arg.as_ptr(); + self.argv.0[self.args.len()] = arg.as_ptr(); self.argv.0.push(ptr::null()); // Also make sure we keep track of the owned value to schedule a @@ -133,6 +141,10 @@ impl Command { &self.argv.0 } + pub fn get_program(&self) -> &CStr { + &*self.program + } + #[allow(dead_code)] pub fn get_cwd(&self) -> &Option { &self.cwd @@ -315,8 +327,12 @@ impl ChildStdio { impl fmt::Debug for Command { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{:?}", self.program)?; - for arg in &self.args { + if self.program != self.args[0] { + write!(f, "[{:?}] ", self.program)?; + } + write!(f, "{:?}", self.args[0])?; + + for arg in &self.args[1..] { write!(f, " {:?}", arg)?; } Ok(()) diff --git a/src/libstd/sys/vxworks/process/process_vxworks.rs b/src/libstd/sys/vxworks/process/process_vxworks.rs index ced70dea27f99..f7e84ae3de9c7 100644 --- a/src/libstd/sys/vxworks/process/process_vxworks.rs +++ b/src/libstd/sys/vxworks/process/process_vxworks.rs @@ -67,7 +67,7 @@ impl Command { let _lock = sys::os::env_lock(); let ret = libc::rtpSpawn( - self.get_argv()[0], // executing program + self.get_program().as_ptr(), self.get_argv().as_ptr() as *mut *const c_char, // argv c_envp as *mut *const c_char, 100 as c_int, // initial priority