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

This is a continuation of my previous questions. I will copy the code we had bef

ID: 3879521 • Letter: T

Question

This is a continuation of my previous questions. I will copy the code we had before. I need a couple of things changed.

I am programming in C using VIM and terminal under Ubuntu Linux.

Here is the previous code:

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<errno.h>

void main(int argc , char *argv[])
{

   // rc is used to store the recieved value of fork
   int rc;

   // variable used to collect status of a program
        int status;

   // pointer to command
        char *cmd;

   // pointers to arguments to command
        char *myArgv[3];

   // if there are three or more arguments like ./program cmd argv1 argv2 ...
     if (argc >= 3)
   {
            cmd = argv[1];
           myArgv[ 0 ] = cmd;
            myArgv[ 1 ] = argv[ 2 ];
            myArgv[ 2 ] = NULL;
        }
    else
   {   // not enough args we will run ls -al command
       cmd = "ls";
           myArgv[ 0 ] = cmd;
           myArgv[ 1 ] = "-al";
           myArgv[ 2 ] = NULL;
    }

   // Printing the PID of parent process
    printf("Parent: I am pid %d ", getpid() );

   // Here we are forking and creating a child process
    rc = fork();

    if (rc > 0)
   { // I am the parent

            printf("Parent: waiting on pid %d ", rc);

    // here we passed the pid of child, and we are waiting till it terminates
    // the return status is in status variable.
    waitpid(rc, &status, 0);

    if(WIFEXITED(status))
           printf("Child's exit code %d ", WEXITSTATUS(status));
    else
             printf("Child did not terminate with exit ");

   }
    else if (rc == 0)
   { // I am the child

            printf("Child: I am pid %d about to exec() %s ", getpid(), cmd);
            execvp(cmd, myArgv);
      
       // control will only return if execvp fails
                printf("Child: execvp() failed...exiting ");
                exit(1);
        }
        else
        { // an error occured, no child created
           errno=EINVAL;
                perror("Parent: a bad thing happened:");
                exit(1);
   }

   return ;


}

I need to have you add a few things. Put this all in an if statement that only allows it to run if it is a valid command that should be sent to execvp. If not, have it go to an else statement. Don't put any detail into the else part. I think I know how to do that. Also, make sure the commands execute correctly. I am getting errors when I try to use "ls" or "ls -al" for example. It should also run and other common commands that execp can do, except it doesn't need to do PWD.

Explanation / Answer

here is your modified code : -------------------->>>>>>>>>>>>>>>

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<errno.h>
#include<string.h>
void main(int argc , char *argv[])
{

   // rc is used to store the recieved value of fork
   int rc;
   // variable used to collect status of a program
        int status;
   // pointer to command
        char *cmd;
   // pointers to arguments to command
        char *myArgv[3];
   // if there are three or more arguments like ./program cmd argv1 argv2 ...
     if (argc >= 3)
   {
            cmd = argv[1];
           myArgv[ 0 ] = cmd;
            myArgv[ 1 ] = argv[ 2 ];
            myArgv[ 2 ] = NULL;
        }
    else
   {   // not enough args we will run ls -al command
   strcpy(cmd,"ls");
           myArgv[ 0 ] = cmd;
           strcpy(myArgv[1],"-al");
           myArgv[ 2 ] = NULL;
    }
   // Printing the PID of parent process
    printf("Parent: I am pid %d ", getpid() );
   // Here we are forking and creating a child process
    rc = fork();
    if (rc > 0)
   { // I am the parent
            printf("Parent: waiting on pid %d ", rc);
    // here we passed the pid of child, and we are waiting till it terminates
    // the return status is in status variable.
    waitpid(rc, &status, 0);
    if(WIFEXITED(status))
           printf("Child's exit code %d ", WEXITSTATUS(status));
    else
             printf("Child did not terminate with exit ");
   }
    else if (rc == 0)
   { // I am the child
           if(strcmp(cmd,"give cmd here") == 0 || strcmp(cmd,"give cmd here") ... ){
  //please cmp all the cmd above to see the cmd is correct or not i don't have the list of cmd so i am not giving any cmd
     printf("Child: I am pid %d about to exec() %s ", getpid(), cmd);
            execvp(cmd, myArgv);
      
       // control will only return if execvp fails
                printf("Child: execvp() failed...exiting ");
                exit(1);
   }else{
    
   }
        }
        else
        { // an error occured, no child created
           errno=EINVAL;
                perror("Parent: a bad thing happened:");
                exit(1);
   }
   return ;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote