coreutils/cksum/cksum.rs

131 lines
3.1 KiB
Rust
Raw Normal View History

2014-05-16 21:01:20 +00:00
#![crate_id(name="cksum", vers="1.0.0", author="Michael Gehring")]
#![feature(macro_rules)]
/*
* This file is part of the uutils coreutils package.
*
* (c) Michael Gehring <mg@ebfe.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
extern crate getopts;
use std::io::{BufferedReader, EndOfFile, File, IoError, IoResult, print};
use std::io::stdio::stdin;
#[path="../common/util.rs"]
mod util;
static NAME : &'static str = "cksum";
static VERSION : &'static str = "1.0.0";
fn crc_update(mut crc: u32, input: u8) -> u32 {
crc ^= input as u32 << 24;
2014-06-25 11:12:43 +00:00
for _ in range(0u, 8) {
2014-05-16 21:01:20 +00:00
if crc & 0x80000000 != 0 {
crc <<= 1;
crc ^= 0x04c11db7;
} else {
crc <<= 1;
}
}
crc
}
fn crc_final(mut crc: u32, mut length: uint) -> u32 {
while length != 0 {
crc = crc_update(crc, length as u8);
length >>= 8;
}
!crc
}
fn cksum(fname: &str) -> IoResult<(u32, uint)> {
let mut crc = 0u32;
let mut size = 0u;
let mut rd = try!(open_file(fname));
loop {
match rd.read_byte() {
Ok(b) => {
crc = crc_update(crc, b);
size += 1;
}
Err(err) => {
return match err {
IoError{kind: k, ..} if k == EndOfFile => Ok((crc_final(crc, size), size)),
_ => Err(err),
}
}
}
}
}
fn open_file(name: &str) -> IoResult<Box<Reader>> {
match name {
"-" => Ok(box stdin() as Box<Reader>),
_ => {
let f = try!(File::open(&Path::new(name)));
Ok(box BufferedReader::new(f) as Box<Reader>)
}
}
}
pub fn uumain(args: Vec<String>) -> int {
2014-05-16 21:01:20 +00:00
let opts = [
getopts::optflag("h", "help", "display this help and exit"),
getopts::optflag("V", "version", "output version information and exit"),
];
2014-05-17 10:32:14 +00:00
let matches = match getopts::getopts(args.tail(), opts) {
2014-05-16 21:01:20 +00:00
Ok(m) => m,
2014-06-15 10:50:40 +00:00
Err(err) => fail!("{}", err),
2014-05-16 21:01:20 +00:00
};
if matches.opt_present("help") {
println!("{} {}", NAME, VERSION);
println!("");
println!("Usage:");
println!(" {} [OPTIONS] [FILE]...", NAME);
println!("");
2014-05-17 10:32:14 +00:00
print(getopts::usage("Print CRC and size for each file.", opts.as_slice()).as_slice());
return 0;
2014-05-16 21:01:20 +00:00
}
if matches.opt_present("version") {
println!("{} {}", NAME, VERSION);
return 0;
2014-05-16 21:01:20 +00:00
}
let files = matches.free;
if files.is_empty() {
match cksum("-") {
Ok((crc, size)) => println!("{} {}", crc, size),
Err(err) => {
2014-06-09 03:26:51 +00:00
show_error!("{}", err);
return 2;
}
2014-05-16 21:01:20 +00:00
}
return 0;
2014-05-16 21:01:20 +00:00
}
let mut exit_code = 0;
2014-05-16 21:01:20 +00:00
for fname in files.iter() {
2014-05-17 10:32:14 +00:00
match cksum(fname.as_slice()) {
2014-05-16 21:01:20 +00:00
Ok((crc, size)) => println!("{} {} {}", crc, size, fname),
Err(err) => {
2014-06-09 03:26:51 +00:00
show_error!("'{}' {}", fname, err);
exit_code = 2;
}
2014-05-16 21:01:20 +00:00
}
}
exit_code
2014-05-16 21:01:20 +00:00
}