feat: adjust cat to be more busybox-like, add mkdir

This commit is contained in:
user0-07161 2025-03-20 20:18:10 +01:00 committed by teesh3rt
parent 20c093e5a4
commit 981ec1443e
4 changed files with 55 additions and 5 deletions

View file

@ -10,11 +10,15 @@ impl Command for Cat {
fn execute(&self) {
let args: Vec<String> = env::args().collect::<Vec<_>>().clone();
let arguments: Vec<String> = get_args(String::from("cat"), args);
let filename = &arguments[0];
let mut read = BufReader::new(File::open(filename).unwrap());
let mut vecbuf = Vec::new();
let _ = read.read_to_end(&mut vecbuf);
for file in arguments.iter() {
let mut tmpbuf = Vec::new();
let mut read = BufReader::new(File::open(file).unwrap());
let _ = read.read_to_end(&mut tmpbuf);
let _ = vecbuf.append(&mut tmpbuf);
}
let _ = io::stdout().write_all(&vecbuf);
}
}

View file

@ -0,0 +1,42 @@
use boxutils::commands::Command;
use boxutils::commands::get_args;
use std::fs;
use std::env;
pub struct Mkdir;
impl Command for Mkdir {
fn execute(&self) {
let args: Vec<String> = env::args().collect::<Vec<_>>().clone();
let arguments: Vec<String> = get_args(String::from("mkdir"), args.clone());
if arguments.len() == 0 {
panic!(
"{}",
String::from(
"Usage: mkdir [DIR1] [DIR1] etc. pp. [-p, --parents]"
)
);
}
for arg in arguments.iter() {
if arg == "--help" {
println!(
"{}",
String::from(
"Usage: mkdir [DIR1] [DIR2] etc. pp. [-p, --parents]"
));
return;
}
}
for arg in arguments.iter() {
if (arg != "-p") & (arg != "--parent") {
if args.contains(&String::from("-p")) || args.contains(&String::from("--parent")) {
let _ = fs::create_dir_all(String::from(arg));
} else {
fs::create_dir(String::from(arg)).unwrap();
}
}
}
}
}

View file

@ -6,3 +6,6 @@ pub use cat::Cat;
mod echo;
pub use echo::Echo;
mod mkdir;
pub use mkdir::Mkdir;

View file

@ -7,6 +7,7 @@ pub fn get_registry() -> CommandRegistry {
registry.register("hello", Box::new(coreutils::commands::Hello));
registry.register("cat", Box::new(coreutils::commands::Cat));
registry.register("echo", Box::new(coreutils::commands::Echo));
registry.register("mkdir", Box::new(coreutils::commands::Mkdir));
registry.register("box", Box::new(Boxcmd));
registry