[SOLVED] CSC60 Lab 9-Write your own UNIX Shell

35.00 $

Category:
Click Category Button to View Your Next Assignment | Homework

You will receive the following solution file(s) instantly after successful payment:

zip file icon Lab-9-daxgkg.zip (151.8 KB)
Assignment Instructions Updated Recently? Submit Below and we will provide new Solution!
Submit New Instructions
🔒 Securely Powered by:
Secure Checkout
Rate this product

 

PURPOSE: The purpose of this lab is to allow students to learn a user interface aspect of a UNIX shell. Student will work with some basic system calls.
Important note: please use sp1, sp2, sp3, or atoz servers for this lab.

UNIX Shell

We will start with 3 built-in commands:

  •   exit (exit shell)
  •   pwd (print working directory)
  •   cd (change directory)

    The built-in functions are not executed by forking and executing an executable. Instead, the shell process executes them itself.

    Your shell is basically an interactive loop:

  •   it repeatedly prints a prompt “csc60msh > “,
  •   parses the input, executes the command specified on that line of input,
  •   and waits for the command to finish.

    FILES TO COPY:
    To get the file you need, first move to your class folder by typing: cd csc60
    Type: cp -R /gaia/home/faculty/bielr/files_csc60/lab9 .
    Spaces needed: (1) After the cp and after the -R ↑ Don’t miss the space & dot.

    (2) After the directory name at the end & before the dot.
    After the files are in your account and you are still in csc60, you need to type: chmod 755 lab9 This will give permissions to the directory.
    Next move into lab9 directory by typing: cd lab9
    Type: chmod 644 lab9.c
    This will give permissions to the file.
    Your new lab9 directory should now contain: lab9.c

    Please follow these steps:

  •   Review the source codes, compile, and execute the programs. Examine the output texts to understand the behavior of each program.
  •   I have provided you with a shell template (lab9.c) that you can work from. You build your program into it. Read the existing code closely to identify the key components and understand its execution flow. You can compile the shell using the following command: gcc lab9.c
  •   Function main. Handling built-in Commands: There are three special cases where your shell should execute a command directly itself instead of running a separate process.

o First,Additionally,wemustdealwiththefactthatausermightjusttypeanEnter Key with no command.

o Second,iftheuserenters”exit”asacommand,theshellshouldterminate.
o Third,iftheuserenters”pwd”,printthecurrentworkingdirectory.Thiscanbe

obtained with getcwd() function. Page1of6.

CSC 60. Fall 2019. Lab 9. Write your own UNIX Shell, part 1.

o Fourth,iftheuserenters”cddir”,youshouldchangethecurrentdirectoryto”dir” 0by using the chdir system call. If the user simply types “cd” (no dir specified), change to the user’s home directory. The $HOME environment stores the desired path; use getenv(“HOME”) to obtain this.

