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

Can someone please insert inline comments what some of the coding statements are

ID: 3889613 • Letter: C

Question

Can someone please insert inline comments what some of the coding statements are doing

#include <unistd.h>
#include <string.h>

#define MAX_LINE 80 /* the max length command */

int main(void)
{
char *args[MAX_LINE/2 + 1]; /* command line arguments */
char ss[MAX_LINE];  
int should_run = 1; /* flag to determine when to exit program */
int i = 0;
int j;
while(should_run)
{
        printf("osh> ");
        fflush(stdout);
        scanf ("%[^ ]%*c", ss);
        printf("input:%s ",ss);
        i = 0;
        int j;
        args[i] = strtok(ss," ");
        while (args[i] != NULL) {
          i++;
          args[i] = strtok(NULL, " ");
        }

        if(strcmp(args[0], "exit") == 0)
          break;
        if(strcmp(args[i-1], "&") != 0)
        {
          pid_t pid;
          pid = fork();
          if(pid < 0)
          {
                fprintf(stderr,"FORK Failed ");
                return 1;
          }
          else if (pid == 0)
          {
                execvp(args[0],args);
                for(int j=0;j<i;j++)
                  args[j] = NULL;
          }
          else
          {
                wait(NULL);
          }
        }
        else
        {
          pid_t pid;
          pid = fork();
          if(pid < 0){
                fprintf(stderr,"FORK Failed ");
                return 1;
          }
          else if (pid == 0){
                args[i-1] = NULL;
                execvp(args[0],args);
          }
          else
          {
                printf(" ");
          }
        }

}
return 0;
}

Explanation / Answer

#include <unistd.h>

#include <string.h>

/*

* This program implements the functionality of linux terminal.

* It will take command along with options to execute the command.

* The each command given by user will be executed by new child process i.e

* created by fork() function.

* In order to terminate the program from running, give "exit" as an input.

*/

/* the max length command */

#define MAX_LINE 80

int main(void)

{

char *args[MAX_LINE/2 + 1]; /* array of character arrays to store the options/arguments given by user. */

char ss[MAX_LINE]; /* character array to store the command from user */

int should_run = 1; /* flag to determine when to exit program */

int i = 0;

int j;

  

while(should_run)

{

printf("osh> ");

fflush(stdout); // flushing standard input

scanf ("%[^ ]%*c", ss); // reading command along with the arguments/options */

printf("input:%s ",ss); // printing the whole command

i = 0;

int j;

args[i] = strtok(ss," "); // strtok() function is used to tokenize the string, In this line It is storing the command into character array.

while (args[i] != NULL) {

i++;

args[i] = strtok(NULL, " "); // storing each option given to the command in the array

}

if(strcmp(args[0], "exit") == 0) // checking that if command is "exit" or not

break; // coming out of the loop to exit

if(strcmp(args[i-1], "&") != 0) // '&' symbol at the end of the command means ..run the command in the background.

// checking last option given to the command that whether command need to run in the background or not.

{

pid_t pid; // variable to store the process id of the new child process

pid = fork(); // fork function will create new child process and returns the id of the process.

if(pid < 0) // here, parent process is checking that whether new process is created or not.

{

fprintf(stderr,"FORK Failed ");

return 1; // terminating the program after printin above statement.

}

else if (pid == 0) // pid is zero means, fork() function successfully created the new child process.

// below block of code is executed by child process.

{

execvp(args[0],args); // execvp() function executes file i.e command with options here.

for(int j=0;j<i;j++)

args[j] = NULL; // resetting array contents

}

  

else

{

wait(NULL); // meaning wait for any child process whose process group ID is

// equal to that of the calling process.

}

}

else

{

pid_t pid; // variable to store the process id of the new child process

pid = fork(); // fork function will create new child process and returns the id of the process.

if(pid < 0){ // here, parent process is checking that whether new process is created or not.

fprintf(stderr,"FORK Failed ");

return 1; // terminating the program after printin above statement.

}

else if (pid == 0){ // pid is zero means, fork() function successfully created the new child process.

// below block of code is executed by child process.

args[i-1] = NULL; // Removing the '&' from the options given to the command.

execvp(args[0],args); // execvp() function executes file i.e command with options here.

}

else

{

printf(" "); // printing new lines by the parent process.

}

}

}

return 0;

}