diff --git a/Cargo.lock b/Cargo.lock index e5e0fd2..aa76782 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -34,6 +34,11 @@ name = "coreutils" version = "0.0.1" dependencies = [ "arg 0.1.0", +<<<<<<< HEAD +======= + "exit-no-std", + "libc", +>>>>>>> cc5a4cf (true(1) & false(1): EVEN SMALLER) "sysexits", ] diff --git a/Cargo.toml b/Cargo.toml index 8d5deba..7fc48d8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ authors = [ ] [dependencies] +libc = { version = "0.2.14", default-features = false } sysexits = "0.3.4" arg = { version = "0.1.0", path = "arg", package = "arg" } diff --git a/src/cat.rs b/src/cat.rs index 057b511..cb81066 100644 --- a/src/cat.rs +++ b/src/cat.rs @@ -18,9 +18,10 @@ */ use std::env; -use std::fs::{File, read_to_string}; +use std::fs::{File, read, read_to_string}; use std::io; use std::io::{ Read, Write }; +use std::os::unix::io::FromRawFd; use std::str; use arg::Args; @@ -52,17 +53,11 @@ fn main() -> ExitCode { for path in args.paths { if path == "-" { let mut bytes: Vec = Vec::new(); - match io::stdin().lock().read_to_end(&mut bytes) { - Ok(EOF) => { - for byte in &bytes { - output.push_str(str::from_utf8(&[byte.to_owned()]).unwrap()); - } - }, - Err(_) => { - eprintln!("Usage: {} [options...] [files...]", &argv0); - return ExitCode::Usage; - }, - }; + io::stdin().lock().read_to_end(&mut bytes).unwrap(); + + for byte in &bytes { + output.push_str(str::from_utf8(&[byte.to_owned()]).unwrap()); + } } else { match read_to_string(&path) { Ok(contents) => { output.push_str(&contents) }, @@ -75,11 +70,5 @@ fn main() -> ExitCode { print!("{}", output); output.clear(); } - match io::stdout().flush() { - Ok(_) => return ExitCode::Ok, - Err(_) => { - eprintln!("{}: Cannot write to stdout", &argv0); - return ExitCode::OsErr; - }, - }; + ExitCode::Ok } diff --git a/src/false.rs b/src/false.rs new file mode 100644 index 0000000..4b01be0 --- /dev/null +++ b/src/false.rs @@ -0,0 +1,35 @@ +// 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/. + */ + +#![feature(core_intrinsics, lang_items, start)] +#![no_main] +#![no_std] + +extern crate libc; + +#[lang = "eh_personality"] +#[no_mangle] +fn rust_eh_personality() {} + +#[lang = "panic_impl"] +#[no_mangle] +fn panic(_: &core::panic::PanicInfo) -> ! { core::intrinsics::abort() } + +#[no_mangle] +fn main() { exit_no_std::exit(1); } diff --git a/src/true.rs b/src/true.rs new file mode 100644 index 0000000..8b748e6 --- /dev/null +++ b/src/true.rs @@ -0,0 +1,35 @@ +// 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/. + */ + +#![feature(core_intrinsics, lang_items, start)] +#![no_main] +#![no_std] + +extern crate libc; + +#[lang = "eh_personality"] +#[no_mangle] +fn rust_eh_personality() {} + +#[lang = "panic_impl"] +#[no_mangle] +fn panic(_: &core::panic::PanicInfo) -> ! { core::intrinsics::abort() } + +#[no_mangle] +fn main() { exit_no_std::exit(0); }