1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 19:47:45 +00:00

Merge pull request #163 from Arcterus/master

Add stdin (-) support to some programs
This commit is contained in:
Heather 2014-05-07 19:21:56 -07:00
commit 78ed54e0b5
4 changed files with 53 additions and 16 deletions

View file

@ -71,7 +71,12 @@ fn main() {
}, },
None => 80 None => 80
}; };
fold(matches.free, bytes, spaces, width); let files = if matches.free.is_empty() {
vec!("-".to_owned())
} else {
matches.free
};
fold(files, bytes, spaces, width);
} }
} }
@ -89,13 +94,16 @@ fn handle_obsolete(args: ~[~str]) -> (~[~str], Option<~str>) {
} }
fn fold(filenames: Vec<~str>, bytes: bool, spaces: bool, width: uint) { fn fold(filenames: Vec<~str>, bytes: bool, spaces: bool, width: uint) {
if filenames.len() == 0 { for filename in filenames.iter() {
fold_file(io::stdin(), bytes, spaces, width); let filename: &str = *filename;
} else { let buffer = BufferedReader::new(
for filename in filenames.iter() { if filename == "-".to_owned() {
let filename: &str = *filename; ~io::stdio::stdin_raw() as ~Reader
fold_file(BufferedReader::new(safe_unwrap!(File::open(&Path::new(filename)))), bytes, spaces, width); } else {
} ~safe_unwrap!(File::open(&Path::new(filename))) as ~Reader
}
);
fold_file(buffer, bytes, spaces, width);
} }
} }

View file

@ -16,6 +16,7 @@ extern crate getopts;
extern crate libc; extern crate libc;
use std::io::fs::File; use std::io::fs::File;
use std::io::stdio::stdin_raw;
use std::io::BufferedReader; use std::io::BufferedReader;
use std::os; use std::os;
use crypto::digest::Digest; use crypto::digest::Digest;
@ -66,7 +67,12 @@ fn main() {
let quiet = matches.opt_present("quiet") || status; let quiet = matches.opt_present("quiet") || status;
let strict = matches.opt_present("strict"); let strict = matches.opt_present("strict");
let warn = matches.opt_present("warn") && !status; let warn = matches.opt_present("warn") && !status;
md5sum(matches.free, binary, check, tag, status, quiet, strict, warn); let files = if matches.free.is_empty() {
vec!("-".to_owned())
} else {
matches.free
};
md5sum(files, binary, check, tag, status, quiet, strict, warn);
} }
} }
@ -77,9 +83,16 @@ fn md5sum(files: Vec<~str>, binary: bool, check: bool, tag: bool, status: bool,
let mut failed = 0; let mut failed = 0;
for filename in files.iter() { for filename in files.iter() {
let filename: &str = *filename; let filename: &str = *filename;
let mut file = safe_unwrap!(File::open(&Path::new(filename))); let mut file = BufferedReader::new(
if filename == "-".to_owned() {
~stdin_raw() as ~Reader
} else {
~safe_unwrap!(File::open(&Path::new(filename))) as ~Reader
}
);
if check { if check {
let mut buffer = BufferedReader::new(file); let mut buffer = file;
//let mut buffer = BufferedReader::new(file);
for (i, line) in buffer.lines().enumerate() { for (i, line) in buffer.lines().enumerate() {
let line = safe_unwrap!(line); let line = safe_unwrap!(line);
let (ck_filename, sum) = match from_gnu(line, bytes) { let (ck_filename, sum) = match from_gnu(line, bytes) {
@ -132,7 +145,7 @@ fn md5sum(files: Vec<~str>, binary: bool, check: bool, tag: bool, status: bool,
} }
} }
fn calc_sum(md5: &mut crypto::md5::Md5, file: &mut File, binary: bool) -> ~str { fn calc_sum(md5: &mut crypto::md5::Md5, file: &mut Reader, binary: bool) -> ~str {
let data = let data =
if binary { if binary {
(safe_unwrap!(file.read_to_end())).as_slice().to_owned() (safe_unwrap!(file.read_to_end())).as_slice().to_owned()

View file

@ -57,8 +57,14 @@ fn main() {
} }
fn paste(filenames: Vec<~str>, serial: bool, delimiters: ~str) { fn paste(filenames: Vec<~str>, serial: bool, delimiters: ~str) {
let mut files: ~[io::BufferedReader<io::File>] = filenames.move_iter().map(|name| let mut files: ~[io::BufferedReader<~Reader>] = filenames.move_iter().map(|name|
io::BufferedReader::new(crash_if_err!(1, io::File::open(&Path::new(name)))) io::BufferedReader::new(
if name == "-".to_owned() {
~io::stdio::stdin_raw() as ~Reader
} else {
~crash_if_err!(1, io::File::open(&Path::new(name))) as ~Reader
}
)
).collect(); ).collect();
let delimiters: ~[~str] = delimiters.chars().map(|x| x.to_str()).collect(); let delimiters: ~[~str] = delimiters.chars().map(|x| x.to_str()).collect();
let mut delim_count = 0; let mut delim_count = 0;

View file

@ -60,14 +60,24 @@ fn main() {
} }
None => "\n".to_owned() None => "\n".to_owned()
}; };
tac(matches.free, before, regex, separator); let files = if matches.free.is_empty() {
vec!("-".to_owned())
} else {
matches.free
};
tac(files, before, regex, separator);
} }
} }
fn tac(filenames: Vec<~str>, before: bool, _: bool, separator: ~str) { fn tac(filenames: Vec<~str>, before: bool, _: bool, separator: ~str) {
for filename in filenames.move_iter() { for filename in filenames.move_iter() {
let mut file = io::BufferedReader::new( let mut file = io::BufferedReader::new(
crash_if_err!(1, io::File::open(&Path::new(filename)))); if filename == "-".to_owned() {
~io::stdio::stdin_raw() as ~Reader
} else {
~crash_if_err!(1, io::File::open(&Path::new(filename))) as ~Reader
}
);
let mut data = crash_if_err!(1, file.read_to_str()); let mut data = crash_if_err!(1, file.read_to_str());
if data.ends_with("\n") { if data.ends_with("\n") {
// removes blank line that is inserted otherwise // removes blank line that is inserted otherwise