Flash 43 lines
// echo — write arguments to fd 1 for /bin/echo.
//
// Writes its arguments space-separated, followed by a newline; argv[0] (the
// program name) is skipped. No flags. Output goes through the unified
// write_fd(1, …) so that, when a shell runs `echo hi | cat`, echo's fd 1 can be
// redirected onto the pipe.
//
// Exercises a `while` loop with `orelse break`, an `if` guard, and a
// nul-terminated C-string helper (`emitz`) that scans for the terminator with a
// second `while`. flibc_mem is imported because the length scan can lower to a
// libcall.
use flibc
link "flibc_start"
link "flibc_mem"
fn emit(s []u8) {
_ = flibc.sys.write_fd(1, s.ptr, s.len)
}
fn emitz(s cstr) {
var n usize = 0
while s[n] != 0 {
n += 1
}
_ = flibc.sys.write_fd(1, s, n)
}
export fn main(argc usize, argv argv) noreturn {
var i usize = 1
while i < argc {
s := argv[i] orelse break
emitz(s)
if i + 1 < argc {
emit(" ")
}
i += 1
}
emit("\n")
flibc.exit()
}