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

75 lines
2.0 KiB
Rust
Raw Normal View History

2022-12-12 03:53:04 +00:00
// Copyright (c) 2022 YAC
2022-12-12 00:25:52 +00:00
// SPDX-License-Identifier: AGPL-3.0-or-later
2022-12-12 03:53:04 +00:00
/* 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/.
*/
2022-12-12 00:25:52 +00:00
use std::env;
2022-12-14 22:56:59 +00:00
use std::fs::{File, read, read_to_string};
2022-12-12 00:25:52 +00:00
use std::io;
2022-12-14 03:43:40 +00:00
use std::io::{ Read, Write };
2022-12-14 22:56:59 +00:00
use std::os::unix::io::FromRawFd;
2022-12-14 03:43:40 +00:00
use std::str;
2022-12-12 00:25:52 +00:00
use arg::Args;
use sysexits::ExitCode;
#[derive(Args, Debug)]
struct Arguments {
#[arg(short = "u")]
u: bool,
paths: Vec<String>,
}
fn main() -> ExitCode {
let mut arguments = env::args().collect::<Vec<String>>();
let argv0 = arguments.remove(0);
2022-12-12 21:49:51 +00:00
let mut args = Arguments::from_args(arguments
2022-12-12 00:25:52 +00:00
.iter()
.map(String::as_str)
.collect::<Vec<&str>>()
).unwrap();
let mut output = String::new();
2022-12-12 21:49:51 +00:00
if args.paths.is_empty() { args.paths.push("-".to_string()); }
for path in args.paths {
if path == "-" {
2022-12-14 03:43:40 +00:00
let mut bytes: Vec<u8> = Vec::new();
2022-12-14 22:56:59 +00:00
io::stdin().lock().read_to_end(&mut bytes).unwrap();
for byte in &bytes {
output.push_str(str::from_utf8(&[byte.to_owned()]).unwrap());
}
2022-12-12 21:49:51 +00:00
} else {
2022-12-12 00:25:52 +00:00
match read_to_string(&path) {
Ok(contents) => { output.push_str(&contents) },
2022-12-12 00:25:52 +00:00
Err(_) => {
2022-12-12 21:49:51 +00:00
eprintln!("{}: {}: No such file or directory.", &argv0, &path);
2022-12-12 00:25:52 +00:00
return ExitCode::NoInput;
},
};
}
2022-12-12 23:55:15 +00:00
print!("{}", output);
output.clear();
2022-12-12 00:25:52 +00:00
}
2022-12-14 22:56:59 +00:00
ExitCode::Ok
2022-12-12 00:25:52 +00:00
}