CS330 Project (Fall 2005)


Part 1--worth 8% (Due November 9)

Design and implement a "myshell" command interpreter to provide the following commands:
(For part 1, you will *NOT* use exec to execute utility programs)

Click here for more submission and marking scheme details


Part 2--worth 7%(Due December 2)

Modify the myshell program that you produced in assignment #1 to include the following features:

Click here for more submission and marking scheme details


The following are meant as suggestions to aid you in completing this project.

To start off with, you will need a way of reading from the keyboard. Once the user types a newline character ('\n'), you can process the entire line. You can do that using the read system call. The following code might help you get started:

#include <cstdio>   //for printf
#include <sys/types.h>
#include <sys/dir.h>
#include <sys/stat.h> //needed to get file info
#include <pwd.h>      //needed for getting user name
#include <grp.h>      //needed for getting group id
#include <time.h>     //needed for converting time
#include <unistd.h>
#include <cstdlib>
#include <cstring>
#include <wait.h>     //needed for wait 

using namespace std;

#define MAX_ARGS_PER_CMD 10
#define MAX_ARG_SIZE 256

int parseBuffer (char *buf, char **target, int maxArgs);

int main (int argc, char* argv[])
{
     char *myArgv[MAX_ARGS_PER_CMD]; //to be used with parseBuffer 

     char buffer[MAX_ARG_SIZE];    //one line of input
     int numChar=0; 
	   int numArgs=0;

     char prompt[130];

     strcpy(prompt, "mysh.");

     write(1, prompt, strlen(prompt));
     while (read(0, &buffer[numChar], 1)!=0)
     {
        if (buffer[numChar]=='\n')
        {
            buffer[numChar]='\0';
            write(1, buffer, numChar);  //just display the line that was typed --for debugging
            //separate the command line into all the arguments by calling parseBuffer--this is useful for Part 2
            //check for "EXIT".  If typed, exit loop
            //else process the command

            write(1,"\n", 1);
            numChar=-1; //start at the beginning of the buffer
		      write(1, prompt,strlen(prompt));
        }
        numChar ++;
        
     }
}


int parseBuffer(char *buf, char **target, int maxArgs) 
{
     int ctr=0;
     char * temp = strtok(buf, " ");
     while(temp !=NULL)
     {
               if (ctr >= maxArgs)
               {
                       return -1;
               }
               target[ctr] = temp; 
               ++ ctr;
               temp = strtok (NULL, " ");
     }
     target[ctr]=NULL;
     return ctr;
}

Some commands that may come in handy. You may find other solutions:

String functions

For turning id numbers into strings and the ctime into a string:

The following code is useful for reading the files in the current directory

#include <sys/types.h>
#include <sys/dir.h>
#include <cstdio>

using namespace std;
main()
{
      DIR *dirp;
      struct dirent *direntp;

      dirp = opendir( "." );
      while ( (direntp = readdir( dirp )) != NULL )
           (void)printf( "%s\n", direntp->d_name );
      closedir( dirp );
      return (0);
}

Another way that you can get all of the entries in a directory is by using getdents.

Our system administrator, Robert Cowles, has provided the following code