coreutils/src/sum/sum.rs

140 lines
3.5 KiB
Rust
Raw Normal View History

2014-07-06 08:13:36 +00:00
#![crate_name = "sum"]
2015-05-06 18:12:30 +00:00
#![feature(rustc_private)]
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;
2015-05-06 18:12:30 +00:00
use std::fs::File;
use std::io::{Read, Result, stdin, Write};
use std::path::Path;
2014-05-28 00:57:33 +00:00
#[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-05-06 18:12:30 +00:00
fn bsd_sum(mut reader: Box<Read>) -> (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[..n].iter() {
2014-05-28 00:57:33 +00:00
checksum = (checksum >> 1) + ((checksum & 1) << 15);
checksum += byte as u16;
}
},
_ => break,
}
}
(blocks_read, checksum)
}
2015-05-06 18:12:30 +00:00
fn sysv_sum(mut reader: Box<Read>) -> (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[..n].iter() {
2014-05-28 00:57:33 +00:00
ret += byte as u32;
}
},
_ => break,
}
}
ret = (ret & 0xffff) + (ret >> 16);
ret = (ret & 0xffff) + (ret >> 16);
(blocks_read, ret as u16)
}
2015-05-06 18:12:30 +00:00
fn open(name: &str) -> Result<Box<Read>> {
2014-10-05 01:09:52 +00:00
match name {
2015-05-06 18:12:30 +00:00
"-" => Ok(Box::new(stdin()) as Box<Read>),
2014-10-05 01:09:52 +00:00
_ => {
let f = try!(File::open(&Path::new(name)));
2015-05-06 18:12:30 +00:00
Ok(Box::new(f) as Box<Read>)
2014-10-05 01:09:52 +00:00
}
}
2014-05-28 00:57:33 +00:00
}
pub fn uumain(args: Vec<String>) -> i32 {
2015-05-06 18:12:30 +00:00
let program = &args[0];
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"),
];
2015-05-06 18:12:30 +00:00
let matches = match getopts::getopts(&args[1..], &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!("");
2015-05-06 18:12:30 +00:00
print!("{}", getopts::usage("checksum and count the blocks in a file", &opts));
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() {
2015-05-06 18:12:30 +00:00
let reader = match open(file) {
2014-06-18 10:36:12 +00:00
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
}