linux-sysprog · intermediate · ~15 min

Write with raw descriptors

Create and write a file through descriptors.

Challenge

Implement int posix_write(const char *path, const char *data, int n) creating/truncating path and writing n bytes with open/write/close. Return the count written, or -1 on failure.

Starter code

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

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

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