Merge pull request #1303 from rinconjc/1278

tee: handle '-' as filename (POSIX spec) fix #1278
This commit is contained in:
cnd 2018-10-19 10:52:40 +04:00 committed by GitHub
commit a673c12694
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

13
src/tee/tee.rs Normal file → Executable file
View file

@ -50,7 +50,7 @@ fn options(args: &[String]) -> Result<Options> {
let version = format!("{} {}", NAME, VERSION);
let arguments = "[OPTION]... [FILE]...";
let brief = "Copy standard input to each FILE, and also to standard output.";
let comment = "If a FILE is -, copy again to standard output.";
let comment = "If a FILE is -, it refers to a file named - .";
let help = format!(
"{}\n\nUsage:\n {} {}\n\n{}\n{}",
version,
@ -59,8 +59,7 @@ fn options(args: &[String]) -> Result<Options> {
opts.usage(brief),
comment
);
let mut names: Vec<String> = m.free.clone().into_iter().collect();
names.push("-".to_owned());
let names: Vec<String> = m.free.clone().into_iter().collect();
let to_print = if m.opt_present("help") {
Some(help)
} else if m.opt_present("version") {
@ -87,12 +86,13 @@ fn exec(options: Options) -> Result<()> {
}
fn tee(options: Options) -> Result<()> {
let writers: Vec<Box<Write>> = options
let mut writers: Vec<Box<Write>> = options
.files
.clone()
.into_iter()
.map(|file| open(file, options.append))
.collect();
writers.push(Box::new(stdout()));
let output = &mut MultiWriter { writers: writers };
let input = &mut NamedReader {
inner: Box::new(stdin()) as Box<Read>,
@ -105,11 +105,8 @@ fn tee(options: Options) -> Result<()> {
}
fn open(name: String, append: bool) -> Box<Write> {
let is_stdout = name == "-";
let path = PathBuf::from(name);
let inner: Box<Write> = if is_stdout {
Box::new(stdout())
} else {
let inner: Box<Write> = {
let mut options = OpenOptions::new();
let mode = if append {
options.append(true)