Add not builtin command - cat

This commit is contained in:
g1n 2021-07-22 07:35:13 +00:00
parent b77a44a031
commit d605d57de6
1 changed files with 29 additions and 0 deletions

29
src/cat.rs Normal file
View File

@ -0,0 +1,29 @@
use std::env;
use std::fs::File;
use std::path::Path;
use std::io::BufReader;
use std::io::stdout;
use std::io::prelude::*;
fn main() -> std::io::Result<()> {
let args: Vec<String> = env::args().collect();
for arg in 1..args.len() {
match args[arg].as_str() {
"--help" => {
println!("Usage: cat [OPTIONS] FILE");
println!("Reads a file");
},
arg => {
if Path::new(arg).exists() {
let file = File::open(arg)?;
let mut buf_reader = BufReader::new(file);
let mut contents = String::new();
buf_reader.read_to_string(&mut contents)?;
print!("{}", contents);
stdout().flush().unwrap();
}
}
}
}
Ok(())
}