linux-sysprog · intermediate · ~15 min

Decode an exit status

Decode the wait status bitfield.

Challenge

wait fills a status word; WEXITSTATUS extracts the child's exit code from it. Reproduce that decoding.

Task

Implement int exit_code(int wstatus) that returns the child's exit code, which lives in bits 8-15 of wstatus.

Input

  • wstatus: a wait status word (integer).

Output

The exit code: (wstatus >> 8) & 0xFF.

Example

exit_code(0)        ->   0
exit_code(5 << 8)   ->   5
exit_code(256)      ->   1

Input format

wstatus: a wait status word.

Output format

The exit code in bits 8-15: (wstatus >> 8) & 0xFF.

Starter code

int exit_code(int wstatus) {
    /* TODO */
    return 0;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.