Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CShell — A Unix Shell Written in C

A lightweight, POSIX-compliant command-line interpreter built from scratch in C. This project demonstrates core operating-system principles — process management, inter-process communication, file descriptor manipulation, and manual memory management — implemented without relying on any existing shell.


Contents


Features Implemented

Feature Details
Process execution Executes standard system commands (ls, grep, cat, etc.) by searching the PATH environment variable
Built-in: cd Changes the shell's current working directory
Built-in: pwd Prints the current working directory
Built-in: history Displays a ring buffer of recently executed commands
Built-in: exit Safely terminates the shell and frees all allocated memory
Output redirection Truncating (>) and appending (>>) to a file
Input redirection Reading command input from a file (<)

Architecture & System Calls

The shell runs an infinite REPL (Read–Eval–Print Loop), split into distinct modules:

  1. Parser (parser.c) Reads raw input with getline() (avoids fixed-size buffer overflows) and tokenizes it, separating redirection symbols (>, >>, <) from the command's own arguments.

  2. Executor (executor.c) Handles process creation:

    • fork() clones the parent process.
    • execvp() replaces the child's memory image with the target executable.
    • waitpid() blocks the parent until the child finishes, preventing zombie processes.
  3. File descriptor redirection When redirection is detected, the child opens the target file with open() and uses dup2() to overwrite STDIN_FILENO (0) or STDOUT_FILENO (1) before calling execvp() — rerouting I/O transparently, with no changes needed in the executed program itself.


Getting Started

Prerequisites

  • GCC
  • GNU Make
  • A Unix/Linux environment (or WSL on Windows) — this project relies on POSIX system calls

Build and run

# Clone the repository
git clone https://github.com/RahulBiswas224/CShell.git
cd CShell

# Compile using the included Makefile
make

# Run the shell
./myshell

Clean build artifacts

make clean

Manual Testing Examples

myshell> ls -la
myshell> echo "Hello World" > output.txt
myshell> cat < output.txt
myshell> echo "Appending text" >> output.txt
myshell> history
myshell> exit

Future Work / Roadmap

The architecture was designed to be extensible. Planned next:

  • Pipes (|) — wire one process's stdout directly into another's stdin using pipe(), e.g. ls -la | grep .c
  • Background jobs (&) — run processes asynchronously, with a zombie-reaping loop using waitpid() and the WNOHANG flag so the shell isn't blocked waiting on background children
  • Signal handling — catch SIGINT (Ctrl+C) and SIGTSTP (Ctrl+Z) so they interrupt the running child process without killing the shell itself

Author

Built by Rahul Biswas as a systems-programming project ahead of technical interviews focused on C, operating systems, and process management.

About

A Unix shell written from scratch in C — supports piping, I/O redirection, background jobs, and built-in commands. No external shell dependencies.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages