Skip to content

Commit 0444a6d

Browse files
author
Salim Afiune
committed
Add as_vec_u16 fn
Simple fn that will convert a `OsString` into a `Vec<u16>`. Very useful when you need to interact with WindowsAPI. Signed-off-by: Salim Afiune <[email protected]>
1 parent abe5e1a commit 0444a6d

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

src/libstd/sys/windows/process.rs

+18-18
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ impl Command {
181181
}
182182

183183
let (envp, _data) = make_envp(self.env.as_ref())?;
184-
let (dirp, _data) = make_dirp(self.cwd.as_ref())?;
184+
let (dirp, _data) = as_vec_u16(self.cwd.as_ref())?;
185185
let mut pi = zeroed_process_information();
186186

187187
// Prepare all stdio handles to be inherited by the child. This
@@ -216,7 +216,7 @@ impl Command {
216216
si.hStdError = stderr.raw();
217217

218218
unsafe {
219-
cvt(c::CreateProcessW(app_name.unwrap_or(ptr::null()),
219+
cvt(c::CreateProcessW(app_name.unwrap_or(ptr::null_mut()),
220220
cmd_str.as_mut_ptr(),
221221
ptr::null_mut(),
222222
ptr::null_mut(),
@@ -423,6 +423,9 @@ fn zeroed_process_information() -> c::PROCESS_INFORMATION {
423423
fn make_command_line(prog: &OsStr, args: &[OsString],
424424
env: Option<&collections::HashMap<OsString, OsString>>)
425425
-> io::Result<(Option<Vec<u16>>, Vec<u16>)> {
426+
// Encode the command and arguments in a command line string such
427+
// that the spawned process may recover them using CommandLineToArgvW.
428+
let mut cmd: Vec<u16> = Vec::new();
426429
// If the program is a BATCH file we must start the command interpreter; set
427430
// ApplicationName to cmd.exe and set CommandLine to the following arguments:
428431
// `/c` plus the name of the batch file.
@@ -432,22 +435,18 @@ fn make_command_line(prog: &OsStr, args: &[OsString],
432435
if batch.is_some() {
433436
if let Some(e) = env {
434437
if let Some(cmd_exe) = e.get("COMSPEC") {
435-
app = Some(ensure_no_nuls(cmd_exe)?.encode_wide().collect());
438+
app = as_vec_u16(cmd_exe).ok().map(|(vec, _d)| vec);
436439
}
437440
}
438441
// If we were unable to find COMSPEC, default to `cmd.exe`
439-
if !app.is_some() {
440-
app = Some(OsStr::new("cmd.exe").encode_wide().collect());
442+
if app.is_none() {
443+
app = as_vec_u16("cmd.exe").ok().map(|(vec, _d)| vec);
441444
}
442-
// Prepend the argument to the program
443-
let mut cmd_prog = OsString::from("cmd /c ");
444-
cmd_prog.push(prog);
445-
let prog = cmd_prog.as_os_str();
445+
// Prepend the argument to the CommandLine
446+
append_arg(&mut cmd, OsString::from("cmd"))?;
447+
append_arg(&mut cmd, OsString::from("/c"))?;
446448
}
447449

448-
// Encode the command and arguments in a command line string such
449-
// that the spawned process may recover them using CommandLineToArgvW.
450-
let mut cmd: Vec<u16> = Vec::new();
451450
append_arg(&mut cmd, prog)?;
452451
for arg in args {
453452
cmd.push(' ' as u16);
@@ -519,12 +518,13 @@ fn make_envp(env: Option<&collections::HashMap<OsString, OsString>>)
519518
}
520519
}
521520

522-
fn make_dirp(d: Option<&OsString>) -> io::Result<(*const u16, Vec<u16>)> {
523-
match d {
524-
Some(dir) => {
525-
let mut dir_str: Vec<u16> = ensure_no_nuls(dir)?.encode_wide().collect();
526-
dir_str.push(0);
527-
Ok((dir_str.as_ptr(), dir_str))
521+
// Convert a OsString to a Vec<u16>
522+
fn as_vec_u16(str: Option<&OsString>) -> io::Result<(*const u16, Vec<u16>)> {
523+
match str {
524+
Some(s) => {
525+
let mut s_str: Vec<u16> = ensure_no_nuls(s)?.encode_wide().collect();
526+
s_str.push(0);
527+
Ok((s_str.as_ptr(), s_str))
528528
},
529529
None => Ok((ptr::null(), Vec::new()))
530530
}

0 commit comments

Comments
 (0)