coreutils/src/sum/sum.rs

139 lines
3.5 KiB
Rust
Raw Normal View History

2014-07-06 08:13:36 +00:00
#![crate_name = "sum"]
2015-01-10 19:31:55 +00:00
#![allow(unstable)]
2014-05-28 00:57:33 +00:00
/*
* This file is part of the uutils coreutils package.
*
* (c) T. Jameson Little <t.jameson.little@gmail.com>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/
extern crate getopts;
extern crate libc;
use std::io::{File, IoResult, print};
use std::io::stdio::{stdin_raw};
#[path="../common/util.rs"]
2015-01-08 12:54:22 +00:00
#[macro_use]
2014-05-28 00:57:33 +00:00
mod util;
static VERSION: &'static str = "1.0.0";
static NAME: &'static str = "sum";
2015-01-10 12:49:18 +00:00
fn bsd_sum(mut reader: Box<Reader>) -> (usize, u16) {
2015-01-08 12:58:23 +00:00
let mut buf = [0; 1024];
2014-05-28 00:57:33 +00:00
let mut blocks_read = 0;
let mut checksum: u16 = 0;
loop {
2014-11-19 20:55:25 +00:00
match reader.read(&mut buf) {
2014-05-28 00:57:33 +00:00
Ok(n) if n != 0 => {
blocks_read += 1;
for &byte in buf.slice_to(n).iter() {
checksum = (checksum >> 1) + ((checksum & 1) << 15);
checksum += byte as u16;
}
},
_ => break,
}
}
(blocks_read, checksum)
}
2015-01-10 12:49:18 +00:00
fn sysv_sum(mut reader: Box<Reader>) -> (usize, u16) {
2015-01-08 12:58:23 +00:00
let mut buf = [0; 512];
2014-05-28 00:57:33 +00:00
let mut blocks_read = 0;
let mut ret = 0;
loop {
2014-11-19 20:55:25 +00:00
match reader.read(&mut buf) {
2014-05-28 00:57:33 +00:00
Ok(n) if n != 0 => {
blocks_read += 1;
for &byte in buf.slice_to(n).iter() {
ret += byte as u32;
}
},
_ => break,
}
}
ret = (ret & 0xffff) + (ret >> 16);
ret = (ret & 0xffff) + (ret >> 16);
(blocks_read, ret as u16)
}
fn open(name: &str) -> IoResult<Box<Reader>> {
2014-10-05 01:09:52 +00:00
match name {
2015-01-10 12:49:18 +00:00
"-" => Ok(Box::new(stdin_raw()) as Box<Reader>),
2014-10-05 01:09:52 +00:00
_ => {
let f = try!(File::open(&Path::new(name)));
2015-01-10 12:49:18 +00:00
Ok(Box::new(f) as Box<Reader>)
2014-10-05 01:09:52 +00:00
}
}
2014-05-28 00:57:33 +00:00
}
2015-01-10 12:49:18 +00:00
pub fn uumain(args: Vec<String>) -> isize {
2014-07-20 01:13:55 +00:00
let program = args[0].as_slice();
2014-05-28 00:57:33 +00:00
let opts = [
getopts::optflag("r", "", "use the BSD compatible algorithm (default)"),
getopts::optflag("s", "sysv", "use System V compatible algorithm"),
getopts::optflag("h", "help", "show this help message"),
getopts::optflag("v", "version", "print the version and exit"),
];
2014-11-19 20:55:25 +00:00
let matches = match getopts::getopts(args.tail(), &opts) {
2014-05-28 00:57:33 +00:00
Ok(m) => m,
2014-06-15 10:50:40 +00:00
Err(f) => crash!(1, "Invalid options\n{}", f)
2014-05-28 00:57:33 +00:00
};
if matches.opt_present("help") {
println!("{} {}", program, VERSION);
println!("");
println!("Usage:");
2014-11-21 09:09:43 +00:00
println!(" {0} [OPTION]... [FILE]...", program);
2014-05-28 00:57:33 +00:00
println!("");
2014-11-19 20:55:25 +00:00
print(getopts::usage("checksum and count the blocks in a file", &opts).as_slice());
2014-05-28 00:57:33 +00:00
println!("");
println!("With no FILE, or when FILE is -, read standard input.");
return 0;
2014-05-28 00:57:33 +00:00
}
if matches.opt_present("version") {
println!("{} {}", program, VERSION);
return 0;
2014-05-28 00:57:33 +00:00
}
let sysv = matches.opt_present("sysv");
2014-06-18 10:36:12 +00:00
let files = if matches.free.is_empty() {
vec!["-".to_string()]
2014-05-28 00:57:33 +00:00
} else {
2014-06-18 10:36:12 +00:00
matches.free
2014-05-28 00:57:33 +00:00
};
2014-06-18 10:38:19 +00:00
let print_names = sysv || files.len() > 1;
2014-06-18 10:36:12 +00:00
for file in files.iter() {
let reader = match open(file.as_slice()) {
Ok(f) => f,
_ => crash!(1, "unable to open file")
};
let (blocks, sum) = if sysv {
sysv_sum(reader)
} else {
bsd_sum(reader)
};
2014-06-18 10:38:19 +00:00
if print_names {
println!("{} {} {}", sum, blocks, file);
} else {
println!("{} {}", sum, blocks);
}
2014-06-18 10:36:12 +00:00
}
0
2014-05-28 00:57:33 +00:00
}