Pseudo Code (Highlight indicates provided code.) /*———————————————————-*/ int main (void)
{

print the prompt(); /* i.e. csc60msh > , Use printf*/ fgets(cmdline, MAXLINE, stdin);

/* You have to write the call. The function itself is provided: function parseline */ Call the function parseline, sending in cmdline & argv, getting back argc

/* code to print out the argc and the agrv list to make sure it all came in. Required.*/ Print a line. Ex: “Argc = %i”
loop starting at zero, thru less than agrc, increment by one.{

printeachargv[loopcounter] [(“Argv%i=%s\n”,i,argv[i]);] }

/* Start processing the built-in commands */ if ( argc compare-equal-to zero){

/* a command was not entered, might have been just Enter or a space&Enter */

continue to end of while(TRUE)-loop }

// next deal with the built-in commands
// Usestrcmptodothetest
// if(strcmp(argv[0],”exit”)==0) //Sampleofcompleteline.
// aftereachcommandexceptfor“exit’,doacontinuetoendofwhile(TRUE)-loop if (“exit”)
{

issue an exit call }

 the continued Else-IF is on next page….

else if (“pwd”) {

declare a char variable array of size MAX_PATH_LENGTH to hold the path Page2of6.

while (TRUE)

{

//

int childPid;

char *cmdLine;

CSC 60. Fall 2019. Lab 9. Write your own UNIX Shell, part 1. (MAX_PATH_LENGTH is in a #define…. look for it)

do a getcwd print the path continue

}
else if (“cd”) {

(May skip error checking here)

declare a char variable dir as a pointer (with an *) iftheargcis1
{

use the getenv call with “HOME” and
return the value from the call to variable dir

} else {

variable dir gets assigned the value of argv[1] }

execute a call to chdir(dir) with error checking. Message = “error changing directory”

continue }

//…………………..IGNORE……………………

//

// else {

/* Else, fork off a process */

//

// switch(pid)

pid = fork();

//

{

//

case -1:

//

perror(“Shell Program fork error”);

//

exit(EXIT_FAILURE);

//

case 0:

//

/* I am child process. I will execute the command, */

//

/* and call: execvp */

//

process_input(argc, argv);

//

break;

//

default:

//

/* I am parent process */

//

if (wait(&status) == -1)

//

else

perror(“Parent Process error”);

//

//

printf(“Child returned status: %d\n”,status);

//

break;

//

}

/* end of the switch */

//

}

/* end of the if-else-if */

Page3of6.

CSC 60. Fall 2019. Lab 9. Write your own UNIX Shell, part 1.

//…end of the IGNORE above…………………….

}

/* end of the while(TRUE) */

}

Partnership

Students may form a group of 2 students (maximum) to work on this lab. As usual, please always contact your instructor for questions or clarification. Your partner does not have to attend the same section.
All code files should include both names.

Using vim, create a small name file with both of your names in it. When you start your script file, cat that name file so both names show up in the script file.
You must BOTH submit your effort. As both of your names occur on everything, when I or another grader find the first submission, we will then give the same grade to the second student.

Useful Unix System Calls ( ):

/* end of main */

Also see PowerPoint Slides named

Lab9 Slides

getenv: get the value of an environment variable

path = getenv(“PATH”);

cwd = getenv(“PWD”);

getcwd: get current working directory.

chdir: change the current working directory (use this to implement cd)

Compilation & Building your program

The use of gcc is just fine. If you want to have the executable have a different name from a.out, type:

gcc –o name-of-executable name-of-source-code Marks Distribution

Lab 9 with the built-in commands is worth 38 points.

Hints

Writing your shell in a simple manner is a matter of finding the relevant library routines and calling them properly. Please see the resources section below.

Page4of6.

CSC 60. Fall 2019. Lab 9. Write your own UNIX Shell, part 1.
Keep versions of your code. This is in case you need to go back to your older version due to an

unforeseen bug/issue.

A lot of code to be used in Lab10 is currently commented out. Leave it there. At the start of Lab10, you will receive instructions about which comment marks you should remove.

C Library functions:

if(strcmp(argv[0], “exit”) == 0) strcmp(argv[0], “cd”) strcmp(argv[0], “pwd”) strcmp(….,”>”)

strcmp(….,”<“)

Print a system error message: perror(“Shell Program error\n”);

Input of characters and strings:

fgets(cmdline, MAXLINE, stdin);

// Sample of complete line.

#include <string.h>

String compare:

int strcmp(const char *s1, const char *s2);

This line is in #include <string.h> & does not need to be duplicated in your code.

Preparing your script file:

When all is well and correct, type:

  1. script StudentName_lab9.txt
  2. If you are on a team, cat your name file here.

At the prompt, type:

  1. gcc lab9.c -Wall to compile the code
  2. a.out to run the program

Enter in sequence:

  1. the Enter Key,
  2. a Space, and then the Enter Key,
  3. pwd
  4. cd ..
  5. pwd
  6. cd lab9
  7. pwd

Page5of6.

CSC 60. Fall 2019. Lab 9. Write your own UNIX Shell, part 1.

12. cd
13. pwd
14. exit (exit from the script) 15. exit (exit from the shell)

If all worked, submit your two files to Canvas.

Deliverables

Submit two files to Canvas. No zip files please. 1. lab9.c

2. YourName_lab9.txt

Page6of6.

  • Lab-9-daxgkg.zip