CMSC 216 Exercise #5 Spring 2019
Shell Jr (”Shellito”) Due: Tue Apr 23, 2019, 11:30PM
1 Objectives
To practice fork() and exec by implementing a very simple shell.
2 Overview
The first thing you need to do is to copy the directory shelljr we have left in the grace cluster under the exercises
directory. Remember that you need that folder as it contains the .submit file that allows you to submit.
3 Specifications
For this exercise you will implement a very simpliflied shell. A shell is a C program that executes commands
by forking itself and using exec. We have been using tcsh, but there are other shells like ksh, sh, bash, etc. The
name of our shell is shell jr .
Unlike other assignments for this course, you can work together with other classmates, but you may NOT
exchange any code.
4 Shell Jr Functionality
Your shell will have a loop that reads command lines and process them. The prompt for your shell will be
”shell jr:”. The commands your shell must handle are:
- exit – When the user enters the exit command the shell will stop executing by calling exit(). Before
executing exit, the shell will print the message”See you”. - hastalavista – Has the same functionality as exit.
- cd – This command changes the current directory. You can assume the user will always provide a directory
as an argument. - A command with a maximum of one argument (e.g., wc location.txt). That means your shell should be
able to handle commands like pwd, date or wc location.txt . - Requirements
- You must NOT use an exec* function to implement the functionality associated with the commands
exit, hastalavista, and cd. For other commands, you must create a child (via fork()) and use execvp() to
execute the command. - If the user provides an invalid command, the message”Failed to execute”followed by the command
name should be printed. In this case the child will exit returning the error code EX OSERR. Use printf
to display the message and flush the output buffer (e.g., fflush(stdout)). Note that the shell is not terminated
by executing an invalid command. - You don’t need to handle the case where the user just types enter (you can assume the user will always
provide a command). - Make sure you use printf to print the shell prompt and that you flush the buffer.
- It is your responsibility to verify that your program generates the expected results in the submit server.
1 - You must use execvp (and no other exec* system call).
- Your code must be written in the file shell jr.c.
- You may not use dup2, read, write, nor pipes.
- You may not use system() in order to execute commands.
- You can assume a line of input will have a maximum of 1024 characters.
- Provide a makefile that builds an executable called shell jr. Name the target that builds the executable
shell jr . Feel free to add any other targets you need. - All your C programs in this course should be written using the compiler gcc, with the options defined
in the gcc aliases info.txt file. This file can be found in the info folder of the public grace account. - Your program should be written using good programming style as defined at
http://www.cs.umd.edu/~nelson… - Common error: If you get the submit server message”Execution error, exit code 126”execute”make
clean”before submitting your code. - Common error: To forget to return the correct value (e.g., 0) in your code.
- Your C program representing your shell does not take command line arguments. That is, the main
function is defined as:
int main() {} - When you see that execvp relies on argv that means it uses an array of strings (argv is not the parameter
associated with command line arguments). Just initialize argv with an array of strings and pass it to
execvp. - Your shell should exit when end of file is seen. This explains why public tests do not have exit nor
hastalvista as the last command. - ShellJr exercise relies on Standard I/O, NOT unix I/O.
- ShellJr takes a maximum of two arguments (e.g., wc location.txt). You can ignore any other values
provided after the second argument. For example, if someone enters wc location.txt bla bla will be
ignored and the command will be processed successfully. - For exit and hastalavista you can ignore any values provided after the command (just exit the shell).
- For cd you can ignore any values provided after the directory name. For example, cd /tmp bla will
change the current directory to /tmp. - If an invalid directory is provided to the cd command, your shell should print an error message similar
to:
“Cannot change to directory <DIRECTORY_HERE>” - Do not use signals.
2 - How to Start
You should start by creating a loop that reads lines and displays them. Then you should 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
simpler. Keep in mind that if sscanf cannot read a value into a string variable, it will not change the variable.
This could help you identify when a command has an argument or not. - Submitting your assignment
- In the assignment directory execute the command submit.
- Your assignment must be electronically submitted by the date and time above to avoid losing credit. See
the course syllabus for details. - Grading Criteria
Your assignment grade will be determined with the following weights:
Results of public tests 28%
Results of release tests 72% - Academic integrity statement
Please carefully read the academic honesty section of the course syllabus. Any evidence of impermissible
cooperation on assignments, use of disallowed materials or resources, or unauthorized use of computer accounts,
will be submitted to the Student Honor Council, which could result in an XF for the course, or suspension
or expulsion from the University. Be sure you understand what you are and what you are not permitted
to do in regards to academic integrity when it comes to assignments. These policies apply to all students, and
the Student Honor Council does not consider lack of knowledge of the policies to be a defense for violating
them. Full information is found in the course syllabus– please review it at this time.