Smart Shell, Part II

You may work alone or with one other classmate, and each person is expected to contribute equally to the effort.

Introduction

Standard system function

The standard C language in Unix provides a function system with the prototype
    int system(const char * command);
This function executes the command specified as argument by invoking the Unix shell. The shell executes the command in the background, and passes the result to the calling program (-1 in case of an error, the value returned by the main function of program specified as command otherwise). To be able to use the system function, you need a working shell in the first place. Given that our ultimate goal is to write a shell from scratch, we'll have to assume that the system function is unavailable to us.

Your own mysystem function

The first goal of this assignment is to write your own mysystem function with the prototype:

   void mysystem(const char * command);
The mysystem should execute the command supplied as an argument. If the command fails, the function should print an error message. Standard C offers a system function called execv that executes the command specified as the first argument:
    execv(char * fullcmd, char * cmdargv[]);
Here fullcmd is the full path for the file (command) to be executed, and cmdargv is an array of strings representing the arguments for fullcmd. By convention, the first string in cmdargv is the name of the file to be executed. The function execv returns to the calling program only if there is an error, such as not being able to find the executable. After a successful execv, there is no return to the calling program.

In building the full command for execv, you will make use of the global variable searchpaths built by the function tokenize_PATH from your previous assignment.

What To Do

  1. In your systems directory there is another directory called smartshell. Make a copy of this directory using the command
        cp -r ~/systems/smartshell ~/systems/smartshell-one
    
    Verify that the new directory has been successfully created (use ls). The solution to this assignment should be in the directory ~/systems/smartshell.

  2. Add to smartshell.c code for a new function
       void mysystem(char * command)
    that mimics the behavior of the standard system function. Your code should implement the following steps:

    Note: You may not use calls to execvp or exceve in implementing the mysystem function.

  3. Test out the functionality of the mysystem function before moving on to the next step. For example, you may comment out the code for the existing main function, and simply add to main one line of code similar to
        mysystem("ls -a"); 
    The output should be identical to the one obtained by typing ls -a at the shell prompt.

  4. Once you have convinced yourself that the mysystem works as expected, you may use it in the main loop from the previous assignment. Invoke mysystem on all user commands other than path, where and quit (which should be handled as in the previous assignment).

  5. Compile as in the previous assignment using the command
        gcc smartshell.c stringarray.c -o xsmartshell
    
    The two source files will be linked into one executable file called xsmartshell.

    To simplify your development process, you may use this Makefile and simply type in make at the shell prompt to compile your code. This saves you from typing in a long command line every time you need to compile your code.

    Makefiles are special format files that help build and manage projects composed of multiple modules. The key point to remember is that you need to insert a TAB character (not spaces) before each command in the Makefile. If interested, you may read more about using make and writing Makefiles.

Important Notes

Submission Instructions

In your smartshell directory create a text file named readme (not readme.txt, or README, or Readme, etc.) that contains:
  1. Your name(s) and assignment number.
  2. The machine you used to complete the assignment (i.e, tanner or your own machine). If you used your own machine, please be ready to demo your code to your TA or your instructor at any time upon request.
  3. A description of whatever help (if any) you received from others outside of class.
  4. An indication of how much time you spent doing the assignment outside of class.
  5. Your assessment of the assignment: Did it help you to learn? What did it help you to learn? Do you have any suggestions for improvement? Etc.
  6. Any information that will help us to grade your work in the most favorable light. In particular you should describe all known bugs.
Descriptions of your code should not be in the readme file. Instead they should be integrated into your code as comments. Your readme file should be a plain text file. Do not create your readme file using Microsoft Word or any other word processor.

Turn in a printout copy of the readme and smartshell.c files, and a sample output (one printout per team). If you work on the department Unix system, you will need to copy and paste your code into a text or a Word document in Windows, since you cannot print directly from Unix.

Leave the source code for all exercises in your directory systems/smartshell. Do not make any changes to these files after the due date for this assignment. If you wish to continue working on these exercises after the due date, make a copy of your directory smartshell using the following Unix command:

    cp -r ~/systems/smartshell ~/systems/smartshell-copy2
A new directory called smartshell-copy2 will be created in your systems directory. You may now make any changes you want to the files in your smartshell-copy2 directory, at any time.

Grading

No credit will be given for code that does not compile.
100     Total points possible
 
20 Program compiles and runs without crashing
20 Each command does something resembling the specified action
45 The mysystem function works correctly and completely
  5 You have added some extra "bells and whistles" (use your imagination)
  5 Submission includes readme file
  5 Submission includes sample output

Have fun!