CS-701 Assignment 3

Introduction

This is the first version of a project we will be developing throughout this semester: to write an interactive shell, akin to sh, csh, bash, etc. In the spirit of Unix shell-naming, we will call our shell, ouch.

This first version of the shell won't actually be an interactive program. Instead, ouch will execute a "sub-command" that the user enters on the ouch command line following a -c command line option.

For example, the following would be a valid invocation of ouch:

      % ouch -c ls -l
In this example, the '%' is the user's shell's prompt, ouch is the name of the command, -c is the -c command line option, and the sub-command to be executed is "ls -l".

Requirements

Write a C++ program named ouch, which accepts the command line option "-c" followed by a sub-command line. Your program is to print the full pathname of the executable file given as the name of the sub-command, or an error message if such an executable file cannot be found. If the executable file is found, build the parameters for the execve() system call and execute the sub-command. If the execve() command fails, print an appropriate error message.

If the user enters any option other than "-c" or omits the "-c" before the sub-command line, it is an error, and your program is to print an error message showing proper invocation syntax, and exit. However, if the user gives no command line arguments at all, the program is to exit normally, with no error message.

Your program must be coded according to the [ Coding Guidelines ] for this course. This includes the requirement that the program must compile and link with no error or warning messages, in addition to running correctly. It also includes the requirement that the program must be properly documented.

You are to use RCS to manage the files in this project. For this assignment, that means the files named Makefile and ouch.cc. Be sure both files contain proper $Id$ and $Log$ RCS macros in appropriate places. (See the Coding Guidelines if in doubt.)

Because the project uses only a single source module, ouch.cc, there is no need for a make rule for building the executable file, ouch. Thus, in addition to comments and variable definitions, the Makefile for the assignment is to include only a rule for the target "clean" like the following:

      clean :
      <tab> rcsclean
      <tab> rm -f core ouch

Project Management Requirements

Create a project directory for the assignment as described in the coding guidelines. Write the code as a single source module named ouch.cc, which you are to compile into an executable file named ouch using the following command line:
      make ouch
The above command must be expanded by make to the following command, which must produce no warning or error messages:
      g++ -g -Wall -Wwrite-strings ouch.cc -o ouch
This example assumes that ouch.cc is already in the project directory. If it and the Makefile are absent, make will check them out first, and remove ouch.cc at the end.

Note that the following variables must be defined either in your Makefile or in your environment:

        CC = g++
        CXXFLAGS = -g -Wall -Wwrite-strings
When I test your program, they will both be defined in my environment.

Coding Requirements

Your code must use the following functions to perform the indicated tasks:

getopt() To process command line options.
perror() To print any error messages due to system call failures.
fprintf() To print error messages to stderr, other than the ones that perror() handles.
getenv() To get the value of the PATH environment variable.
strtok() To divide the value of the PATH environment variable into a list of directories.
stat()
getuid()
getgid()
To determine if a file exists, and if the user has permission to execute it.
execve() To execute the file.

Due Date

Test your program carefully before creating the tar file and submitting it.

Create a clean project directory (a directory containing only the RCS subdirectory, with Makefile,v and ouch.cc,v in the RCS subdirectory under RCS control), put the project directory into a tar file, and send the tar file to me as an email attachment by midnight October 18. Note: when I extract the contents of your tar file, I should get a subdirectory under the current directory for your project directory, and an RCS subdirectory under that.

Be sure the Subject line of your email message says, "CS-701 Assignment 3."