← Back to selected work

get_next_line

A C function that reads input one line at a time while preserving unread state between calls.

Role
1337 / 42 Developer
Access
1337 / 42 validated project
[01]

Context

A C function that reads input one line at a time while preserving unread state between calls. The implementation handles variable buffer sizes, multiple file descriptors, end-of-file behavior, and memory cleanup.

[02]

My responsibility

Built and validated through the 1337 / 42 project curriculum, with direct responsibility for implementation, debugging, testing, and evaluation requirements.

[03]

System scheme

Engineering scheme

Buffered line-reader state machine

Input → Processing → Output

Unread bytes remain attached to each file descriptor so one line can be returned without losing the next call's data.

  1. 01

    File descriptor

    Accept a readable descriptor and configured buffer size.

  2. 02

    Buffered read

    Read chunks until a newline or end of input is reached.

  3. 03

    Persistent stash

    Combine new bytes with unread state from earlier calls.

  4. 04

    Line extraction

    Split and allocate the next complete line.

  5. 05

    Residual state

    Retain bytes after the newline for the next request.

  6. 06

    Cleanup

    Release state on EOF, read failure, or allocation failure.

Multiple descriptors
Allocation safety
Variable buffers
EOF behavior
[05]

Technical scope

C01
File Descriptors02
[07]

Implementation details

  • File descriptor: Accept a readable descriptor and configured buffer size.
  • Buffered read: Read chunks until a newline or end of input is reached.
  • Persistent stash: Combine new bytes with unread state from earlier calls.
  • Line extraction: Split and allocate the next complete line.
  • Residual state: Retain bytes after the newline for the next request.
  • Cleanup: Release state on EOF, read failure, or allocation failure.
[08]

Engineering focus

  • Multiple descriptors remained an explicit concern across the implementation.
  • Allocation safety remained an explicit concern across the implementation.
  • Variable buffers remained an explicit concern across the implementation.
  • EOF behavior remained an explicit concern across the implementation.

Need a system like this?

Tell me about the workflow, integration, or backend problem.

Discuss a project