coreutils_rs/common/mod.rs
1pub mod io;
2
3/// Get the GNU-compatible tool name by stripping the 'f' prefix.
4/// e.g., "fmd5sum" -> "md5sum", "fcut" -> "cut"
5#[inline]
6pub fn gnu_name(binary_name: &str) -> &str {
7 binary_name.strip_prefix('f').unwrap_or(binary_name)
8}
9
10/// Reset SIGPIPE to default behavior (SIG_DFL) for GNU coreutils compatibility.
11/// Rust sets SIGPIPE to SIG_IGN by default, but GNU tools are killed by SIGPIPE
12/// (exit code 141 = 128 + 13). This must be called at the start of main().
13#[inline]
14pub fn reset_sigpipe() {
15 #[cfg(unix)]
16 unsafe {
17 libc::signal(libc::SIGPIPE, libc::SIG_DFL);
18 }
19}
20
21/// Format an IO error message without the "(os error N)" suffix.
22/// GNU coreutils prints e.g. "No such file or directory" while Rust's
23/// Display impl adds " (os error 2)". This strips the suffix for compat.
24pub fn io_error_msg(e: &std::io::Error) -> String {
25 if let Some(raw) = e.raw_os_error() {
26 let os_err = std::io::Error::from_raw_os_error(raw);
27 let msg = format!("{}", os_err);
28 msg.replace(&format!(" (os error {})", raw), "")
29 } else {
30 format!("{}", e)
31 }
32}