Skip to content

Commit

Permalink
Match GNU diff's implementation for exit codes (fixes uutils#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
oSoMoN authored and sylvestre committed Feb 22, 2024
1 parent 3bc8668 commit 1241db4
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,43 @@ use std::env;

use std::fs;
use std::io::{self, Write};
use std::process::{exit, ExitCode};

mod context_diff;
mod ed_diff;
mod normal_diff;
mod params;
mod unified_diff;

fn main() -> Result<(), String> {
// Exit codes are documented at
// https://www.gnu.org/software/diffutils/manual/html_node/Invoking-diff.html.
// An exit status of 0 means no differences were found,
// 1 means some differences were found,
// and 2 means trouble.
fn main() -> ExitCode {
let opts = env::args_os();
let Params {
from,
to,
context_count,
format,
} = parse_params(opts)?;
} = parse_params(opts).unwrap_or_else(|error| {
eprintln!("{error}");
exit(2);
});
// read files
let from_content = match fs::read(&from) {
Ok(from_content) => from_content,
Err(e) => {
return Err(format!("Failed to read from-file: {e}"));
eprintln!("Failed to read from-file: {e}");
return ExitCode::from(2);
}
};
let to_content = match fs::read(&to) {
Ok(to_content) => to_content,
Err(e) => {
return Err(format!("Failed to read to-file: {e}"));
eprintln!("Failed to read to-file: {e}");
return ExitCode::from(2);
}
};
// run diff
Expand All @@ -53,8 +64,15 @@ fn main() -> Result<(), String> {
&to.to_string_lossy(),
context_count,
),
Format::Ed => ed_diff::diff(&from_content, &to_content)?,
Format::Ed => ed_diff::diff(&from_content, &to_content).unwrap_or_else(|error| {
eprintln!("{error}");
exit(2);
}),
};
io::stdout().write_all(&result).unwrap();
Ok(())
if result.is_empty() {
ExitCode::SUCCESS
} else {
ExitCode::from(1)
}
}

0 comments on commit 1241db4

Please sign in to comment.