RustForge

rust-kernel

Rustbelt: a hobby x86_64 kernel written in Rust

rust-kernel / user / echo.rs
599 bytes ยท raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//! Reads lines from the keyboard (fd 0) and echoes them back. "exit" quits.
#![no_std]
#![no_main]

#[macro_use]
#[path = "ulib.rs"]
mod ulib;

fn main() -> i64 {
    println!("[echo] pid {}: type a line and press Enter", ulib::getpid());
    let mut buf = [0u8; 256];
    loop {
        let n = ulib::read(0, &mut buf); // blocks until Enter
        if n <= 0 {
            return 1;
        }
        let line = core::str::from_utf8(&buf[..n as usize]).unwrap_or("?").trim_end();
        println!("[echo] you typed: {}", line);
        if line == "exit" {
            return 0;
        }
    }
}