use std::{env, fs};
use std::io::{stdin, Error, Write, Read};
pub mod ls;
pub mod cd;
pub mod find;
pub mod grep;
pub fn parse_command_line_args(command_line: &str) -> Vec<String> {
let mut args = Vec::new();
let mut current_arg = String::new();
let mut in_quotes = false;
for c in command_line.chars() {
if c == '"' {
in_quotes = !in_quotes;
if !current_arg.is_empty() {
args.push(current_arg.clone());
current_arg.clear();
}
} else if c.is_whitespace() && !in_quotes {
if !current_arg.is_empty() {
args.push(current_arg.clone());
current_arg.clear();
}
} else {
current_arg.push(c);
}
}
if !current_arg.is_empty() {
args.push(current_arg);
}
args
}
pub fn working_directory() -> Result<String, Error> {
let path = env::current_dir();
let cwd = path?;
Ok(cwd.display().to_string())
}
pub fn cat(file_path: &str, handle: &mut Box<dyn Write>) -> Result<(), Box<dyn std::error::Error>> {
let mut buffer = Vec::new();
if file_path.is_empty() {
let mut stdin = stdin();
match stdin.read_to_end(&mut buffer) {
Ok(0) => {} Ok(_) => {
}
Err(e) => {
eprintln!("Error reading from stdin: {}", e);
}
}
} else {
buffer = fs::read(file_path)?;
};
handle.write_all(&buffer)?;
Ok(())
}