|
1 | 1 | //! Emulated wait status for non-Unix #[cfg(unix) platforms
|
2 | 2 | //!
|
3 | 3 | //! Separate module to facilitate testing against a real Unix implementation.
|
| 4 | +use core::ffi::NonZero_c_int; |
4 | 5 |
|
5 | 6 | use crate::ffi::c_int;
|
6 | 7 | use crate::fmt;
|
7 | 8 |
|
| 9 | +use super::ExitStatusError; |
| 10 | + |
8 | 11 | /// Emulated wait status for use by `process_unsupported.rs`
|
9 | 12 | ///
|
10 | 13 | /// Uses the "traditional unix" encoding. For use on platfors which are `#[cfg(unix)]`
|
@@ -40,6 +43,18 @@ impl ExitStatus {
|
40 | 43 | if (w & 0x7f) == 0 { Some((w & 0xff00) >> 8) } else { None }
|
41 | 44 | }
|
42 | 45 |
|
| 46 | + pub fn exit_ok(&self) -> Result<(), ExitStatusError> { |
| 47 | + // This assumes that WIFEXITED(status) && WEXITSTATUS==0 corresponds to status==0. This is |
| 48 | + // true on all actual versions of Unix, is widely assumed, and is specified in SuS |
| 49 | + // https://pubs.opengroup.org/onlinepubs/9699919799/functions/wait.html. If it is not |
| 50 | + // true for a platform pretending to be Unix, the tests (our doctests, and also |
| 51 | + // process_unix/tests.rs) will spot it. `ExitStatusError::code` assumes this too. |
| 52 | + match NonZero_c_int::try_from(self.wait_status) { |
| 53 | + /* was nonzero */ Ok(failure) => Err(ExitStatusError(failure)), |
| 54 | + /* was zero, couldn't convert */ Err(_) => Ok(()), |
| 55 | + } |
| 56 | + } |
| 57 | + |
43 | 58 | pub fn signal(&self) -> Option<i32> {
|
44 | 59 | let signal = self.wait_status & 0x007f;
|
45 | 60 | if signal > 0 && signal < 0x7f { Some(signal) } else { None }
|
|
0 commit comments