OpSys Fall 2006 - HW3

HW3 - Unix Shell

Due Date: October 26th at 11:59PM
Submit to WebCT drop box labeled HW3

Late Policy

IMPORTANT: This counts as two homeworks!

- Unix Shell

The objectives of this assignment are:

  1. Experience with fork() and exec() system calls.
  2. Understand how a shell works.
  3. Learning how to use pipes.

You are to write a simple Unix shell (the shell is simple, but this assignment is not!). Your program should read commands from the user (use readline!) one line at a time, and for each line the shell attempts to process the user command.

There are two basic kinds of commands that your shell must handle: built-in commands (that your shell handles itself) and external programs (the shell starts a new process which then executes the external program). The details of how to handle each of these types of commands is shown below. Your shell must support an unlimited number of pipes between external commands, automatically directing the output of one external command to the input of another. Your shell must also support I/O redirection (arranging it so that external commands will read from, or write to, a file). Finally, your shell will use the environment variable OSPATH as a list of directories in which to search for external commands.

- Shell prompt

The prompt output by your shell when asking for a line of input must be the name of the current working directory followed by a space (NOT the entire path, just the name of the current directory). The result should be something like this (sample session in which user input is shown in blue):

fred ls
opsys    proglang    rpi.gif    stolenmusic      
fred cd opsys
opsys ls
hw1   hw2   hw3
opsys 

- Shell internal commands

There are a few commands that your shell must recognize and deal with directly - these commands are listed below. For each of these commands, the shell should not create any new processes! You do not need to be able to support using pipes with any shell internal command.

The actual commands you need to support are shown below, you must use the exact syntax described (otherwise our testing scripts won't work with your shell).

- Command lines involving external commands.

If the user input does not match any of the internal commands listed above, your shell must attempt to find an executable file with the same name as the command, and to run the command. If the command name includes the character "/" - the user is specifying a path to the executable file. If the command name doesn't specify a path - your shell should assume it is a file name, and should use OSPATH to locate the actual executable file.

Your shell must support any number of command line arguments, for example the user could type "ls -al foo.c blah.h" and you need to make sure the ls command receives the command line arguments. Your shell does not need to support any special characters like "*" or "?", so if the user types in "ls *.c" you just pass along the "*.c" to the ls command (a real shell would replace this with the name of all files that end in .c).

Your shell must support an unlimited number of pipes - this means that each command line could include a number of external commands. Note that you do not need to support pipes with internal commands. For each pipe in the command line you need to take care of connecting stdout of the left command to stdin of the command following the "|". For example, if the user types "ls -al | sort", you need to arrange is so that the ls command is run with stdout directed to a Unix pipe, and that the sort command is run with stdin coming from that same pipe.

Your shell must support I/O redirection. If an external command is followed by "<", it must read input from the named file. For example, the command line "sort < foo", means that sort should be fed the file foo via stdin. If an external command is followed by a ">", out must arrange it so that stdout of the command is directed to a file. For example, "ls > save" means that the ls command should save it's output to the the file named "save". It is possible that you could have both on the same command, for example: "sort < infile > outfile".

You do not need to support redirection of standard error.

Your shell should be able to handle something like this:

ls -al | cut -c31- | sort -rn > foo

If the user tries something silly (ambiguous) like this (which indicates two sources of input for the sort command):

ls -al | sort < foo

You can do whatever you want, as long as this doesn't crash your shell. An error message would, of course, be nice...

- Project Requirements

The following are the requirements for the project:

Grading: Your code will be graded according to the formula below. Note that to get full credit we must be able to understand your code (it must be commented!)

20% Internal commands properly implemented, this includes proper parsing of internal commands and proper output generated by the where and print commands.
30% External commands properly implemented, this includes sending command line arguments to external programs.
10% File I/O redirection (support for "<" and ">") works properly. This is only needed with external commands.
20% Pipes work with external commands properly. There should not be any limit (imposed by your program) on the number of pipes in a single command line.
10%Impossible to crash the program. Basically you need to make sure there is nothing we can do (there is no input we can send to your shell) that can cause your program to crash (for example, SEGV). Check the return value of all system calls (check for errors)!. Keep track of dynamically allocated memory (and free anything that is not needed).
10%Code quality (comments, organization, how hard is it to understand ?).

You can get partial credit for any part.

If you code does not compile and run under FreeBSD, you will lose at least 1/2 the relevant points ( partial credit will be awarded based on visual inspection of the code).

- How to Submit

Log in to WebCT at webct.rpi.edu using your RCS id and password. Once you get to MyWebCT click on "Operating Systems", and from there go to the homework drop boxes. Submit your files (individually, zipped or tarred) to the drop box labeled HW3.

-Resources