//! cat [file...] -- print files (or stdin, also for "-"). Shell alias: type. #![no_std] #![no_main] #[macro_use] #[path = "ulib.rs"] mod ulib; use ulib::*; /// Copy everything from `fd` to stdout. fn copy(fd: i64) -> i64 { let mut buf = [0u8; 512]; loop { let n = read(fd, &mut buf); if n <= 0 { return n; // 0 = end of file, < 0 = error } write_all(1, &buf[..n as usize]); } } fn main() -> i64 { if argc() < 2 { return if copy(0) < 0 { 1 } else { 0 }; } let mut status = 0; for i in 1..argc() { let name = arg(i).unwrap_or(""); let fd = if name == "-" { 0 } else { open(name, 0) }; let r = if fd < 0 { fd } else { copy(fd) }; if r < 0 { eprintln!("cat: {}: {}", name, strerror(r)); status = 1; } if fd > 0 { close(fd); } } status }