mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-31 13:07:46 +00:00
new io cksum (includes BufReader fix)
closes kwantam/coreutils#1 via cherry-pick
This commit is contained in:
parent
d89fbedf12
commit
d8f58305d6
1 changed files with 16 additions and 15 deletions
|
@ -1,5 +1,5 @@
|
||||||
#![crate_name = "cksum"]
|
#![crate_name = "cksum"]
|
||||||
#![feature(collections, core, old_io, old_path, rustc_private)]
|
#![feature(rustc_private)]
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This file is part of the uutils coreutils package.
|
* This file is part of the uutils coreutils package.
|
||||||
|
@ -12,8 +12,9 @@
|
||||||
|
|
||||||
extern crate getopts;
|
extern crate getopts;
|
||||||
|
|
||||||
use std::old_io::{EndOfFile, File, IoError, IoResult, print};
|
use std::io::{self, stdin, Read, Write, BufReader};
|
||||||
use std::old_io::stdio::stdin_raw;
|
use std::path::Path;
|
||||||
|
use std::fs::File;
|
||||||
use std::mem;
|
use std::mem;
|
||||||
|
|
||||||
use crc_table::CRC_TABLE;
|
use crc_table::CRC_TABLE;
|
||||||
|
@ -43,20 +44,18 @@ fn crc_final(mut crc: u32, mut length: usize) -> u32 {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn cksum(fname: &str) -> IoResult<(u32, usize)> {
|
fn cksum(fname: &str) -> io::Result<(u32, usize)> {
|
||||||
let mut crc = 0u32;
|
let mut crc = 0u32;
|
||||||
let mut size = 0usize;
|
let mut size = 0usize;
|
||||||
|
|
||||||
let mut stdin_buf;
|
let file;
|
||||||
let mut file_buf;
|
let mut rd : Box<Read> = match fname {
|
||||||
let rd = match fname {
|
|
||||||
"-" => {
|
"-" => {
|
||||||
stdin_buf = stdin_raw();
|
Box::new(stdin())
|
||||||
&mut stdin_buf as &mut Reader
|
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
file_buf = try!(File::open(&Path::new(fname)));
|
file = try!(File::open(&Path::new(fname)));
|
||||||
&mut file_buf as &mut Reader
|
Box::new(BufReader::new(file))
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -64,12 +63,14 @@ fn cksum(fname: &str) -> IoResult<(u32, usize)> {
|
||||||
loop {
|
loop {
|
||||||
match rd.read(&mut bytes) {
|
match rd.read(&mut bytes) {
|
||||||
Ok(num_bytes) => {
|
Ok(num_bytes) => {
|
||||||
|
if num_bytes == 0 {
|
||||||
|
return Ok((crc_final(crc, size), size));
|
||||||
|
}
|
||||||
for &b in bytes[..num_bytes].iter() {
|
for &b in bytes[..num_bytes].iter() {
|
||||||
crc = crc_update(crc, b);
|
crc = crc_update(crc, b);
|
||||||
}
|
}
|
||||||
size += num_bytes;
|
size += num_bytes;
|
||||||
}
|
}
|
||||||
Err(IoError { kind: EndOfFile, .. }) => return Ok((crc_final(crc, size), size)),
|
|
||||||
Err(err) => return Err(err)
|
Err(err) => return Err(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -81,7 +82,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
getopts::optflag("V", "version", "output version information and exit"),
|
getopts::optflag("V", "version", "output version information and exit"),
|
||||||
];
|
];
|
||||||
|
|
||||||
let matches = match getopts::getopts(args.tail(), &opts) {
|
let matches = match getopts::getopts(&args[1..], &opts) {
|
||||||
Ok(m) => m,
|
Ok(m) => m,
|
||||||
Err(err) => panic!("{}", err),
|
Err(err) => panic!("{}", err),
|
||||||
};
|
};
|
||||||
|
@ -92,7 +93,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
println!("Usage:");
|
println!("Usage:");
|
||||||
println!(" {} [OPTIONS] [FILE]...", NAME);
|
println!(" {} [OPTIONS] [FILE]...", NAME);
|
||||||
println!("");
|
println!("");
|
||||||
print(getopts::usage("Print CRC and size for each file.", opts.as_slice()).as_slice());
|
println!("{}", getopts::usage("Print CRC and size for each file.", opts.as_ref()));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -116,7 +117,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
|
|
||||||
let mut exit_code = 0;
|
let mut exit_code = 0;
|
||||||
for fname in files.iter() {
|
for fname in files.iter() {
|
||||||
match cksum(fname.as_slice()) {
|
match cksum(fname.as_ref()) {
|
||||||
Ok((crc, size)) => println!("{} {} {}", crc, size, fname),
|
Ok((crc, size)) => println!("{} {} {}", crc, size, fname),
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
show_error!("'{}' {}", fname, err);
|
show_error!("'{}' {}", fname, err);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue