A custom shell implementation for CMPT 201 Systems Programming
This shell was implemented using several core system calls:
- Used to create a new child process for executing external commands.
- Enables running commands in the foreground or background by forking separate processes.
- Used within child processes to replace the process image with the specified command and its arguments.
- Ensures the child process runs the desired program.
- 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.
- Used to retrieve and display the current working directory as part of the shell prompt and for the
pwdcommand.
- Used to implement the
cdcommand to change the current working directory.
- Used to tokenize and parse user input into commands and arguments safely.
- Used for reading user input and writing shell output, ensuring signal-safe I/O operations.
- A custom handler for
SIGINT(triggered byCTRL-C) prevents the shell from terminating. - Displays help information and re-displays the shell prompt when interrupted.
- Execute external programs with arguments.
- Support for background execution using
&.
exit: Exits the shell program.- Prints an error if any arguments are provided (
exit: too many arguments).
- Prints an error if any arguments are provided (
pwd: Prints the current working directory.- Prints an error if arguments are provided (
pwd: too many arguments).
- Prints an error if arguments are provided (
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 argumentsif more than one argument is provided.
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 numbern.- Prints an error if
nis invalid (history: command invalid).
- Prints an error if
!!: Re-runs the last command.- Prints an error if no command has been entered (
history: no command entered).
- Prints an error if no command has been entered (
CTRL-C: Does not terminate the shell. Instead:- Displays help information (same as the
helpcommand). - Re-displays the shell prompt.
- Displays help information (same as the
- Displays the current working directory as the shell prompt (e.g.,
/home/user$). - Uses
getcwd()to retrieve the working directory and handles errors gracefully.