Question: Please help with grep Linux program. I have essential all of the program done, I just get a bad selector file for the compiled output??

Please help with grep Linux program. I have essential all of the program done, I just get a bad selector file for the compiled output??

Can anyone help and show the command line and output in Linux only please? Thank you!!

The following program use redirect to implement env j grep HOME, which prints out your home directory. (a) Read the man page of dup2 and execl system calls. (b) Fill in the blanks in the code provided and make sure it works as the description above. (c) Compile the code and make sure it is executable.

The BOLD writing is where it was blank. Maybe I have the wrong values, please help.

#include

#include

#include

#include

int main (void) {

pid_t childpid;

int fd[2];

if (pipe(fd) == -1) { /* setup a pipe */

perror("Failed to setup pipeline");

return 1;

}

if ((childpid = fork()) == -1) { /* fork a child */

perror("Failed to fork a child");

return 1;

}

if (childpid == 0) { /* env is the child */

if (dup2(fd[1],STDOUT_FILENO)==-1)

perror("Failed to redirect stdout of env");

else if (close(fd[0] == -1)) /* close unused file descriptor */

perror("Failed to close extra pipe descriptors on env");

else {

execl("/usr/bin/env", "env", NULL); /* execute env */

perror("Failed to exec env");

}

return 1;

}

if (dup2(fd[0],STDIN_FILENO)==-1)

/* grep is the parent */

perror("Failed to redirect stdin of grep");

else if (close(fd[1]==-1))

perror("Failed to close extra pipe file descriptors on grep");

else {

execl("/bin/grep", "grep", "HOME", NULL); /* execute "grep HOME" */

perror("Failed to exec grep");

}

return 1;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!