yac
/
coreutils
Archived
2
0
Fork 0
This repository has been archived on 2024-01-01. You can view files and clone it, but cannot push or open issues or pull requests.
coreutils/src/rs/cat.rs

67 lines
1.6 KiB
Rust

// Copyright (c) 2022 Emma Tebibyte
// SPDX-License-Identifier: AGPL-3.0-or-later
use std::fs::read_to_string;
use std::io;
use std::io::Write;
use std::env;
use std::process;
fn main() {
let mut args: Vec<String> = env::args().collect();
let argv0 = args.remove(0);
let mut opts = Vec::new();
for arg in &args {
if arg.starts_with("-") {
opts.push(arg);
}
}
if opts.is_empty() == false {
for option in opts {
match option.as_str() {
// Write bytes from the input file to the standard output without
// delay as each is read.
"-u" => {},
_ => {
println!("Usage: {} [options...] [files...]", argv0);
process::exit(64); // sysexits(3) EX_USAGE
},
};
}
}
else {
for path in args {
let mut val = String::new();
match path.as_str() {
"-" => {
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);
}
}
io::stdout().flush().unwrap();
}