Every command a shell runs reports a number when it finishes - zero for success, non-zero for failure. Today you capture that number and remember it, which later powers `$?`, `&&`, and `||`.
After a command finishes, extract its exit code and store it as the shell's last status.
When a program finishes, it hands the operating system a small integer - its
exit status. By convention 0 means success and anything else means some kind
of failure (1, 2, 127, …). The wait you already do returns this status
packed together with other information about how the child died, so you have to
unpack the plain exit code out of it.
Store that code on the shell as the last status. On its own it does nothing
visible today, but it is the value behind three things you will build soon: the
$? variable that prints it, and the && and || operators that decide whether
to run the next command based on whether the previous one succeeded. Getting the
capture right now means all three come almost for free later.
waitpid(pid, &status, 0);if (WIFEXITED(status))last_status = WEXITSTATUS(status); // 0..255// keep last_status on the shell for later ($?, && , ||)