1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 03:27:44 +00:00

tee: handle '-' as filename (POSIX spec) fix #1278

This commit is contained in:
Julio Rincon 2018-10-16 22:10:53 +11:00
parent fe21c4b7fd
commit 4ac2de925d

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