Skip to content

ichiro-wang/simple-shell

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple Shell Application

A custom shell implementation for CMPT 201 Systems Programming

Key System Calls Used

This shell was implemented using several core system calls:

fork()

  • Used to create a new child process for executing external commands.
  • Enables running commands in the foreground or background by forking separate processes.

exec()

  • Used within child processes to replace the process image with the specified command and its arguments.
  • Ensures the child process runs the desired program.

wait()

  • Used in foreground execution to wait for the child process to complete before returning control to the shell.
  • In background execution, waitpid() is used in a non-blocking loop to clean up terminated child processes and prevent zombie processes.

getcwd()

  • Used to retrieve and display the current working directory as part of the shell prompt and for the pwd command.

chdir()

  • Used to implement the cd command to change the current working directory.

strtok_r()

  • Used to tokenize and parse user input into commands and arguments safely.

read() and write()

  • Used for reading user input and writing shell output, ensuring signal-safe I/O operations.

Signal Handling (SIGINT)

  • A custom handler for SIGINT (triggered by CTRL-C) prevents the shell from terminating.
  • Displays help information and re-displays the shell prompt when interrupted.

Features

Command Execution

  • Execute external programs with arguments.
  • Support for background execution using &.

Internal Commands

  • exit: Exits the shell program.
    • Prints an error if any arguments are provided (exit: too many arguments).
  • pwd: Prints the current working directory.
    • Prints an error if arguments are provided (pwd: too many arguments).
  • cd: Changes the current working directory.
    • ~: Changes to the home directory (e.g., cd ~/tmp).
    • -: Changes to the previous directory.
    • Handles errors such as invalid directories or too many arguments.
  • help: Displays help information about internal commands.
    • No arguments: Lists all supported internal commands and their descriptions.
    • First argument: Displays help for a specific internal command.
    • Errors: Displays help: too many arguments if more than one argument is provided.

History Feature

  • history: Displays up to the 10 most recent commands executed in the shell.
    • Outputs command numbers and full command details, including arguments.
    • Background commands (&) are included.
  • !n: Re-runs the command with the history number n.
    • Prints an error if n is invalid (history: command invalid).
  • !!: Re-runs the last command.
    • Prints an error if no command has been entered (history: no command entered).

Signal Handling

  • CTRL-C: Does not terminate the shell. Instead:
    • Displays help information (same as the help command).
    • Re-displays the shell prompt.

Shell Prompt

  • Displays the current working directory as the shell prompt (e.g., /home/user$).
  • Uses getcwd() to retrieve the working directory and handles errors gracefully.

About

A custom shell implementation created using system calls such as fork, exec, wait, etc.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors