#include #include #include #include #include #include // Examples of the 6 'exec' commands // Note that you can only use one of these commands per run, because each // one will replace this entire program. // There is one man page for 'execve' and one (w/ many aliases) for // the other five. The other five are in fact aliases for execve. int main(int argc, char *argv[]) { int ret; // the command and its args, followed by a 0 char *cmd[] = { "ls", "-Als", 0 }; // the environment, followed by a 0 char *env[] = { "HOME=/usr/home", "LOGNAME=home", 0 }; // These 3 have a hard-coded command w/ its arguments // (terminated by a 0) // The one ending in 'e' also specifies the environment // The one ending in 'p' doesn't need to give the full path // because the system will look up the PATH variable ret = execl ("/bin/ls", "ls", "-Als", 0); ret = execle ("/bin/ls", "ls", "-Als", 0, env); ret = execlp ("ls", "ls", "-Als", 0); // These 3 use the cmd array instead of a hard-coded command // Same notes as above about 'e' and 'p' ret = execv ("/bin/ls", cmd); ret = execve ("/bin/ls", cmd, env); ret = execvp ("ls", cmd); // Detailed definitions of each function: // execl() executes the ls command, specifying the // pathname of the executable (/bin/ls) and using arguments // supplied directly to the command // execle() specifies the environment for the new process // image using the env argument. // execlp() searches for the location of the ls command in the // directories specified by the PATH environment variable. // execv() passes arguments to the ls command in the cmd array. // execve() passes arguments to the ls command in the cmd array, // and specifies the environment for the new process image using // the env argument. // execvp() searches for the location of the ls command in the // directories specified by the PATH environment variable, and // passes arguments to the ls command in the cmd array. }