ajhahn.de
← FlashOS
Flash 38 lines
// rm — remove files for /bin/rm.
//
//   rm FILE...
//
// Unlinks each argument in turn (SYS_UNLINK): the kernel tombstones the
// directory entry and frees the file's cluster chain. Files only — a
// directory is refused by the syscall (no -r). A file that cannot be removed
// (missing, a directory, read-only mount) prints a diagnostic to fd 2 and the
// next argument is still tried. No flags.
//
// Same coreutil recipe as cat / echo: flibc _start shim, flibc_mem, no
// buffers of its own.

use flibc

link "flibc_start"
link "flibc_mem"

fn diag(msg []u8) {
    _ = flibc.sys.write_fd(2, msg.ptr, msg.len)
}

export fn main(argc usize, argv argv) noreturn {
    if argc < 2 {
        diag("usage: rm FILE...\n")
        flibc.exit()
    }
    var i usize = 1
    while i < argc {
        const path = argv[i] orelse break
        i += 1
        if flibc.sys.unlink(path) < 0 {
            diag("rm: cannot remove file\n")
        }
    }
    flibc.exit()
}