Skip to content

ps600779/custom-C-shell

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Custom C Shell

A custom Unix-like shell implementation written in C++ that provides basic shell functionality with command execution, piping, I/O redirection, and process management.

Features

Built-in Commands

  • cd [directory] - Change directory

    • cd or cd ~ - Go to home directory
    • cd - - Go to previous directory
    • cd <path> - Change to specified directory
  • pwd - Print working directory

  • echo [args] - Display text to stdout

    • Supports quoted strings with proper spacing
  • ls [options] [directories] - List directory contents

    • -a - Show hidden files
    • -l - Long listing format
    • -la or -al - Combination of both flags
  • search <filename> - Search for a file recursively

    • Returns True if found, False otherwise
    • Uses breadth-first search
  • history [n] - Display command history

    • history - Shows last 10 commands
    • history <n> - Shows last n commands
    • Maintains up to 20 commands
    • Stores history in history.txt
  • pinfo [pid] - Display process information

    • pinfo - Show current process info
    • pinfo <pid> - Show specific process info
    • Displays: PID, status, memory usage, and executable path
  • fg <pid> - Bring background process to foreground

    • Sends SIGCONT signal to the specified process
  • exit - Exit the shell

Advanced Features

Command Chaining

  • Semicolon (;) - Execute multiple commands sequentially
    cd /tmp; ls; pwd

Piping

  • Pipe (|) - Chain commands together
    ls -l | grep txt | wc -l

Background Processes

  • Ampersand (&) - Run commands in background
    sleep 100 &

I/O Redirection

  • Input (<) - Redirect input from file
    sort < input.txt
  • Output (>) - Redirect output to file
    ls > output.txt

System Commands

Any command not recognized as a built-in will be executed as a system command using execvp().

Technical Details

Architecture

The shell consists of three main components:

  1. main.cpp - Main shell loop and command dispatcher
  2. utils.cpp - Implementation of all built-in commands and utilities
  3. headers.h - Header file with includes and function declarations

Key Components

Command Processing

  • processCommand() - Handles individual command execution
  • parseCommandTree() - Parses piped commands into linked list structure
  • executeCommandTree() - Executes piped commands with proper file descriptor management

Process Management

  • Uses fork() and execvp() for command execution
  • Implements proper process groups with setsid()
  • Handles foreground and background processes
  • Tracks process status (Running, Sleeping, Zombie, Stopped)

History Management

  • appendToHistory() - Adds commands to history (max 20)
  • printHistory() - Displays command history
  • Avoids duplicate consecutive entries
  • Persistent storage in history.txt

File Operations

  • searchFile() - BFS-based file search
  • listDirectory() - Directory listing with detailed info
  • Supports I/O redirection with file descriptors

Building and Running

Prerequisites

  • C++ compiler with C++17 support (g++)
  • macOS (uses macOS-specific libraries like libproc.h)
  • Make build system

Compilation

make

This will compile the source files and create the executable myprogram.

Running

./myprogram

Cleaning Build Files

make clean

Project Structure

custom-C-shell/
├── headers.h       # Header file with declarations and includes
├── main.cpp        # Main shell loop and command parsing
├── utils.cpp       # Implementation of built-in commands
├── Makefile        # Build configuration
├── history.txt     # Command history storage
└── README.md       # This file

Example Usage

# Basic navigation
priyanshu@MacBook:~> pwd
/Users/priyanshusharma/Documents/AOS_Assignment2

# List files with details
priyanshu@MacBook:~> ls -la

# Chain commands
priyanshu@MacBook:~> cd /tmp; ls; pwd

# Pipe commands
priyanshu@MacBook:~> ls -l | grep cpp

# Background process
priyanshu@MacBook:~> sleep 30 &
Started background process with PID: 12345

# Process information
priyanshu@MacBook:~> pinfo 12345

# Command history
priyanshu@MacBook:~> history 5

# File search
priyanshu@MacBook:~> search main.cpp
True

# I/O redirection
priyanshu@MacBook:~> ls > files.txt
priyanshu@MacBook:~> grep cpp < files.txt

Limitations

  • Platform-specific: Currently designed for macOS (uses libproc.h, proc_pidinfo, etc.)
  • Hardcoded paths: Some paths are hardcoded (e.g., home directory path in getRelativePath())
  • Limited error handling: Some edge cases may not be fully handled
  • No job control: Limited support for complex job control features

Future Enhancements

  • Cross-platform support (Linux, Windows)
  • Tab completion
  • Command history navigation with arrow keys
  • More robust signal handling
  • Configuration file support
  • Aliases and shell functions
  • Improved job control (bg, jobs commands)
  • Scripting support

Author

Priyanshu Sharma

License

This project is open source and available for educational purposes.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors