int execl(const char *name, const char *arg0, const char *arg1, ...,
const char *argn, (char *) 0); int execv(const char *path, const char *argv[]); int execle(const char *path, const char *arg0, const char *arg1, ...,
const char *argn, (char *) 0, const char *envp[]); int execlp(const char *file, const char *arg0, const char *arg1, ...,
const char *argn, (char *) 0); int execvp(const char *file, const char *argv[]); extern char **environ;
The exec family of functions in all its forms overlays the calling process with the named file, then transfers to the entry point of the core image of the file. There can be no return from a successful exec; the calling core image is lost. An unsuccessful exec generates a return value of -1.
The path argument is a pointer to the name of the file to be executed. The pointers arg[0], arg[1]... address null-terminated strings. Conventionally arg[0] is the name of the file.
Execl is useful when a known file with known arguments is being called; the arguments to execl are the character strings constituting the file and the arguments; the first argument is conventionally the same as the file name (or its last component). A 0 argument must end the argument list.
The execv version is useful when the number of arguments is unknown in advance; the arguments to execv are the name of the file to be executed and a vector of strings containing the arguments. The last argument string must be followed by a 0 pointer.
The execlp and execvp interfaces take the argument file that is used to construct a pathname that identifies the new process image file. If the file argument contains a slash character, the file argument is used as the pathname for this file. Otherwise, the path prefix for this file is obtained by a search of the directories passed as the environment variable PATH. If this environment variable is not present, then the directories /bin and /usr/bin are searched.
When a C program is executed, it is called as follows:
main(int argc, char *argv[]);
where argc is the argument count and argv is an array of character pointers to the arguments themselves. As indicated, argc is conventionally at least one and the first member of the array points to a string containing the name of the file.
Argv is directly usable in another execv because argv[argc] is 0.
Additionally, the following variable:
extern char **environ;is initialized as a pointer to an array of character pointers to the environment strings. Each string consists of a name, an ``='', and a null-terminated value. The array of pointers is terminated by a null pointer. The shell sh(1) passes an environment entry for each global shell variable defined when the program is called. See environ(7) for some conventionally used names. The C run-time start-off routine places a copy of envp in the global cell environ, which is used by execv(2) and execl(2) to pass the environment to any subprograms executed by the current program.