feat(userspace): shell runs programs (ls, cat) via spawn w/ terminal+storage+args page; ext4 holes read as zeros; storage re-attach same buffer
This commit is contained in:
13
crates/apps/cat/Cargo.toml
Normal file
13
crates/apps/cat/Cargo.toml
Normal file
@@ -0,0 +1,13 @@
|
||||
[package]
|
||||
name = "cat"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[[bin]]
|
||||
name = "cat"
|
||||
test = false
|
||||
bench = false
|
||||
|
||||
[dependencies]
|
||||
types = { workspace = true }
|
||||
libkos = { workspace = true }
|
||||
45
crates/apps/cat/src/main.rs
Normal file
45
crates/apps/cat/src/main.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
//! Prints files: `cat <file>...`.
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
extern crate libkos;
|
||||
|
||||
use libkos::fs::{FsError, Storage};
|
||||
use libkos::println;
|
||||
|
||||
/// Where storage's buffer is mapped.
|
||||
const STORAGE_BUF_VA: usize = 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)
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
extern "C" 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(_) => println!("cat: {}: cannot read", path),
|
||||
}
|
||||
}
|
||||
if !any {
|
||||
println!("usage: cat <file>...");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user