linux-sysprog · intermediate · ~15 min

Read with raw descriptors

Use the unbuffered fd-based I/O layer.

Challenge

Using the POSIX syscalls open/read/close (not stdio), implement int posix_read(const char *path, char *buf, int n) reading up to n bytes, returning the count, or -1 if open fails.

Starter code

#include <fcntl.h>
#include <unistd.h>

int posix_read(const char *path, char *buf, int n) {
    /* TODO */
    return -1;
}

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