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:
commit
78ed54e0b5
4 changed files with 53 additions and 16 deletions
18
fold/fold.rs
18
fold/fold.rs
|
@ -71,7 +71,12 @@ fn main() {
|
|||
},
|
||||
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) {
|
||||
if filenames.len() == 0 {
|
||||
fold_file(io::stdin(), bytes, spaces, width);
|
||||
} else {
|
||||
for filename in filenames.iter() {
|
||||
let filename: &str = *filename;
|
||||
fold_file(BufferedReader::new(safe_unwrap!(File::open(&Path::new(filename)))), bytes, spaces, width);
|
||||
let buffer = BufferedReader::new(
|
||||
if filename == "-".to_owned() {
|
||||
~io::stdio::stdin_raw() as ~Reader
|
||||
} else {
|
||||
~safe_unwrap!(File::open(&Path::new(filename))) as ~Reader
|
||||
}
|
||||
);
|
||||
fold_file(buffer, bytes, spaces, width);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -16,6 +16,7 @@ extern crate getopts;
|
|||
extern crate libc;
|
||||
|
||||
use std::io::fs::File;
|
||||
use std::io::stdio::stdin_raw;
|
||||
use std::io::BufferedReader;
|
||||
use std::os;
|
||||
use crypto::digest::Digest;
|
||||
|
@ -66,7 +67,12 @@ fn main() {
|
|||
let quiet = matches.opt_present("quiet") || status;
|
||||
let strict = matches.opt_present("strict");
|
||||
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;
|
||||
for filename in files.iter() {
|
||||
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 {
|
||||
let mut buffer = BufferedReader::new(file);
|
||||
let mut buffer = file;
|
||||
//let mut buffer = BufferedReader::new(file);
|
||||
for (i, line) in buffer.lines().enumerate() {
|
||||
let line = safe_unwrap!(line);
|
||||
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 =
|
||||
if binary {
|
||||
(safe_unwrap!(file.read_to_end())).as_slice().to_owned()
|
||||
|
|
|
@ -57,8 +57,14 @@ fn main() {
|
|||
}
|
||||
|
||||
fn paste(filenames: Vec<~str>, serial: bool, delimiters: ~str) {
|
||||
let mut files: ~[io::BufferedReader<io::File>] = filenames.move_iter().map(|name|
|
||||
io::BufferedReader::new(crash_if_err!(1, io::File::open(&Path::new(name))))
|
||||
let mut files: ~[io::BufferedReader<~Reader>] = filenames.move_iter().map(|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();
|
||||
let delimiters: ~[~str] = delimiters.chars().map(|x| x.to_str()).collect();
|
||||
let mut delim_count = 0;
|
||||
|
|
14
tac/tac.rs
14
tac/tac.rs
|
@ -60,14 +60,24 @@ fn main() {
|
|||
}
|
||||
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) {
|
||||
for filename in filenames.move_iter() {
|
||||
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());
|
||||
if data.ends_with("\n") {
|
||||
// removes blank line that is inserted otherwise
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue