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
Raw Normal View History

2022-12-05 01:44:55 +00:00
// Copyright (c) 2022 Emma Tebibyte
// SPDX-License-Identifier: AGPL-3.0-or-later
use std::fs::read_to_string;
2022-12-06 01:59:40 +00:00
use std::io;
use std::io::Write;
2022-12-05 01:44:55 +00:00
use std::env;
use std::process;
fn main() {
2022-12-06 00:04:28 +00:00
let mut args: Vec<String> = env::args().collect();
let argv0 = args.remove(0);
2022-12-05 05:48:19 +00:00
let mut opts = Vec::new();
for arg in &args {
if arg.starts_with("-") {
opts.push(arg);
}
}
2022-12-05 01:44:55 +00:00
2022-12-05 05:48:19 +00:00
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" => {},
2022-12-05 01:44:55 +00:00
2022-12-06 00:04:28 +00:00
_ => {
2022-12-06 01:59:40 +00:00
println!("Usage: {} [options...] [files...]", argv0);
2022-12-06 00:04:28 +00:00
process::exit(64); // sysexits(3) EX_USAGE
},
2022-12-05 05:48:19 +00:00
};
}
}
else {
for path in args {
2022-12-06 01:59:40 +00:00
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
},
};
2022-12-06 00:04:28 +00:00
},
2022-12-06 01:59:40 +00:00
_ => {
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);
2022-12-05 05:48:19 +00:00
}
2022-12-05 01:44:55 +00:00
}
2022-12-05 05:48:19 +00:00
2022-12-06 01:59:40 +00:00
io::stdout().flush().unwrap();
2022-12-05 01:44:55 +00:00
}