Shell Program Exercise

Due Date:
February 27, 1996
Deliverables:
When the project is complete, send me an e-mail message telling me the path to the project directory for this exercise. The project directory shall contain three source files, a Makefile, and nothing else. In addition, the directory ~/man/man1 is to contain a man page for the executable program. You may leave a README file in the project directory if you think that one is needed, but I neither expect nor require you to do so.
Requirements:
In addition to the information given here, be sure to consult the Grading Form for this assignment, which includes additional information about the requirements for this project.

Project Description

Your assignment is to modify and extend Program 1.8 from the Stevens textbook. First create a project directory to work in and get a copy of the program, the header file, and the definition file for the functions err_ret() and err_sys() from ~vickery/CS-701/APUE. You will find Program 1.8 and ourhdr.h in proc/shell2.c, and error.c in lib.sun.

Do the exercise as a sequence of steps. Most of you will have done some or all of Steps 2 and 3 before you got the assignment, so you will have to go back and do Step 1 out of sequence.

  1. Copy the default Makefile from your home directory to the project directory, use the command, "chmod 600 Makefile" to make it writeable, and add the following lines to the end of it:
    OBJS = shell2.o error.o
    
    myshell :  $(OBJS)
            $(CC) $(CFLAGS) $(OBJS) $(LDLIBS) -o $@
    
    Be sure the last line is indented using a <tab> character, not spaces. You should now be able to build the executable shell2 program by issuing the command, "make".

  2. Edit shell2.c so that it follows the Coding Guidelines for this course. You do not have to edit the other two files.

    1. Add file header and function header comment blocks.
    2. Fix the program so that it behaves "reasonably" if the user enters an empty command line.
    3. Make calls to exit() meet the specifications of the Coding Guidelines.
    4. Fix anything else you can spot.

    Be sure the program still works!

  3. Change the program so that the user can enter command line arguments, like "ls -l a*". You may place a "reasonable" limit on the number of command line arguments the user may enter.

    To do this, you will have to break each command line into a sequence of space-separated tokens using the strtok() standard library function. There is sample code to do this in the Harbison and Steele book listed in the CS-701 Course Description.

    It would be very hard to use the execlp() function to handle command line arguments. You would have to have a separate call for each of the different number of number of arguments the user might enter! Instead, look up the execvp() function in the man pages, and use that. You will save each of the pointers returned by strtok() in an array, and pass the array as the second argument to execvp().

    Extra Credit: Handle command line arguments in quotes properly.

  4. As it stands, typing ^C when the program is running causes the "interrupt" message to be displayed, a new prompt to be issued, and the program exits. Fix the program so it continues to accept command lines instead of exiting.

    To do this, you will have to check to see if the global variable errno is set to EINTR (defined in errno.h) when the fgets() function returns NULL. If so, repeat the fgets() instead of exiting.

    Extra Credit: Make the program contine to accept commands no matter how many times the user types ^C.

  5. Write a man page for the new shell program. It may be very brief, but it must be in standard man page format, and it must be installed properly. Consult the man page handout for this course and/or the man entry in Section 5 of the on-line man pages for information on how to mark up the text of your man page.



Christopher Vickery
Queens College of CUNY