Question: Execute the unmodified program and report on the results. Modify the cmd2 array so that instead of being assigned nullptr , cmd2[1] receives a strdup

Execute the unmodified program and report on the results.

Modify the cmd2 array so that instead of being assigned nullptr, cmd2[1] receives a strdup of "-l". What's the resulting difference in output, and why?

Try changing the argument "/bin/ls" in the call to execv to "ls". What did or did not happen in this call, and why?

Further modify the program from its state in item 3 by changing the call of execv to calling execvp. Again, what did or did not happen in this call, and why?

#include #include #include #include using namespace std; extern char **environ; // Pointer to environment variables int main(void) { int i = 0; // Loop iterator int ret; // Return value of exec. If exec works, then // you shouldn't ever be able to read it. char *argv[256]; // For lack of a better variable name... for (i = 0; i < 256; i++) // Make sure argv is null terminated. argv[i] = (char *) 0; char **cmd2 = new char*[3]; cmd2[0] = strdup("ls"); cmd2[1] = NULL; cmd2[2] = NULL; ret = execv ("/bin/ls", cmd2); if (ret) { cout << "Yoikes! I got a ret value of " << ret << endl; cout << "(call of exec failed.)" << endl; } return 0; }

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!