get_next_line is a C function that reads from a file descriptor one line at a time. It uses a static variable to buffer data between calls, dynamically allocating just enough memory for each line. The function supports a configurable BUFFER_SIZE set at compile time and works with multiple file descriptors simultaneously.

This was a challenging project — managing memory precisely while handling edge cases like varying buffer sizes, empty lines, and files without a trailing newline. During the live coding evaluation, I was also asked to adapt the function to split on a custom delimiter instead of newline.

What I learned: static variables for state persistence across function calls, careful memory management with malloc and free, and reading from file descriptors at a low level.

Peer feedback:

“Nice get next line, handling all the potential errors returned from malloc and read.”

— Peer evaluator, 42 London

View on GitHub