Description: The bash shell utility on UNIX and UNIX-based platforms is a command-line shell to access the resources of the operating system via built-in commands, by starting processes to run programs and utilities.
Assignment: In this programming assignment, you will implement a customized simplified version of a Unix Shell called "cssh". There are no built-in programming structures or variables in cssh. The cash shell program should be able to:
Execute programs that are in the current directory or on the $PATH environment variable and that are compiled executables (object files) with permission 'X'set, and handle 10 redirects for programs.
Pass arguments that are separated by spaces and string arguments quoted with single '-quotes, such as 'this is a single argumen, to commands.
Exit on the "exit" command
Change directory with the "cd" command.
Command line options: In your "cssh", it must support the following forms of basic commands:
./program ARG1 ARG2...
Execute program (with permission 'x'), arguments are passed to it
ls DIR
Execute /bin/ls with DIR argument (ls is on $PATH)
exit
Exit from "cssh"
cs DIR
Change directory to DIR
< FILE and > FILE
Handle IO redirects with < and/or > (space < or > and FILE)
Exit code: the "cssh" should exit with EXIT_SUCCESS after the "exit command (its arguments can be ignored).
Helpful reading: to understand the system calls that you will need to make in the C/C++ code for this assignment, which are fork(2), wait(2), execlp(3), execvp(3), signal(3), open(2), close(2), dup(2), dup2(2), please read this first:
Implementing your shell: we suggest the following steps to implement cssh:
First of all, you are not allowed to use system(3) directly anywhere in your code. If your code contains this call, you will receive zero points for this assignment.
Implement cssh using the example code provided herein. The first version of your shell can be directly based on these examples and should be able to handle command execution. Test your implementation before implementing the cd command and IO redirects.
To implement "cd", see the man page of chdir(2) for help.
To implement "> FILE" redirect where there is always spacing between the ">" and FILE, check if the array av [] has a ">" string and if so open the file specified in av [] using open(2) for writing and use dup2(fdout, 1) with this open file descriptor fdout (see the man page of dup(2) and dup2(2)). Then modify the array av[] to remove these two entries from the array, shifting other array elements to ensure the array is not destroyed. Also reduce ac by 2. Do not forget to close(fdout)) in the forked child and parent processes, because we dup-ed fdout.
Likewise, to implement "
You should now be able to handle 10 redirects with the proper adjustment to av[] and ac, such as "sort < somefile1.txt > somefile2.txt" and in fact the IO redirects can be reordered as in a real shell such as "sort > somefile2.txt < somefile1.txt" and even " somefile2.txt sort".
Check that your cash can execute commands such as echo, Is and pwd. Also check that you can execute a local program with ./program and arguments.
Remove strtok(3) and replace it with your own tokenizer function that tokenizes arguments that are spaced apart by space (0x20) and tabs (Ox09). A quoted argument with a'-quote should be stored in av [] as one entry without the '-quotes. A simple trick is to change the line string by replacing white space by \0 and by setting the av[] pointers to the location of each argument in the line string. Now, each array argument is a \0-terminated string. Do something similar with each '-quoted argument.
Implement a pipe "I" between two commands using pipe(2) to create two pipe descriptors, such that the first is dup-ed as 1 for the first command that is forked+execvp-ed and the second is dup-ed as O for the second command that is forked+execvp-ed. Note that you should execute two fork() and two execup() and pass the argument array av[] to both but in a way that ensures that each command receives its portion of the arguments. IO redirects may not work yet, since your code must now extract two sets of these as well
After implementing the pipe(2) call, the dup2(2) calls, and changes to split av[] up to two parts to pass to the two commands, check that cat somefile1.txt | sort works as expected.
Change your code to implement 10 redirects for both commands. You may assume that the first command has no ">"redirect and that the second command has no "<"redirect. Check that cat < somefile1.txt | sort > somefile2.txt works as expected.
. You can expect spacing to surround"|".
Test if argument parsing works by checking you can execute echo Hello'world !! Bye! which has a quoted argument, such that the wide spacing in the argument is preserved when passed to echo.
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference
and should not be submitted as is. We are not held liable for any misuse of the solutions.
Please see the frequently asked questions page
for further questions and inquiries.
Kindly complete the form.
Please provide a valid email address and we will get back to you within 24 hours.
Payment is through PayPal, Buy me a Coffee
or Cryptocurrency.
We are a nonprofit organization however we need funds to keep this organization operating
and to be able to complete our research and development projects.