std/sys/process/
mod.rs

1cfg_select! {
2    target_family = "unix" => {
3        mod unix;
4        use unix as imp;
5    }
6    target_os = "windows" => {
7        mod windows;
8        use windows as imp;
9    }
10    target_os = "uefi" => {
11        mod uefi;
12        use uefi as imp;
13    }
14    _ => {
15        mod unsupported;
16        use unsupported as imp;
17    }
18}
19
20// This module is shared by all platforms, but nearly all platforms except for
21// the "normal" UNIX ones leave some of this code unused.
22#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
23mod env;
24
25pub use env::CommandEnvs;
26pub use imp::{
27    Command, CommandArgs, EnvKey, ExitCode, ExitStatus, ExitStatusError, Process, Stdio,
28};
29
30#[cfg(any(
31    all(
32        target_family = "unix",
33        not(any(
34            target_os = "espidf",
35            target_os = "horizon",
36            target_os = "vita",
37            target_os = "nuttx"
38        ))
39    ),
40    target_os = "windows",
41))]
42pub fn output(cmd: &mut Command) -> crate::io::Result<(ExitStatus, Vec<u8>, Vec<u8>)> {
43    use crate::sys::pipe::read2;
44
45    let (mut process, mut pipes) = cmd.spawn(Stdio::MakePipe, false)?;
46
47    drop(pipes.stdin.take());
48    let (mut stdout, mut stderr) = (Vec::new(), Vec::new());
49    match (pipes.stdout.take(), pipes.stderr.take()) {
50        (None, None) => {}
51        (Some(out), None) => {
52            let res = out.read_to_end(&mut stdout);
53            res.unwrap();
54        }
55        (None, Some(err)) => {
56            let res = err.read_to_end(&mut stderr);
57            res.unwrap();
58        }
59        (Some(out), Some(err)) => {
60            let res = read2(out, &mut stdout, err, &mut stderr);
61            res.unwrap();
62        }
63    }
64
65    let status = process.wait()?;
66    Ok((status, stdout, stderr))
67}
68
69#[cfg(not(any(
70    all(
71        target_family = "unix",
72        not(any(
73            target_os = "espidf",
74            target_os = "horizon",
75            target_os = "vita",
76            target_os = "nuttx"
77        ))
78    ),
79    target_os = "windows",
80)))]
81pub use imp::output;