Initial commit.
This commit is contained in:
parent
e9ef2e17fb
commit
909d96f584
9
Cargo.toml
Normal file
9
Cargo.toml
Normal file
@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "cmpr"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
clap = { version = "3", features = ["derive"] }
|
59
src/main.rs
Normal file
59
src/main.rs
Normal file
@ -0,0 +1,59 @@
|
||||
use clap::Parser;
|
||||
use std::fs;
|
||||
use std::fs::File;
|
||||
use std::io::BufReader;
|
||||
use std::io::prelude::*;
|
||||
use std::process;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[clap(author, version, about, long_about = None)]
|
||||
struct Args {
|
||||
/// file A
|
||||
file_a: String,
|
||||
|
||||
/// file B
|
||||
file_b: String,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args = Args::parse();
|
||||
|
||||
let size_a = fs::metadata(&args.file_a).unwrap().len();
|
||||
let size_b = fs::metadata(&args.file_b).unwrap().len();
|
||||
|
||||
if size_a != size_b {
|
||||
println!("Files have different sizes.");
|
||||
process::exit(1);
|
||||
}
|
||||
|
||||
let handle_a = File::open(&args.file_a).unwrap();
|
||||
let handle_b = File::open(&args.file_b).unwrap();
|
||||
|
||||
let mut reader_a = BufReader::new(handle_a);
|
||||
let mut reader_b = BufReader::new(handle_b);
|
||||
|
||||
let mut buf_a = [0; 1024];
|
||||
let mut buf_b = [0; 1024];
|
||||
|
||||
loop {
|
||||
let len_a = reader_a.read(&mut buf_a).unwrap();
|
||||
let len_b = reader_b.read(&mut buf_b).unwrap();
|
||||
|
||||
if len_a == 0 && len_b == 0 {
|
||||
break;
|
||||
}
|
||||
|
||||
if len_a != len_b {
|
||||
println!("Files are NOT equal.");
|
||||
process::exit(2);
|
||||
}
|
||||
|
||||
if buf_a != buf_b {
|
||||
println!("Files are NOT equal.");
|
||||
process::exit(3);
|
||||
}
|
||||
}
|
||||
|
||||
println!("Files are equal.");
|
||||
process::exit(0);
|
||||
}
|
Loading…
Reference in New Issue
Block a user