KCSH is a simple implementation of shell in C. It demonstrates the basics of how shell works. Since its purpose is demonstration (not completeness of features, it has many limitations. KCSH runs in interactive mode. KCSH is most rudimentary shell and is structured as the following loop:
- The shell runs continuously and displays a prompt of the format (pwd:~$) where pwd stands for the present work directory
- The shell reads input one at a time
- Parse the input into program name and an array of parameters
- Use
$fork()$ system call to spawn a new child process- The child process uses
$execvp()$ system call to launch specified program - The parent process uses
$wait()$ system call to wait for child process to terminate
- The child process uses
- Once the child process finishes. The shell repeates the loop.
- The only builtin commands are
exit
,cd
,history
Aditionally implemented functions to change the color of the prompt
Use gcc kcsh.c -o kcsh
to compile and then use ./kcsh
to run the shell
To Test for pipeline run the command ping -c 5 www.google.com | grep rtt
in the shell window
-
history [argument optional] command displays all the commands entered by the user in the shell window. A file is created to store all the commands entered so that history does not get destroyed even after closing the shell. Argument accepts a numeric value and displays the last K number of commands entered.
working
-
pwd displays the present work directory
working
-
cd [dir (..)/dir] is used to nevigate through the directories
working
-
exit command is used to escape from the shell
working
-
ls [-l optional] command is used to list all the files and sub directories in the present directory
working
-
mkdir creates a directory at the specified, if complete path not specified then creates a directory at pwd with the specified name
working
-
rmdir removes specified directory, if complete path not specified then removes the specified directory at pwd
working
-
env lists all the environment parameters.
working
-
clear clears the shell screen
working
Rest other commands are executed using execvp() commandworking
The kcsh shell also knows how to launch the programs in foreground and background. Using & at the end of the command will run the program in background more. For example try running sleep 3 and sleep 3 & command in the shell and observe the outcome. working
Note : Foreground and background processes not working for pipelined commands
Two kind of parsing is performed. Parsing is performed separately when the input contains pipes and when the input does not contains pipe. Parsing of the input is performed using the strtok() function.
- No functionality to execute previously executed command or to browse the previously executed command using tab and arrow key.
- Commands must be on a single line
- Arguments must be separated by whitespaces
- No quoting arguments or escaping whitespaces
- Multiple pipeline commands not executing properly
This code is in the public domain (see UNLICENSE for more details). This means you can use, modify, and distribute it without any restriction. I appreciate, but don't require, acknowledgement in derivative works.