@@ -181,7 +181,7 @@ impl Command {
181
181
}
182
182
183
183
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 ( ) ) ?;
185
185
let mut pi = zeroed_process_information ( ) ;
186
186
187
187
// Prepare all stdio handles to be inherited by the child. This
@@ -216,7 +216,7 @@ impl Command {
216
216
si. hStdError = stderr. raw ( ) ;
217
217
218
218
unsafe {
219
- cvt ( c:: CreateProcessW ( app_name. unwrap_or ( ptr:: null ( ) ) ,
219
+ cvt ( c:: CreateProcessW ( app_name. unwrap_or ( ptr:: null_mut ( ) ) ,
220
220
cmd_str. as_mut_ptr ( ) ,
221
221
ptr:: null_mut ( ) ,
222
222
ptr:: null_mut ( ) ,
@@ -423,6 +423,9 @@ fn zeroed_process_information() -> c::PROCESS_INFORMATION {
423
423
fn make_command_line ( prog : & OsStr , args : & [ OsString ] ,
424
424
env : Option < & collections:: HashMap < OsString , OsString > > )
425
425
-> 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 ( ) ;
426
429
// If the program is a BATCH file we must start the command interpreter; set
427
430
// ApplicationName to cmd.exe and set CommandLine to the following arguments:
428
431
// `/c` plus the name of the batch file.
@@ -432,22 +435,18 @@ fn make_command_line(prog: &OsStr, args: &[OsString],
432
435
if batch. is_some ( ) {
433
436
if let Some ( e) = env {
434
437
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 ) ;
436
439
}
437
440
}
438
441
// 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 ) ;
441
444
}
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" ) ) ?;
446
448
}
447
449
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 ( ) ;
451
450
append_arg ( & mut cmd, prog) ?;
452
451
for arg in args {
453
452
cmd. push ( ' ' as u16 ) ;
@@ -519,12 +518,13 @@ fn make_envp(env: Option<&collections::HashMap<OsString, OsString>>)
519
518
}
520
519
}
521
520
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) )
528
528
} ,
529
529
None => Ok ( ( ptr:: null ( ) , Vec :: new ( ) ) )
530
530
}
0 commit comments