Question
Someone Please write comments to each line of code and also explain each line in comments that what is going on in code. #include #include
Someone Please write comments to each line of code and also explain each line in comments that what is going on in code.
#include
// pre-processor directives #define BUFFERSIZE 256 #define PROMPT "myShell >> " #define PROMPTSIZE sizeof(PROMPT)
// function prototypes void execute_commands(char *[], int, int); // command execution controller. int arg_found(char *[], char *, int); // index of arg if found in the array myargv void bg_process(char *[], int); // execute command in background process void first_redirect_output(char *[], int); // first redirection of the standard output void second_redirect_output(char *[], int); // second redirection of the standard output void redirect_input(char *[], int); // redirection of the standard input void single_piped_command(char *[], int); // execution of multiple commands connected by single shell pipe void execute(char *[], int); // executes a single command up to four command line arguments void pwd(); // prints current working directory void change_dir(); // changes directory void ls(); // list segments
// Begin main() int main(int argc, char** argv) {
// variable declaration and initialization char *str_line = NULL; // string line ssize_t bytes_read; size_t len = 0; const char delims[1] = " "; char *token; char *myargv[BUFFERSIZE]; // stores parsed substrings int myargc; // count of the number of arguments (token) stored in the array myargv /* * shell#0 implementation * getline() read an entire line from the stream, storing the address of * the buffer containing the text into *str_line. The buffer is null terminated * and includes the newline character, if one was found. */ printf("%s", PROMPT);
while (getline(&str_line, &len, stdin) != -1) {
myargc = 0; // reset str_line[strlen(str_line)-1] = '\0';
// Parse the line and tokenize it // strtok returns pointer to the first token token = strtok(str_line, delims);
while (token != NULL) { // store array of tokens in myargv array myargv[myargc++] = token; token = strtok(NULL, delims); } // end while loop
// At this stage we have an array of tokens ready for execution // so we match the type of command for execution based on user inputs if (myargc > 0) { // there exists at least a single command to execute
// check exit condition if (strcmp(myargv[0], "exit") == 0) { exit(-1);
} else { // execute commands execute_commands(myargv, myargc, 0); } // end if-els-if
} // end if printf("%s", PROMPT);
} // end while loop return 0; } // end main()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started