linux-sysprog · intermediate · ~15 min

errno after a failed open

Read errno after a failed syscall.

Challenge

When a syscall fails it returns a sentinel and sets the global errno to describe why. Trigger a failure and read errno.

Task

Implement int missing_file_errno(void) that attempts to open a file that does not exist (read-only) and returns the resulting errno value.

Input

None.

Output

The errno set by the failed openENOENT ("no such file or directory").

Example

missing_file_errno()   ->   ENOENT

Rules

  • Read errno immediately after the failing call, before any other library call can overwrite it.

Input format

None.

Output format

The errno value after a failed open (ENOENT).

Constraints

Read errno right after the failing syscall.

Starter code

#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.