linux-sysprog · intermediate · ~15 min
Read errno after a failed syscall.
When a syscall fails it returns a sentinel and sets the global errno to describe why. Trigger a failure and read errno.
Implement int missing_file_errno(void) that attempts to open a file that does not exist (read-only) and returns the resulting errno value.
None.
The errno set by the failed open — ENOENT ("no such file or directory").
missing_file_errno() -> ENOENT
errno immediately after the failing call, before any other library call can overwrite it.None.
The errno value after a failed open (ENOENT).
Read errno right after the failing syscall.
#include <fcntl.h>
#include <errno.h>
int missing_file_errno(void) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.