Starting from:

$30

CMSC216 Exercise 5 -Solved

Your shell will have a loop that reads command lines and processes them. The prompt for your 
shell is “shell jr: “. The commands your shell must handle are: 
1. exit: When the user enters the exit command, the shell prints the message “See you” and 
terminates by calling exit(). 
2. hastalavista: Has the same functionality as exit. 
3. cd dir: This command changes the working directory to dir. Assume the user always provide 
a directory as an argument. Use chdir() to change the working directory. 
4. A linux command with a maximum of one argument, such as “pwd”, “date”, “wc loca
tion.txt”, “cat location.txt”, etc. 
15 
Requirements 
1. Do NOT use an exec* function to implement the functionality associated with the commands 
exit, hastalavista, and cd. For other commands, create a child (via fork()) and use execvp() 
to execute the command. 
2. If the user provides an invalid command, print the message ”Failed to execute ” followed 
by the command name. In this case the child exits returning the error code EX OSERR. Use 
printf to display the message and flflush the output buffer (eg, fflflush(stdout)). Note that the 
shell does not terminate when given an invalid command. 
3. You don’t need to handle the case where the user just types enter. You can assume the user 
always provides a command. 
4. To print the shell prompt, use printf and flflush the buffer. 
5. It is your responsibility to verify that your program generates the expected results in the 
submit server. 
6. You must use execvp (and no other exec* system call). 
7. Your code must be written in the fifile shell jr.c. 
8. Do not use dup2, read, write, nor pipes. 
9. Do not use system() to execute commands. 
10. Assume a line of input has at most 1024 characters. 
11. Provide a makefifile that builds an executable named “shell jr”. The rule that builds the exe
cutable must have the target “shell jr”. Also have a target named “clean” that removes the 
shell jr executable and any object fifiles. 
12. Your program should be written using good programming style as defifined at 
http://www.cs.umd.edu/~nelson/classes/resources/cstyleguide/ 
13. Common error: If you get the submit server message “Execution error, exit code 126” execute 
“make clean” before submitting your code. 
14. Common error: Forgetting to return the correct value (eg, 0) in your code. 
15. Your shell jr C program does not take command line arguments. Hence the main function 
would have the form (no argc or argv): 
int main() { } 
16. Remember that the argv parameter in execvp is an array of strings (so you wold construct 
an array of strings and pass it to execvp). 
17. Your shell should exit when it detects end of fifile. This explains why public tests do not have 
exit nor hastalvista as the last command. 
18. This exercise relies on Standard I/O, NOT Unix I/O. 
219. When a line is entered into shell jr, it reads at most the fifirst two arguments in the line and 
ignores any other arguments. For example, if command “wc location.txt” is entered, it reads 
two arguments (“wc” and “location.txt”) and successfully executes the command. If “wc lo
cation.txt bla” is entered, it reads two arguments (“wc” and “location.txt”) and successfully 
executes the command. 
20. For exit and hastalavista, ignore any values provided after the command (just exit the shell). 
21. For cd, ignore any values provided after the directory name. For example, “cd /tmp bla” 
would change the working directory to /tmp. 
22. If an invalid directory is provided to the cd command, your shell should print an error 
message: 
"Cannot change to directory INVALID_DIRECTORY_NAME" 
where INVALID DIRECTORY NAME is replaced with the invalid directory name. 
23. Do not use signals. 

How to Start 
Start by creating a loop that reads lines and displays them. Then begin to process each command, 
starting with the exit and cd commands. You are free to process each line any way you want; 
however, reading a whole line using fgets and then processing the line using sscanf could make 
things easier. Keep in mind that if sscanf cannot read a value into a string variable, it does not 
change the variable. This could help you to identify whether a command has an argument. 

More products