-
-
Save BurntSushi/bc44576642c4fe9feedfb9f839245d92 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate walkdir; | |
use std::error::Error; | |
use std::fs::File; | |
use std::io::{self, Read, Write}; | |
use std::process; | |
use walkdir::WalkDir; | |
fn main() { | |
if let Err(err) = try_main() { | |
eprintln!("{}", err); | |
process::exit(1); | |
} | |
} | |
fn try_main() -> Result<(), Box<Error>> { | |
let nul = 0; | |
let stdout = io::stdout(); | |
let mut stdout = stdout.lock(); | |
for entry in WalkDir::new("./").into_iter().filter_map(|e| e.ok()) { | |
if !entry.file_type().is_file() { | |
continue; | |
} | |
let mut bytes_count = 0; | |
let mut file = File::open(entry.path()) | |
.map_err(|err| format!("{}: {}", entry.path().display(), err))?; | |
let mut s = Vec::with_capacity(file.metadata()?.len() as usize); | |
file.read_to_end(&mut s)?; | |
for b in s.into_iter() { | |
if b == nul { | |
writeln!( | |
stdout, | |
"{} bytes={} binary file", | |
entry.path().display(), | |
bytes_count | |
)?; | |
break; | |
} | |
bytes_count += 1; | |
} | |
writeln!(stdout, "{} bytes={}", entry.path().display(), bytes_count)?; | |
} | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment