10c · echo¶
Released Fri Sep 18, in class
This is the README OSlings gives every student for 10c_echo (oslings
lesson shows it). The starter code is not published here: it reaches you
through OSlings, in class on the class network. Back to
All Exercises.
Learn → Understand → Implement. Your first Unix command, and the façade that will let this exact file run on the kernel you write in December.
Learn¶
So far you have written Rust functions that other Rust functions called.
A command is different: it is a whole program, started by a shell, handed a
list of words, and expected to produce bytes and a number. That is the entire
job description, and echo is the smallest program that has one.
What a Unix command actually is¶
When you type
the shell splits your line into words, finds the program named by the first word, and starts it with the rest as arguments. The program receives:
argv— the argument vector, an array of strings.argv[0]is the name the program was invoked as (echo), and the real arguments start atargv[1].argcis how many entries there are.- standard output (
stdout), file descriptor 1 — a place to write bytes. Normally your terminal; if the shell saw> out.txt, a file instead. The program cannot tell the difference and does not care. - an exit status it returns when it finishes:
0means success, anything else means failure. The shell keeps it in$?.
echo uses all three: it reads argv, writes to fd 1, and exits 0.
What the real echo does¶
Deceptively little, but exactly:
The rule is: join argv[1..] with a single space, then write one newline.
Look closely at the last two examples, because they are where people get this
wrong. The space is a separator, not a terminator: it goes between
neighboring arguments, so n arguments produce n - 1 spaces. And the empty
argument in echo a "" b still counts as an argument, so you get
a, space, nothing, space, b — two adjacent spaces. Meanwhile bare echo
has zero arguments, zero separators, and still prints its newline.
Separator-not-terminator is a pattern, not a trick question. You will write it
again in 12c_wc (columns), in your shell's prompt code, and in every kernel
routine that prints a table.
Why you are writing it against ulib¶
ulib is a thin façade: a small, fixed set of I/O calls (read, write,
open, close, exit, plus helpers like write_all) that your command is
written against instead of std::io. Today ulib forwards those calls to
macOS or Linux and the whole thing is an ordinary cargo binary on your
laptop.
In December, when your kernel boots into user mode, oslings ship echo will
recompile this same source file for riscv64gc-unknown-none-elf. ulib
switches its backend to raw ecall instructions, the linker produces a flat
image, and rv6's exec — which you will have written — copies it into a
fresh address space and jumps to it. Your echo then runs on your operating
system, at your shell's prompt. The file does not change. Not one line.
That promise is the reason for every constraint in the command exercises:
- No allocator. rv6 user programs have no heap, so no
Vec, noString, noformat!. Fixed-size buffers and slices only. - No
core::fmt. A singlewrite!drags 12–18 KiB of formatting machinery into an image with a hard size budget. That is whyulibgives youwrite_usizeinstead. - Bytes, not strings.
execpushes NUL-terminated bytes onto the new program's stack. There is no UTF-8 anywhere, so arguments are&[u8]and writes take&[u8].b"\n"is a byte-string literal — a&[u8; 1], which coerces to&[u8]— and that is the typewrite_allwants.
The test code at the bottom of the file is host-only and may use Vec freely.
Your run may not.
The pieces of ulib you need today¶
args.len() // argc, including argv[0]
args.get(i) // Option<&[u8]> — argument i, or None if out of range
ulib::write_all(STDOUT, bytes) -> Result<(), Error>
write_all rather than write is deliberate. A raw write is allowed to
write fewer bytes than you gave it and tell you so in its return value;
ignoring that is a classic silent bug where long lines come out truncated.
write_all loops until everything is out. Use it always.
Understand¶
Open commands/src/bin/echo.rs. It is short enough to read top to bottom.
That is the entire bare-metal ceremony: when — and only when — the target is
target_os = "none" (the RISC-V kernel target), the crate opts out of the
standard library and out of the normal main entry point. On your laptop the
attribute does nothing at all.
This expands to a plain fn main() on the host, and to the _start symbol
that rv6's exec jumps to on the kernel — unpacking argc/argv from the
stack in both cases and passing the result to your run. Notice what your
file does not contain: no #[cfg] attributes, no #[no_mangle], no
duplicated logic per target. That is the façade doing its job.
Your function. The i32 you return becomes the process exit status, so
returning 0 is you reporting success.
At the bottom is the #[cfg(test)] mod tests block. As in the Rust exercises, that block
is the contract: it is given to you, you never edit it, and it is compiled
only under cargo test. Read the four tests before writing anything — they
pin down precisely the corners described above:
| Test | What it pins down |
|---|---|
joins_arguments_with_single_spaces |
the normal case, and exit status 0 |
no_arguments_prints_just_a_newline |
zero arguments still prints "\n" |
a_single_argument_has_no_trailing_space |
separator, not terminator |
empty_arguments_are_preserved |
["a", "", "b"] → "a b\n" |
The harness (ulib::testing) calls your run directly, with writes diverted
into a buffer. No process is spawned and no test-only code path exists in your
command — the source under test is byte-for-byte the source that will run on
rv6.
Implement¶
Fill in the two IMPLEMENT markers in the body of run:
- The loop — for each index from
1toargs.len() - 1, write a single space first if this is not the first argument, then write the argument's bytes. - The newline — one
b"\n"after the loop, unconditionally.
Then delete the let _ = (args, STDOUT); line; it exists only to keep the
compiler quiet while nothing in the body uses either name yet.
Check your work:
That is plain cargo test on your own machine — no QEMU, no kernel, nothing to
boot. Green tests mean you are done. The RISC-V build of this same file is
opt-in and does not happen until exercise 53k, in December.
Stuck? oslings hint.