Consider environment variables used in Unixbased operating s

Consider environment variables used in Unix-based operating systems. A frequently seen environment variable is named PATH, and is used by command interpreters, or shells, to identify the names of directories to be searched to find executable programs. For example, a typical value of PATH may be: \"/Users/chris/bin:/usr/local/bin:/usr/bin:/bin:.\" which provides a colon-separated list of directories to search for a required program. Write a C99 function, named executeUsingPATH (), which accepts the name of a program to execute and a NULL-pointer terminated vector of arguments to be passed to t6hat program. The requested program may be specified using just its name, or with either an absolute or a relative pathname. int executeUsingPATH(char *programName, char *arguments[]); Function executeUsingPATH () should attempt to execute programName from each directory provided via PATH, in order. If programName is found and may be executed (passing to it the indicated program arguments), the function should wait for its execution to terminate, and then return the exit status of the terminated process. If the function cannot find and execute programName then it should simply return the integer -1. Your function should not simply call the similar library function named execvp ().

Solution

The below program creates a new process and execute an HFS file called programname. The path /users/chris/bin is added at the end of the PATH environment variable before calling execlp. Here, a -1 will be returned if the call to execlp goes unsuccessful.

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

main()
{
pid_t pid;
char *pathvar;
char newpath[1000];

pathvar = getenv(\"PATH\");
strcpy(newpath, pathvar);
strcat(newpath, \":/users/chris/bin\");
setenv(\"PATH\", newpath);

if ((pid = fork()) == -1)
perror(\"fork error\");
else if (pid == 0) {
execlp(\"programname\", \"programname\", NULL);
printf(\"Return not expected. Must be an execlp error.n\");
}
}

 Consider environment variables used in Unix-based operating systems. A frequently seen environment variable is named PATH, and is used by command interpreters,

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site