Project 6: I/O Redirection Engine

Support for all standard redirections: > file, >> file, < file, 2>&1, &> file, <<EOF (here-docs), and numbered fd redirections like 3>&1.

Quick Reference

Attribute Value
Primary Language C
Alternative Languages Rust, Zig, Go
Difficulty Level 3: Advanced (The Engineer)
Time Estimate 1 week
Knowledge Area Operating Systems / File Descriptors
Tooling Unix Shell
Prerequisites Projects 1-5, solid understanding of file descriptors

What You Will Build

Support for all standard redirections: > file, >> file, < file, 2>&1, &> file, <<EOF (here-docs), and numbered fd redirections like 3>&1.

Why It Matters

This project builds core skills that appear repeatedly in real-world systems and tooling.

Core Challenges

  • Opening files with correct flags (O_CREAT, O_TRUNC, O_APPEND) → maps to file operations
  • Implementing fd duplication (2>&1 vs 2>file) → maps to dup2 semantics
  • Order-sensitive redirections (cmd >file 2>&1 vs cmd 2>&1 >file differ!) → maps to evaluation order
  • Here-documents (reading until delimiter, creating temp file) → maps to advanced features
  • Saving/restoring fds (for built-ins that redirect) → maps to fd management

Key Concepts

  • File descriptor duplication: “The Linux Programming Interface” Chapter 5 - Kerrisk
  • Open flags: “Advanced Programming in the UNIX Environment” Chapter 3 - Stevens
  • Here-documents: POSIX Shell Specification Section 2.7.4 - The Open Group

Real-World Outcome

$ ./mysh
mysh> echo hello > output.txt
mysh> cat output.txt
hello
mysh> echo world >> output.txt
mysh> cat output.txt
hello
world
mysh> ls nonexistent 2>&1 | head
ls: nonexistent: No such file or directory
mysh> cat << EOF
> This is a
> here document
> EOF
This is a
here document
mysh> exec 3>logfile.txt
mysh> echo "logging" >&3
mysh> cat logfile.txt
logging

Implementation Guide

  1. Reproduce the simplest happy-path scenario.
  2. Build the smallest working version of the core feature.
  3. Add input validation and error handling.
  4. Add instrumentation/logging to confirm behavior.
  5. Refactor into clean modules with tests.

Milestones

  • Milestone 1: Minimal working program that runs end-to-end.
  • Milestone 2: Correct outputs for typical inputs.
  • Milestone 3: Robust handling of edge cases.
  • Milestone 4: Clean structure and documented usage.

Validation Checklist

  • Output matches the real-world outcome example
  • Handles invalid inputs safely
  • Provides clear errors and exit codes
  • Repeatable results across runs

References

  • Main guide: SHELL_INTERNALS_DEEP_DIVE_PROJECTS.md
  • “The Linux Programming Interface” by Michael Kerrisk