Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// For the full copyright and license information, please view the LICENSE-*
// files that was distributed with this source code.

use crate::safe_println;
use crate::utils::format_failure_to_read_input_file;
use std::env::{self, ArgsOs};
use std::ffi::OsString;
Expand Down Expand Up @@ -721,7 +722,7 @@ fn report_difference(
format_visible_byte(to_byte)
);
}
println!();
safe_println!();
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub fn main(opts: Peekable<ArgsOs>) -> ExitCode {
params.to.to_string_lossy()
);
} else {
io::stdout().write_all(&result).unwrap();
let _ = io::stdout().write_all(&result);
}
if result.is_empty() {
maybe_report_identical_files();
Expand Down
12 changes: 12 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,15 @@ macro_rules! assert_diff_eq {
assert_eq!(actual, $expected);
}};
}

/// A safe version of println!() that does not panic if stdout is redirected to /dev/full
#[macro_export]
macro_rules! safe_println {
() => {
let _ = ::std::io::Write::write_all(&mut ::std::io::stdout(), b"\n");
};
($($arg:tt)*) => {
let _ = ::std::io::Write::write_fmt(&mut ::std::io::stdout(), format_args!($($arg)*))
.and_then(|_| ::std::io::Write::write_all(&mut ::std::io::stdout(), b"\n"));
};
}
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ fn name(binary_path: &Path) -> &OsStr {
const VERSION: &str = env!("CARGO_PKG_VERSION");

fn usage(name: &str) {
println!("{name} {VERSION} (multi-call binary)\n");
println!("Usage: {name} [function [arguments...]]\n");
println!("Currently defined functions:\n");
println!(" cmp, diff\n");
safe_println!("{name} {VERSION} (multi-call binary)\n");
safe_println!("Usage: {name} [function [arguments...]]\n");
safe_println!("Currently defined functions:\n");
safe_println!(" cmp, diff\n");
}

fn second_arg_error(name: &OsStr) -> ! {
Expand Down
Loading