All 42 projects

42 Project brief

minishell

A small bash, written in C with a partner. My partner parsed the command line; I built everything that runs it: processes, pipes, redirections, heredocs, builtins and signals.

My role
Execution: processes, pipes, builtins
Team
2 developers
Validated
Jul 2022
Workload
210 h

The project

minishell is the 42 project where you write your own shell, modeled on bash: a prompt with history, programs found through PATH, quotes and variable expansion, pipes, redirections, heredocs, a set of builtins, exit codes and the usual keyboard shortcuts.

Only readline and a short list of system calls are allowed, so everything, from parsing a command line to wiring the file descriptors of a pipeline, is written by hand in C.

The team’s stack

  • C
  • fork & execve
  • pipe & dup2
  • File descriptors
  • Signals
  • readline
  • Make

My part

We split the work cleanly: my partner wrote the tokenizer, syntax checks and quote handling, and I wrote the execution side, 27 files and about 2,100 lines of C.

01

Running commands

From a parsed command to a running process.

  • Finds programs through PATH with access(), and answers command not found with exit status 127
  • Forks, rewires the file descriptors with dup2, runs execve and reads the exit status, so $? is always right
  • Builtins like cd, export and exit run inside the shell itself, so they can change its state
02

Pipes

Pipelines of any length, like ls | grep .c | wc -l.

  • One pipe per link, each command in its own child process with its input and output rewired
  • Unused pipe ends are closed in every process, so each command sees the end of its input and the pipeline finishes
  • The shell waits for the whole pipeline before showing the prompt again
03

Redirections & heredoc

Files and inline input as a command’s input or output.

  • < to read a file, > to overwrite and >> to append, opened with the matching flags
  • Heredoc (<<) collects lines until the delimiter into a temporary file, with its own Ctrl-C handling to cancel it
04

Builtins

The commands a shell has to run itself.

  • echo with -n, cd (home when there is no argument), pwd, export, unset, env and exit
  • export with or without a value, and export alone lists the variables as declare -x
  • exit checks its argument and returns 255 for a non-numeric one, like bash
05

Prompt, environment & signals

The parts you only notice when they are wrong.

  • A readline prompt with history
  • Ctrl-C gives a fresh prompt, Ctrl-\ is ignored and Ctrl-D exits, as in bash
  • The environment is copied at startup into the shell’s own list, which export and unset edit

Inside a pipeline

  1. 1

    Parse

    For ls | grep .c > out.txt, my partner’s parser returns two commands, the redirection attached to the second.

  2. 2

    Plumb

    A pipe is created to connect them.

  3. 3

    Fork

    Each command gets its own child: ls writes into the pipe, grep reads from it.

  4. 4

    Redirect

    out.txt is opened, truncated, and becomes grep’s standard output.

  5. 5

    Run

    Each child finds its program through PATH and replaces itself with execve.

  6. 6

    Wait

    The shell waits for both, stores the exit status in $?, and shows a new prompt.

Code