Assignment 5, Write a Shell Program

- Put your program in a file called mysh.cpp.

  1. ( 95pts) Write a C++ program , "mysh.cpp". This program should:

    1. Print a dollar sign prompt ($) and then read a line from standard input.
    2. When the user types in a name, which can be assumed to be the name of an executable file or command, your program should create a process that executes that program file or command. If the user types 'logout', your shell should terminate. If the user enters an empty line, your shell should skip this input and print the prompt again. (Hint: use execlp. You may assume the user program takes no command line arguments.)
    3. After the user program has executed, mysh should again print the $ prompt and wait for input.

    You should test your program and submit only the source code separately from the remaining questions.

  2. (optional; 5pts) Extend your mysh.cpp shell so that it takes an arbitrary number of command line arguments.

    Hints:

    1. Lookup the execvp system call in the unix manual pages.
    2. Use the stringutil.h and stringutil.cpp files discussed in class to get the command line arguments in a vector.
    3. You can convert a C++ string to a C-string like this:
              #include <string>   // for C++-strings
              #include <string.h> // for C-string functions
      
      	char * string2cstr(const string& s)
              {
                 char * result;
      
      	   result = strdup( s.c_str() );
      	   return result;
              }