68 lines
1.5 KiB
Rust
68 lines
1.5 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 sysexits::ExitCode;
|
|
|
|
fn main() -> ExitCode {
|
|
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);
|
|
return ExitCode::Usage;
|
|
},
|
|
};
|
|
}
|
|
}
|
|
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);
|
|
return ExitCode::Usage;
|
|
},
|
|
};
|
|
},
|
|
|
|
_ => {
|
|
match read_to_string(&path) {
|
|
Ok(output) => { val.push_str(&output); },
|
|
Err(_) => {
|
|
println!("{}: {}: No such file or directory.", argv0, path);
|
|
return ExitCode::NoInput;
|
|
},
|
|
};
|
|
},
|
|
}
|
|
print!("{}", val);
|
|
}
|
|
|
|
io::stdout().flush().unwrap();
|
|
ExitCode::Ok
|
|
}
|