cjrh_moreutils_common/
lib.rs1use std::process::ExitStatus;
4
5#[cfg(unix)]
6use std::os::unix::process::{CommandExt, ExitStatusExt};
7
8pub fn status_code(status: ExitStatus) -> i32 {
9 if let Some(code) = status.code() {
10 code
11 } else {
12 #[cfg(unix)]
13 {
14 128 + status.signal().unwrap_or(1)
15 }
16 #[cfg(not(unix))]
17 {
18 1
19 }
20 }
21}
22
23pub fn exit_with_status(status: ExitStatus) -> ! {
24 std::process::exit(status_code(status));
25}
26
27pub fn shell_command(command: &str) -> std::process::Command {
28 let mut cmd = std::process::Command::new("/bin/sh");
29 #[cfg(unix)]
30 cmd.arg0("sh");
31 cmd.arg("-c").arg("--").arg(command);
32 cmd
33}
34
35pub fn usage(msg: &str) -> ! {
36 eprintln!("{msg}");
37 std::process::exit(1)
38}
39
40pub fn write_all_or_die<W: std::io::Write>(mut w: W, bytes: &[u8]) {
41 if let Err(e) = w.write_all(bytes) {
42 eprintln!("write: {e}");
43 std::process::exit(1);
44 }
45}