CS321 01                                             Assignment 1
Due 10/01/2014 

Part I: Run commands on CS1

Run the following UNIX command with at least one parameter if possible (for example “ls –l”). You can use man page to read the command manual (for example “man ls” to read the reference manual of command ls). You may switch to other shell other than default shell sh (type $bash or $ksh) to run the commands.

 

passwd

cat

cp

pico

ps

grep

history

logout/exit

 

Save your run results in a text file called unix-command-analysis.txt. Submit the file via Drop Box on the class Sakai web site before the due day.

 

Part II: OS Shell Project on cs1

Write a C/C++ program to simulate a simple shell (command interpreter) on CS1 UNIX (Solaris).
The program operations will be used to illustrate the process creation, synchronization, and execution using FORK, WAIT, and SYSTEM Unix commands The shell should be called MSH (short for My SHell) and its initial prompt should be "=>".

1. MSH should first prompt user for the login name and password. The list of login names and passwords can be stored either in a table or in an external file.
 MSH will continue with a new "msh=> " prompt if the login is successfully.

 - The password stored in the table or external file must be encrypted. You should use the crypt(…) function to encrypt the password.

2. MSH should then read the command and parameters entered by the user, where the syntax of commands is:

 command [parameter | [parameter]*]   /* [] means optional, | means or, * means 0 or more */

 MSH will analyze and run the command. If the command is one of the following commands supported by the MSH, then it forks a new process to execute the command. Otherwise it prints the error message to indicate that the command is not a valid one.

The MSH commands should be initially stored in an external file and read into a command array (or a hash table) when MSH starts.

 The 7 commands supported by the MSH are:

 mypwd /* Change the password – Write your own MSH function to change the password */
 mycp fileName1 fileName2 [fileName3]
     /* Copies the contents of fileName1 and fileName2 onto the screen
         or copy fileName1 and fileName2 to fileName3 if fileName3 is provided. You may use the UNIX command “cat” or “cp” to copy the files*/
 myedit [subdirName].fileName
    /* edit an existed file in the current directory or in a subdirectory if the subdirName is provided. You may use the UNIX command “pico or vi” to edit the file */
 myps [loginName] /* display the status of all current processes, or display the processes belonging to the user if the loginName is provided. 

        You may use the UNIX command “psef | grep loginName” to display processes status */

mysearch word fileName
     /* search a word in a file. Print the lines where the word is found.

        You may use the UNIX command “grep” to search a word in the file */
 myhistory /* list of all previous commands executed - Write your own MSH function to list the history */
 mylogout /* the MSH should exit the shell after other commands are completed. */

(Extra credit:  Program the upper arrow key to list the previous command executed.)

3) MSH should fork a child process to execute the command. The parent process will wait for the termination of the child process before go back to the "msh=>" prompt if the command is in a foreground process, otherwise it simply go back to the "msh=>" prompt if the command is in a background process.
 - The command should be executed as a background process if a “&” sign is followed at the end of the command.

4) The main() function of your program should follow the program structure listed below:

build_command();       /* read in the commands into a table or hash table */

user_login();                /* Authenticate the user */

while (i < N) {              /* repeat maximum N times */

    type_prompt( );        /* display prompt */

    n = read_command (command, parameters, background)            /* input from terminal */

    if (n>0) { /* valid command */

            if (pid = fork() != 0) { /* Parent code */

              if (!background)  {

                pid = wait( &status); /* wait for child to exit */

                if (status == LOGOUTCODE) exit(0); /* status must be a hex number */

                               /* For example: LOGOUTCODE is 0x0500 is child terminated with the command exit(5) */

             } /* end of parent code */

            } else { /* Child code */

              exec_command (command, parameters);      /* execute command */

              exit(0);

            } /* end of child code */

   } else { cout << “Invalid command, try again\n”; }

   i++;

 }

 

Testing Requirements:
  Test every command supported by the MSH in a foreground process. Test just one command in a background process.

Assignment Hand-in Policies:
   1) Write a Microsoft word report that describes your program structure (Good program modular design, data structures, and internal documentation are part of the grade), problems that you have encountered and solved (or not solved), and run results.
       Attach the screen shots of your run results on your report.
   2) Submit your report and program source code via Drop Box on the class Sakai web site before the due day.