Initial commit.

This commit is contained in:
raute 2022-09-27 20:09:56 +02:00
parent e9ef2e17fb
commit 909d96f584
2 changed files with 68 additions and 0 deletions

9
Cargo.toml Normal file
View 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
View 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);
}