use crate::io;
use crate::sys;
use crate::applets::get_arg;
pub fn cat(argc: i32, argv: *const *const u8) -> i32 {
if argc < 2 {
let mut buf = [0u8; 4096];
loop {
let n = io::read(0, &mut buf);
if n <= 0 { break; }
io::write_all(1, &buf[..n as usize]);
}
return 0;
}
let mut exit_code = 0;
for i in 1..argc {
let path = match unsafe { get_arg(argv, i) } {
Some(p) => p,
None => continue,
};
if path == b"-u" {
continue;
}
if path == b"-" {
let mut buf = [0u8; 4096];
loop {
let n = io::read(0, &mut buf);
if n <= 0 { break; }
io::write_all(1, &buf[..n as usize]);
}
continue;
}
let fd = io::open(path, libc::O_RDONLY, 0);
if fd < 0 {
sys::perror(path);
exit_code = 1;
continue;
}
let mut buf = [0u8; 4096];
loop {
let n = io::read(fd, &mut buf);
if n <= 0 { break; }
io::write_all(1, &buf[..n as usize]);
}
io::close(fd);
}
exit_code
}
#[cfg(test)]
mod tests {
extern crate std;
use std::sync::atomic::{AtomicUsize, Ordering};
static TEST_COUNTER: AtomicUsize = AtomicUsize::new(0);
use std::process::{Command, Stdio};
use std::io::Write;
use std::fs;
use std::path::PathBuf;
fn get_armybox_path() -> PathBuf {
if let Ok(path) = std::env::var("ARMYBOX_PATH") {
return PathBuf::from(path);
}
let release = PathBuf::from("target/release/armybox");
if release.exists() {
return release;
}
PathBuf::from("target/debug/armybox")
}
fn setup() -> PathBuf {
let counter = TEST_COUNTER.fetch_add(1, Ordering::SeqCst);
let dir = std::env::temp_dir().join(format!("armybox_cat_test_{}_{}", std::process::id(), counter));
let _ = fs::create_dir_all(&dir);
fs::write(dir.join("file1.txt"), "hello\n").unwrap();
fs::write(dir.join("file2.txt"), "world\n").unwrap();
dir
}
fn cleanup(dir: &std::path::Path) {
let _ = fs::remove_dir_all(dir);
}
#[test]
fn test_cat_single_file() {
let armybox = get_armybox_path();
if !armybox.exists() {
eprintln!("Skipping test: armybox binary not found at {:?}", armybox);
return;
}
let dir = setup();
let output = Command::new(&armybox)
.args(["cat", dir.join("file1.txt").to_str().unwrap()])
.output()
.unwrap();
assert_eq!(output.status.code(), Some(0));
assert_eq!(std::string::String::from_utf8_lossy(&output.stdout), "hello\n");
cleanup(&dir);
}
#[test]
fn test_cat_multiple_files() {
let armybox = get_armybox_path();
if !armybox.exists() {
eprintln!("Skipping test: armybox binary not found at {:?}", armybox);
return;
}
let dir = setup();
let output = Command::new(&armybox)
.args([
"cat",
dir.join("file1.txt").to_str().unwrap(),
dir.join("file2.txt").to_str().unwrap(),
])
.output()
.unwrap();
assert_eq!(output.status.code(), Some(0));
assert_eq!(std::string::String::from_utf8_lossy(&output.stdout), "hello\nworld\n");
cleanup(&dir);
}
#[test]
fn test_cat_nonexistent_file() {
let armybox = get_armybox_path();
if !armybox.exists() {
eprintln!("Skipping test: armybox binary not found at {:?}", armybox);
return;
}
let output = Command::new(&armybox)
.args(["cat", "/nonexistent/file"])
.output()
.unwrap();
assert_ne!(output.status.code(), Some(0));
assert!(std::string::String::from_utf8_lossy(&output.stderr).contains("No such file"));
}
#[test]
fn test_cat_unbuffered_option() {
let armybox = get_armybox_path();
if !armybox.exists() {
eprintln!("Skipping test: armybox binary not found at {:?}", armybox);
return;
}
let dir = setup();
let output = Command::new(&armybox)
.args(["cat", "-u", dir.join("file1.txt").to_str().unwrap()])
.output()
.unwrap();
assert_eq!(output.status.code(), Some(0));
assert_eq!(std::string::String::from_utf8_lossy(&output.stdout), "hello\n");
cleanup(&dir);
}
#[test]
fn test_cat_stdin_dash() {
let armybox = get_armybox_path();
if !armybox.exists() {
eprintln!("Skipping test: armybox binary not found at {:?}", armybox);
return;
}
let mut child = Command::new(&armybox)
.args(["cat", "-"])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.unwrap();
{
let stdin = child.stdin.as_mut().unwrap();
stdin.write_all(b"stdin test").unwrap();
}
let output = child.wait_with_output().unwrap();
assert_eq!(std::string::String::from_utf8_lossy(&output.stdout), "stdin test");
}
#[test]
fn test_cat_empty_file() {
let armybox = get_armybox_path();
if !armybox.exists() {
eprintln!("Skipping test: armybox binary not found at {:?}", armybox);
return;
}
let dir = setup();
fs::write(dir.join("empty.txt"), "").unwrap();
let output = Command::new(&armybox)
.args(["cat", dir.join("empty.txt").to_str().unwrap()])
.output()
.unwrap();
assert_eq!(output.status.code(), Some(0));
assert_eq!(output.stdout.len(), 0);
cleanup(&dir);
}
#[test]
fn test_cat_binary_data() {
let armybox = get_armybox_path();
if !armybox.exists() {
eprintln!("Skipping test: armybox binary not found at {:?}", armybox);
return;
}
let dir = setup();
fs::write(dir.join("binary.bin"), &[0u8, 1, 2, 255, 254, 253]).unwrap();
let output = Command::new(&armybox)
.args(["cat", dir.join("binary.bin").to_str().unwrap()])
.output()
.unwrap();
assert_eq!(output.status.code(), Some(0));
assert_eq!(output.stdout, vec![0u8, 1, 2, 255, 254, 253]);
cleanup(&dir);
}
#[test]
fn test_cat_mixed_stdin_and_file() {
let armybox = get_armybox_path();
if !armybox.exists() {
eprintln!("Skipping test: armybox binary not found at {:?}", armybox);
return;
}
let dir = setup();
let mut child = Command::new(&armybox)
.args([
"cat",
dir.join("file1.txt").to_str().unwrap(),
"-",
])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.unwrap();
{
let stdin = child.stdin.as_mut().unwrap();
stdin.write_all(b"STDIN").unwrap();
}
let output = child.wait_with_output().unwrap();
assert_eq!(std::string::String::from_utf8_lossy(&output.stdout), "hello\nSTDIN");
cleanup(&dir);
}
#[test]
fn test_cat_no_args_stdin() {
let armybox = get_armybox_path();
if !armybox.exists() {
eprintln!("Skipping test: armybox binary not found at {:?}", armybox);
return;
}
let mut child = Command::new(&armybox)
.args(["cat"])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.unwrap();
{
let stdin = child.stdin.as_mut().unwrap();
stdin.write_all(b"from stdin").unwrap();
}
let output = child.wait_with_output().unwrap();
assert_eq!(output.status.code(), Some(0));
assert_eq!(std::string::String::from_utf8_lossy(&output.stdout), "from stdin");
}
}