Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am having an error with my code. For some reason the code will not compile pro

ID: 3759621 • Letter: I

Question

I am having an error with my code. For some reason the code will not compile properly.

My code won't compile. Please fix if possible. This is C++

Here is the shell.cpp file

---------------------------------------------------------------------------------------------

#include <sys/wait.h>

#include <sys/types.h>

#include <unistd.h>

#include <string.h>

#include <string>

#include <iostream>

#include <stdlib.h>

#include <chrono>

#include <ctime>

using namespace std;

// Method to parse the inputs

void Parse_Args(char* in_cmnd, char* splt_cmd[], char red_input[])

{

cout << "[cmd]: ";

cin.getline(red_input,50);

in_cmnd = strtok(red_input, " ");

int i = 0;

// Split the commands

while(in_cmnd != NULL)

{

splt_cmd[i] = in_cmnd;

i++;

in_cmnd = strtok(NULL, " ");

}

}

// Method to clear the command array

void Clean(char* splt_cmd[])

{

for(int lp=0; lp < 40; lp++)

{

splt_cmd[lp] = NULL;

}

}

// Method to execute the commands

void Execute(char* splt_cmd[])

{

pid_t prcsid;

prcsid = fork();

switch(prcsid)

{

case -1:

cout << "Fork Execution Failure" << endl;

exit(-1);

case 0:

execvp(splt_cmd[0], splt_cmd);

if(execvp(splt_cmd[0], splt_cmd) == -1)

{

cout << "Command Not Valid" << endl;

exit(0);

}

default:

wait(NULL);

cout << "Child process finished" << endl;

}

}

// Main method

int main()

{

char* cmnd;

char* splt_cmd[40];

char red_input[50];

std::chrono::time_point<std::chrono::system_clock> beg, fin;

while(splt_cmd[0] != NULL)

{

Clean(splt_cmd);

beg == std::chrono::system_clock::now();

Parse_Args(cmnd, splt_cmd, red_input);

if(strcmp(splt_cmd[0], "exit") == 0 || strcmp(splt_cmd[0], "quit") == 0 )

{

break;

}

else if(strcmp(splt_cmd[0], "^") == 0 )

{

Execute("!"+ splt_cmd[1]);

}

else if(strcmp(splt_cmd[0], "ptime") == 0 )

{

cout<< "Time spent executing child processes:" << tot_seconds.count() << "s ";

}

else

{

Execute(splt_cmd);

}

fin = std::chrono::system_clock::now();

std::chrono::duration<double> tot_seconds = fin-beg;

}

return 1;

}

----------------------------------------------------------------------------------

Assignment Directions:

Introduction

A shell is a user program or environment provides for user interaction. The shell is a command language interpreter that accepts commands from standard input (the keyboard) and executes them. A shell may simply accept user commands and pass them off to the operating system to execute them, it may additionally provide mechanisms, such as redirecting the output of one program to the input of another.

Assignment

Write a C++ program that will act as a command line interpreter for Linux. Use the “[cmd]:” string for your prompt. For example, when a user types a line such as…

word1 word2 word3

...the shell interprets 'word1' as the name of an executable program and 'word2' and 'word3' as parameters to be passed to that program in argv. Your shell should accept any number of command line parameters.

The shell should use the directory system in the order specified by the PATH environment variable to locate the executable file (i.e. You don't have to do anything, don't send me an email asking how to do this, it automatically happens). If the file is found, the shell should pass all parameters in argv and be executed. When the executable file has completed or terminated, the shell program should again resume with the next prompt. Your shell program must wait to display the next command prompt until the previous command has finished executing.

The shell should track the amount of time spent executing child processes. Whenever the command 'ptime' is entered into your shell, the number of seconds, accurate to microseconds, spent executing (waiting for) child processes must be displayed.

The shell should keep a history of the previous commands entered, with no maximum hard-coded history length. If the user enters the command 'history', the shell should provide a listing of the complete command shell history. If the user enters the command '^ 10', for example, the 10th command in the history buffer should be executed (a 1 based index).

The command 'exit' is used to terminate your shell program, when entered, your shell program should end.

The following is an example of what the command shell will look like and how programs will be executed.

[cmd]: ls

shell

shell.cpp

[cmd]: ptime

Time spent executing child processes: 0 seconds 24 milliseconds and 380 microseconds

[cmd]:

Notes

Write a makefile that builds your program, including a 'clean' target.

You must use fork, execlp (or any other exec family of functions you like), wait and std::chrono to complete this assignment.

Reference for std::chrono : http://en.cppreference.com/w/cpp/chrono (Links to an external site.) (Links to an external site.)

Your shell must accept any number of parameters, if the max number of parameters is hard-coded, up to 12 points will be taken off.

Your shell must not have any global variables, up to 20 points taken off for global variables.

Your shell must not crash when the user enters bad commands or bad command arguments.

Explanation / Answer

#include <sys/wait.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <string>
#include <iostream>
#include <stdlib.h>
#include <chrono>
#include <ctime>

using namespace std;

// Method to parse the inputs

void Parse_Args(char* in_cmnd, char* splt_cmd[], char red_input[])

{



cout << "[cmd]: ";

cin.getline(red_input,50);

in_cmnd = strtok(red_input, " ");

int i = 0;



// Split the commands

while(in_cmnd != NULL)

{

splt_cmd[i] = in_cmnd;

i++;

in_cmnd = strtok(NULL, " ");

}

}

// Method to clear the command array

void Clean(char* splt_cmd[])

{

for(int lp=0; lp < 40; lp++)

{

splt_cmd[lp] = NULL;

}

}

// Method to execute the commands

void Execute(char* splt_cmd[])

{

pid_t prcsid;

prcsid = fork();

switch(prcsid)

{

case -1:

cout << "Fork Execution Failure" << endl;

exit(-1);

case 0:

execvp(splt_cmd[0], splt_cmd);



if(execvp(splt_cmd[0], splt_cmd) == -1)

{

cout << "Command Not Valid" << endl;

exit(0);

}



default:

wait(NULL);

cout << "Child process finished" << endl;

}



}

// Main method

int main()

{

char* cmnd;

char* splt_cmd[40];

char red_input[50];

std::chrono::time_point<std::chrono::system_clock> beg, fin;



while(splt_cmd[0] != NULL)

{

Clean(splt_cmd);

beg == std::chrono::system_clock::now();

Parse_Args(cmnd, splt_cmd, red_input);

if(strcmp(splt_cmd[0], "exit") == 0 || strcmp(splt_cmd[0], "quit") == 0 )

{

break;

}

else if(strcmp(splt_cmd[0], "^") == 0 )

{

Execute("!"+ splt_cmd[1]);

}

else if(strcmp(splt_cmd[0], "ptime") == 0 )

{

cout<< "Time spent executing child processes:" << tot_seconds.count() << "s ";

}

else

{

Execute(splt_cmd);

}

fin = std::chrono::system_clock::now();

std::chrono::duration<double> tot_seconds = fin-beg;

}

return 1;

}