CSE 451
Introduction to Operating Systems

Autumn 1998


Project 2 : A Simple UNIX Shell

Out: Friday, October 23
Due: Monday, November 2

This is a group assignment.


Introduction:

In this assignment you are required to program a simple UNIX shell. A shell is a user-level program that allows users to type in commands and execute and control programs. sh, ksh, csh, tcsh, bash etc. are examples of shells. It might be useful to take a look at the manual pages of these shells (to obtain the manual page of csh, type: man csh).

Every shell prints out a prompt, reads a line of input, parses it into the program name and the parameters, fork's off an new process, exec's the program and wait's for the program to complete. It does this in a loop. Although most of the commands, you type on the prompt, are names of other UNIX programs (like ls, more etc.), shells recognize some commands (called internal commands) which are not program names. Examples are exit (which terminates the shell), cd (change directory) etc. The shells directly make the system calls to execute these commands.

For example, when  /bin/ls mydir1 mydir2 is typed at the prompt of csh, the command line is seperated into the program name /bin/ls, and two arguments: mydir1 and mydir2. The shell then forks off a new process and the child execs /bin/ls and passes it the arguments mydir1 and mydir2. The parent process waits (by executing a wait() call) for the child to terminate. The /bin/ls program prints out the directories and exits. Then the parents process continues, prints out a new prompt and waits for the next command.

If an internal command, like cd mydir is typed, then the shell calls the system call chdir(mydir) to change the current directory of the shell, prints out a new prompt and waits for the next command. Therefore fork() and exec() are not used for internal commands.


Task:

Write a shell program in C or C++ which has the following features:
  1. It should parse the parameters and pass in the parameters to the exec'ed program.
  2. It should recognize two internal commands exit and cd. exit terminates the shell, ie, the shell calls the exit() system call or returns from main. cd uses the chdir system call to change to the new directory.
Assume that the full names like /bin/ls are given. So you need not implement search paths etc. Also try to use the same prompt as given below. This will help partially automate the grading.

The output produced by your shell should look like the following:

CSE451Shell% /bin/cat /etc/motd
DEC OSF/1 V3.2 (Rev. 214); Thu Feb 22 08:48:40 PST 1996
DEC OSF/1 V3.2 Worksystem Software (Rev. 214)

This is an AFS fileserver.  Please run long running jobs (hours) or memory
intensive jobs elsewhere.

CSE451Shell% /bin/date
Sun Apr  5 22:51:50 PDT 1998

Note: The words in bold are output by the shell and the words underlined are typed in by the user.
Please take a look at the manual pages of  execv, fork and wait.

To allow users to pass arguments to programs you will have to parse the input line into words separated by whitespace (spaces and'\t' tab characters) and place these words into an array of strings. You might try using strtok() for this (man strtok for a very good example of how to solve exactly this problem with strtok). Then you'll need to pass the name of the command as well as the entire list of tokenized strings to one of the other variants of exec, such as execvp(). These tokenized strings will then end up as the argv[] argument to the main() function of the new program executed by the child process. Try man execv or man execvp for more details.


What to turn in:

  1. The C/C++ files of your shell implementation.
  2. A Makefile which compiles your code into a program called mysh when you execute make.
  3. A README file that describes the features of your shell and documents your design and implementation decisions.


How to turnin:

You have to use the turnin program on orcasor sanjuan to turn in your project.

To turn in your solution, use the following:

turnin -c cse451 -p project-2  <all the files>

To submit all the files in the current directory:

turnin -c cse451 -p project-2 *

Remember to use only orcas or sanjuan to turnin files.