added stdin support to cat
This commit is contained in:
parent
680887069f
commit
5b47345bf5
@ -2,7 +2,8 @@
|
|||||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
use std::fs::read_to_string;
|
use std::fs::read_to_string;
|
||||||
use std::io::{stdin, stdout, Write};
|
use std::io;
|
||||||
|
use std::io::Write;
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::process;
|
use std::process;
|
||||||
|
|
||||||
@ -25,7 +26,7 @@ fn main() {
|
|||||||
"-u" => {},
|
"-u" => {},
|
||||||
|
|
||||||
_ => {
|
_ => {
|
||||||
println! ("Usage: {} [options...] [files...]", argv0);
|
println!("Usage: {} [options...] [files...]", argv0);
|
||||||
process::exit(64); // sysexits(3) EX_USAGE
|
process::exit(64); // sysexits(3) EX_USAGE
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@ -33,15 +34,33 @@ fn main() {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
for path in args {
|
for path in args {
|
||||||
match read_to_string(&path) {
|
let mut val = String::new();
|
||||||
Ok(val) => println!("{}", val),
|
|
||||||
Err(_) => {
|
match path.as_str() {
|
||||||
println!("{}: {}: No such file or directory.", argv0, path);
|
"-" => {
|
||||||
process::exit(66); // sysexits(3) EX_NOINPUT
|
let mut content = String::new();
|
||||||
|
match io::stdin().read_line(&mut content) {
|
||||||
|
Ok(_) => { val.push_str(&content); },
|
||||||
|
Err(_) => {
|
||||||
|
println!("Usage: {} [options...] [files...]", argv0);
|
||||||
|
process::exit(64); // sysexits(3) EX_USAGE
|
||||||
|
},
|
||||||
|
};
|
||||||
},
|
},
|
||||||
};
|
|
||||||
|
_ => {
|
||||||
|
match read_to_string(&path) {
|
||||||
|
Ok(output) => { val.push_str(&output); },
|
||||||
|
Err(_) => {
|
||||||
|
println!("{}: {}: No such file or directory.", argv0, path);
|
||||||
|
process::exit(66); // sysexits(3) EX_NOINPUT
|
||||||
|
},
|
||||||
|
};
|
||||||
|
},
|
||||||
|
}
|
||||||
|
print!("{}", val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stdout().flush().unwrap();
|
io::stdout().flush().unwrap();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user