coreutils_rs/common/
mod.rs1pub mod io;
2
3#[inline]
6pub fn gnu_name(binary_name: &str) -> &str {
7 binary_name.strip_prefix('f').unwrap_or(binary_name)
8}
9
10#[inline]
14pub fn reset_sigpipe() {
15 #[cfg(unix)]
16 unsafe {
17 libc::signal(libc::SIGPIPE, libc::SIG_DFL);
18 }
19}
20
21#[cfg(target_os = "linux")]
26pub fn enlarge_stdout_pipe() {
27 let mut stat: libc::stat = unsafe { std::mem::zeroed() };
28 if unsafe { libc::fstat(1, &mut stat) } != 0 {
29 return;
30 }
31 if (stat.st_mode & libc::S_IFMT) != libc::S_IFIFO {
32 return;
33 }
34 for &size in &[8 * 1024 * 1024i32, 1024 * 1024, 256 * 1024] {
35 if unsafe { libc::fcntl(1, libc::F_SETPIPE_SZ, size) } > 0 {
36 break;
37 }
38 }
39}
40
41#[cfg(not(target_os = "linux"))]
42pub fn enlarge_stdout_pipe() {}
43
44#[cold]
48#[inline(never)]
49pub fn io_error_msg(e: &std::io::Error) -> String {
50 if let Some(raw) = e.raw_os_error() {
51 let os_err = std::io::Error::from_raw_os_error(raw);
52 let msg = format!("{}", os_err);
53 msg.replace(&format!(" (os error {})", raw), "")
54 } else {
55 format!("{}", e)
56 }
57}