Custom Shell Program
Custom Shell Program
1 Introduction
This report presents a custom shell program written in C. The program provides a command-line interface
where users can enter commands to be executed by the shell. The shell program supports basic commands,
such as changing directories, list and echo.
2 Code Description
Video Link: https://youtu.be/8vgIPug dS0
The custom shell program is composed of several functions that are responsible for different aspects of the
program. The init() function is responsible for clearing the terminal screen and displaying the current user,
directory, and time.
The argsFormater(bluechar line[]) function formats the input string by removing leading and trailing
white space and checking for the presence of an ampersand () to indicate that a process should be run in the
background. The function also splits the input string into individual arguments and stores them in an array.
The scanner(bluechar line[]) function reads input from the console using the fgets() function and passes
the input to the argsFormater(bluechar line[]) function.
The process(bluechar* args[],bluechar line[]) function processes the input by checking for the CD com-
mand and splitting the input string into individual arguments.
The parser(bluechar* args[], bluechar line[]) function scans input from the user and calls the scanner(bluechar
line[]) and process(bluechar* args[],bluechar line[]) functions to process the input.
The execute(bluechar* args[]) function executes the command entered by the user. If the process is run in
the background, the function sets the flag variable to 1 and executes the process using the execvp() function. If
the process is run in the foreground, the function waits for the process to complete using the waitpid() function.
The signalHandler(blueint signal) function is called when a child process terminates and sets the flag
variable to 0.
3 The Algorithm
The program is separated into small functions:
3.1 main()
The function starts by removing any old logs by calling the remove function with the name of the log file.
It then sets up a signal handler for child processes using the signal function with the SIGCHLD signal and
the signalHandler function.
The fuction declares an array of char called line with a maximum size of MAXCHAR. This array will be
used to store the user’s command line input.
The program then calls the init function, which likely initializes any necessary variables or data structures.
The fuction enters a loop that will continue to read and execute commands from the user until the parser
function returns false. The parser function is not shown in the code, but it likely parses the user’s input into
an array of strings, which are then stored in the args array.
For each iteration of the loop, the program calls the execute function with the args array as its argument.
The execute function likely takes the array of strings and executes the appropriate command based on the user’s
1
input.
3.2 init()
This init() function to be initializing the terminal by displaying some information to the user such as username,
current directory, date, and time. Here are the steps it follows:
-Clear the terminal screen.
-Display a greeting message.
-Get the username using getenv(”USER”).
-Get the current working directory using getcwd() and display it.
-Display the current date and time using time() and localtime().
-Sleep for 2 seconds.
-Clear the terminal screen again.
Overall, this function provides some basic information to the user and gives the terminal a clean and organized
appearance.
2
3.6 parser(char* args[], char line[])
The parser function is responsible for getting input from the user, processing it, and returning a flag indicating
whether the program should continue running. Here’s how it works:
It first prints the prompt to the user, consisting of their username followed by ”¿¿ ”. It then calls the scanner
function, which reads input from the console and formats it into an array of arguments using the argsFormater
function. It then calls the process function, which further processes the array of arguments to handle special
commands like cd and to handle quoted arguments. Finally, it returns a flag indicating that the program
should continue running. Overall, the parser function serves as the main entry point for getting user input and
processing it, allowing the shell to interact with the user and execute commands.
4 Conclusion
The custom shell program provides a basic command-line interface that allows users to execute commands
and run processes. The program demonstrates the use of several C libraries and functions, including stdio.h,
stdlib.h, string.h, unistd.h, and signal.h. The program could be extended to include additional functionality,
such as support for input/output redirection and pipes.
3
5 The source code
4
Figure 2: Code Block 2
5
Figure 4: Code Block 4
6
Figure 5: Code Block 5