49 lines
1.3 KiB
Rust
49 lines
1.3 KiB
Rust
//! Prints files: `cat <file>...`.
|
|
|
|
#![no_std]
|
|
#![forbid(unsafe_code)]
|
|
#![no_main]
|
|
|
|
extern crate libkos;
|
|
|
|
use libkos::fs::{FsError, Storage};
|
|
use libkos::println;
|
|
|
|
/// Where storage's buffer is mapped.
|
|
const STORAGE_BUF_VA: types::address::VirtualAddress = types::address::VirtualAddress::from_raw(0x0180_0000);
|
|
|
|
fn cat(fs: &mut Storage, path: &str) -> Result<(), FsError> {
|
|
let h = fs.open(path.as_bytes())?;
|
|
let mut buf = [0u8; 4096];
|
|
loop {
|
|
let n = fs.read(h, &mut buf)?;
|
|
libkos::io::write_bytes(&buf[..n]);
|
|
if n < buf.len() {
|
|
break;
|
|
}
|
|
}
|
|
fs.close(h)
|
|
}
|
|
|
|
libkos::entry!(main);
|
|
|
|
fn main() {
|
|
let Some(mut fs) = libkos::start::storage().and_then(|ep| Storage::attach(ep, STORAGE_BUF_VA).ok()) else {
|
|
println!("cat: no storage");
|
|
return;
|
|
};
|
|
let mut any = false;
|
|
for path in libkos::start::args().skip(1) {
|
|
any = true;
|
|
match cat(&mut fs, path) {
|
|
Ok(()) => {}
|
|
Err(FsError::NotFound) => println!("cat: {}: no such file", path),
|
|
Err(FsError::Denied) => println!("cat: {}: outside what it was given", path),
|
|
Err(_) => println!("cat: {}: cannot read", path),
|
|
}
|
|
}
|
|
if !any {
|
|
println!("usage: cat <file>...");
|
|
}
|
|
}
|