Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with writing a simple shell in C. The image below is the psuedocode that the program must follow. The code underneath it

I need help with writing a simple shell in C. The image below is the psuedocode that the program must follow. The code underneath it is the code that was provided and thus will need to be filled in according to the psuedocode in the image. The text in bold are comments made by the instructor. All non-bolded text is part of the code that must be used.

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.

image text in transcribed

/* This is lab9.c the csc60mshell

* This program serves as a skeleton for starting for lab 9.

* Student is required to use this program to build a mini shell

* using the specification as documented in direction.

*/

#include

#include

#include

#include

#include

#include

#include

#include

#define MAXLINE 80

#define MAXARGS 20

#define MAX_PATH_LENGTH 50

#define TRUE 1

/* function prototypes */

int parseline(char *cmdline, char **argv);

//The two functions below will be needed in lab10.

//Leave them here to be used later.

/* void process_input(int argc, char **argv); */

/* void handle_redir(int count, char *argv[]); */

/* ----------------------------------------------------------------- */

/* The main program starts here */

/* ----------------------------------------------------------------- */

int main(void)

{

char cmdline[MAXLINE];

char *argv[MAXARGS];

int argc;

int status;

pid_t pid;

/* Loop forever to wait and process commands */

while (TRUE) {

/* Print your shell name: csc60mshell (m for mini shell) */

printf("FillInThisSpace> ");

/* Read the command line */

fgets(cmdline, MAXLINE, stdin);

/* Call parseline to build argc/argv */

/* If user hits enter key without a command, continue to loop again at the beginning */

/* Hint: if argc is zero, no command declared */

/* Hint: look up for the keyword "continue" in C */

/* Handle build-in command: exit, pwd, or cd */

/* Put the rest of your code here */

//.......................IGNORE........................

// /* Else, fork off a process */

// else {

// pid = fork();

// switch(pid)

// {

// case -1:

// perror("Shell Program fork error");

// exit(1);

// case 0:

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

// process_input(argc, argv);

// break;

// default:

// /* I am parent process */

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

// perror("Shell Program error");

// else

// printf("Child returned status: %d ",status);

// break;

// } /* end of the switch */

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

}

/* end of the while */

}

/* end of main */

/* ----------------------------------------------------------------- */

/* parseline */

/* ----------------------------------------------------------------- */

/* parse input line into argc/argv format */

int parseline(char *cmdline, char **argv)

{

int count = 0;

char *separator = " \t"; /* Includes space, Enter, Tab */

/* strtok searches for the characters listed in separator */

argv[count] = strtok(cmdline, separator);

while ((argv[count] != NULL) && (count+1

argv[++count] = strtok((char *) 0, separator);

}

return count;

}

/* ----------------------------------------------------------------- */

/* process_input */

/* ----------------------------------------------------------------- */

/*void process_input(int argc, char **argv) { */

/* Step 1: Call handle_redir to deal with operators: */

/* , or both */

/* Step 2: perform system call execvp to execute command */

/* Hint: Please be sure to review execvp.c sample program */

/* if (........ == -1) { */

/* perror("Shell Program"); */

/* _exit(-1); */

/* } */

}

/* ----------------------------------------------------------------- */

//void handle_redir(int count, char *argv[])

/* No code here yet. later */

/* ----------------------------------------------------------------- */

/* ----------------------------------------------------------------- */

Pseudo Code (Highlight indicates provided code.) int main (void) while (TRUE) int child Pid; char cmdLine print the prompt ie. csc60mshell 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" oop starting at zero, thru less than agrc, increment by one. print each argv[loop counter] 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 Use strcmp to do the test after each command, do a continue to end of while(TRUE)-loop if ("exit") issue an exit call else if ("pwd") declare a char variable array of size MAX PATH LENGTH to hold the path do a getcwd print the path else if ("cd") declare a char variable dir as a pointer (with an if the argc is 1 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" end of the while loop end of main Pseudo Code (Highlight indicates provided code.) int main (void) while (TRUE) int child Pid; char cmdLine print the prompt ie. csc60mshell 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" oop starting at zero, thru less than agrc, increment by one. print each argv[loop counter] 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 Use strcmp to do the test after each command, do a continue to end of while(TRUE)-loop if ("exit") issue an exit call else if ("pwd") declare a char variable array of size MAX PATH LENGTH to hold the path do a getcwd print the path else if ("cd") declare a char variable dir as a pointer (with an if the argc is 1 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" end of the while loop end of main

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions