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/cat.rs

99 lines
2.6 KiB
Rust

// Copyright (c) 2022 YAC
// SPDX-License-Identifier: AGPL-3.0-or-later
/* This file is part of YAC coreutils.
*
* YAC coreutils is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* YAC coreutils is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
use std::env;
use std::fs::{File, read_to_string};
use std::io;
use std::io::{ BufRead, Write };
use std::os::unix::io::FromRawFd;
use arg::Args;
use sysexits::ExitCode;
#[derive(Args, Debug)]
struct Arguments {
#[arg(short = "u")]
u: bool,
paths: Vec<String>,
}
fn out(argv0: &str, u: bool, val: &str) -> ExitCode {
if u {
let mut stdout = unsafe { File::from_raw_fd(1) };
match stdout.write_all(val.as_bytes()) {
Ok(_) => {},
Err(_) => {
eprintln!("{}: Cannot write to stdout", argv0);
return ExitCode::OsErr;
},
};
} else { print!("{}", val); }
ExitCode::Ok
}
fn main() -> ExitCode {
let mut arguments = env::args().collect::<Vec<String>>();
let argv0 = arguments.remove(0);
let mut args = Arguments::from_args(arguments
.iter()
.map(String::as_str)
.collect::<Vec<&str>>()
).unwrap();
let mut output = String::new();
if args.paths.is_empty() { args.paths.push("-".to_string()); }
for path in args.paths {
if path == "-" {
loop {
match io::stdin().lock().read_line(&mut output) {
Ok(EOF) => {
out(&argv0, args.u, &output);
output.clear();
if EOF == 0 { break }
},
Err(_) => {
eprintln!("Usage: {} [options...] [files...]", &argv0);
return ExitCode::Usage;
},
};
}
} else {
match read_to_string(&path) {
Ok(output) => { out(&argv0, args.u, &output); },
Err(_) => {
eprintln!("{}: {}: No such file or directory.", &argv0, &path);
return ExitCode::NoInput;
},
};
}
}
match io::stdout().flush() {
Ok(_) => return ExitCode::Ok,
Err(_) => {
eprintln!("{}: Cannot write to stdout", &argv0);
return ExitCode::OsErr;
},
};
}