Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

csh: A Unix Shell in C

A Unix shell implementation written in C, supporting piping, an interactive history browser, and standard builtins. Built from scratch using POSIX system calls.

Features

  • Command execution — runs any program available on $PATH via execvp
  • Piping — chains commands with |, supporting multiple pipes (e.g. ls | grep foo | wc -l)
  • Interactive history browser — press Ctrl+\ to enter history mode, browse with arrow keys, select with Enter
  • Builtins — cd, exit, and help
  • Signal handling — Ctrl+C kills the foreground process without terminating the shell, SIGTERM restores terminal state before exiting
  • Input preprocessing — automatically normalises spacing around | so ls|grep foo works as expected
  • Dynamic argument lists — argument buffers grow automatically via realloc, no hardcoded limits

Building

make

Requires GCC and GNU Make.

Usage

./shell

Commands

>> ls -la                        # run any program
>> ls | grep foo | wc -l         # pipe commands
>> cd ~/projects                 # change directory
>> exit                          # exit the shell

History Mode

Press Ctrl+\ to enter history mode:

  • ↑ / ↓ — browse history
  • Enter — execute selected command
  • q — return to normal mode

Duplicate consecutive commands are not added to history.

Project Structure

shell/
├── main.c          # main loop
├── parse.c/.h      # tokenisation and input preprocessing
├── execute.c/.h    # forking, piping, execvp
├── history.c/.h    # history linked list and interactive browser
├── builtins.c/.h   # builtin command dispatch table
├── signals.c/.h    # signal handler setup and handlers
├── globals.c/.h    # shared global state
└── Makefile

Implementation Notes

  • Commands are stored as a linked list of linked_cmds nodes, each holding a NULL-terminated char** argument array — the format execvp expects directly
  • Piping is implemented by creating a pipe() before each piped command, using dup2 to wire stdout to the write end in the child and stdin to the read end in the next child
  • History is a doubly linked list to support both forward and backward browsing
  • History mode uses raw terminal mode (termios) to read keypresses without waiting for Enter
  • SIGQUIT (Ctrl+\) sets a volatile sig_atomic_t flag that the getline loop checks after EINTR to enter history mode safely

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages