#![no_std] #![no_main] use lancer_user::fs; use lancer_user::net; use lancer_user::syscall; use lancer_user::{print, println}; fn print_chunked(data: &[u8]) { data.split_inclusive(|&b| b == b'\n').for_each(|segment| { match core::str::from_utf8(segment) { Ok(s) => print!("{}", s), Err(_) => lancer_user::io::write_bytes(segment), } }); } #[unsafe(no_mangle)] pub extern "C" fn lancer_main() -> ! { let (rx, _tx) = match net::init() { Some(pair) => pair, None => { lancer_user::show!(net, error, "netsock init failed"); syscall::exit(); } }; let mut args_buf = [0u8; 256]; let args_len = net::recv_args(&rx, &mut args_buf); let args = &args_buf[..args_len]; let (_cmd, rest) = net::next_token(args); let (path_tok, _) = net::next_token(rest); if path_tok.is_empty() { println!("usage: cat "); syscall::exit(); } let mut client = unsafe { fs::init() }; let handle = match fs::open_path_with_rights(&mut client, 0, path_tok, fs::FsRights::READ) { Ok(h) => h, Err(e) => { println!("cat: {}", e.name()); syscall::exit(); } }; let mut buf = [0u8; 4096]; fn read_all(client: &mut fs::FsClient, handle: u8, buf: &mut [u8], offset: u64) { match client.read(handle, offset, buf) { Ok(0) => {} Ok(n) => { let data = &buf[..n]; print_chunked(data); read_all(client, handle, buf, offset + n as u64) } Err(e) => { println!("cat: read error: {}", e.name()); } } } read_all(&mut client, handle, &mut buf, 0); let _ = client.close(handle); syscall::exit() }