ajhahn.de
← Flash
Flash 80 lines
// sysinfo — one-screen system summary for /bin/sysinfo.
//
// Prints the logged-in user and the kernel's free-page count through the
// console_ui banner and key/value rows. The first Flash port to use an
// optional-capture `if`: `if pwfile.lookupByUid(...) |entry| { … }` runs the
// matched arm only when the lookup yields a record, with `entry` bound to the
// unwrapped value — lowering 1:1 to Zig's `if (opt) |entry| { … }`. The numeric
// uid is rendered with a small `u64dec` helper (decimal conversion via `%` and
// `/`), and the free-page line is assembled with a `for`-over-bytes copy.
//
// pwfile and console_ui are the existing FlashOS modules; build_options carries
// the compile-time PASSWD_MAX bound. Only the optional-capture `if` is new
// surface — every other construct landed with the v0.2 control-flow sweep.

use flibc
use pwfile
use console_ui
use build_options

link "flibc_start"
link "flibc_mem"

const PASSWD_MAX usize = build_options.passwd_max

fn u64dec(out []mut u8, v u64) usize {
    var x u64 = v
    if x == 0 {
        out[0] = '0'
        return 1
    }
    var tmp [20]u8 = undefined
    var n usize = 0
    while x != 0 {
        tmp[n] = '0' + #as(u8, #intCast(x % 10))
        n += 1
        x /= 10
    }
    var i usize = 0
    while i < n {
        out[i] = tmp[n - 1 - i]
        i += 1
    }
    return n
}

fn currentUser(buf []mut u8) []u8 {
    uid_raw := flibc.sys.getuid()
    if uid_raw < 0 {
        return "?"
    }
    uid := #as(u32, #intCast(uid_raw))
    n := u64dec(buf, uid)
    if pwfile.lookupByUid(buf[0..n], uid) |entry| {
        return entry.user
    }
    return buf[0..n]
}

fn freePages(out []mut u8) usize {
    pages := flibc.sys.dump_free()
    var n usize = u64dec(out, pages)
    suffix := " free"
    for c in suffix {
        out[n] = c
        n += 1
    }
    return n
}

export fn main(_ usize, _ argv) noreturn {
    console_ui.banner("sysinfo")
    var ubuf [PASSWD_MAX]u8 = undefined
    user := currentUser(&ubuf)
    console_ui.screen.kv("user", user)
    var fbuf [32]u8 = undefined
    m := freePages(&fbuf)
    console_ui.screen.kv("memory", fbuf[0..m])
    flibc.exit()
}