linux-sysprog · intermediate · ~15 min

Copy bytes between fds

Loop the read/write pair correctly; handle short writes.

Challenge

Implement ssize_t fd_copy(int src, int dst) that reads bytes from src and writes them to dst until EOF, returning the total number of bytes copied or -1 on error. Use a buffer of any reasonable size (e.g. 4096 bytes).

Starter code

#include <unistd.h>
#include <stddef.h>

ssize_t fd_copy(int src, int dst) {
    /* TODO */
    return -1;
}

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