A C implementation of the get_next_line function that reads a line from a file descriptor. This project handles efficient line-by-line reading from files, with support for multiple open file descriptors simultaneously. The implementation manages dynamic memory allocation to accumulate and return complete lines, regardless of buffer size.
- File descriptor handling in C
- Static variable usage for state persistence across function calls
- Dynamic memory allocation and management
- Efficient buffer handling for incremental reading
- Boundary case handling (empty files, no newline, multiple consecutive newlines)
git clone https://github.com/fabbbiodc/get_next_line.git
cd get_next_linemake # Compile mandatory version
make bonus # Compile bonus version (multiple fd support)Include the header and call the function:
#include "get_next_line.h"
char *line = get_next_line(fd);
while (line != NULL)
{
printf("%s", line);
free(line);
line = get_next_line(fd);
}The project includes built-in test targets:
make test # Run mandatory tests
make test_b # Run bonus tests (multiple fd)| Command | Description |
|---|---|
make |
Compile mandatory version |
make bonus |
Compile bonus version (multiple fd) |
make clean |
Remove object files |
make fclean |
Remove all build artifacts |
make re |
Full rebuild |
The get_next_line function reads from a file descriptor one line at a time:
- Reading - Uses
read()with a configurableBUFFER_SIZEto fetch data incrementally - Accumulating - Stores residual data in a
staticvariable to preserve state between calls - Extracting - Identifies newline boundaries and returns complete lines
- Cleanup - Returns
NULLwhen EOF is reached and frees remaining memory
The bonus version extends this to handle multiple file descriptors simultaneously by maintaining a separate static buffer for each fd.
- Configurable Buffer Size - Compile with
-D BUFFER_SIZE=<value>to tune read performance - Multiple File Descriptor Support - Bonus implementation reads from several open files concurrently
- Memory Safe - Properly handles allocation, reallocation, and cleanup of line buffers
- Edge Case Handling - Handles empty files, files without trailing newlines, and consecutive newlines
get_next_line/
├── get_next_line.c # Main implementation
├── get_next_line.h # Header file with prototype and BUFFER_SIZE
├── get_next_line_utils.c # Utility functions (strchr, strdup, join)
├── get_next_line_bonus.c # Bonus implementation (multiple fd support)
├── get_next_line_bonus.h # Bonus header
├── get_next_line_utils_bonus.c # Bonus utilities
├── test_mandatory.c # Mandatory tests
├── test_bonus.c # Bonus tests
├── test1.txt # Test file 1
├── test2.txt # Test file 2
└── Makefile # Build & test automation
- Language: C
- System Calls:
read(),malloc(),free() - Build: Makefile with configurable
BUFFER_SIZE
- The
read()System Call - Core system call used for reading - Static Variables in C - Key concept for state persistence
- Dynamic Memory Management - malloc, realloc, and free reference