Exec()
2002-04-11 19:50:53
Category: c:runtime
Description: Shows how to run files using the exec() family of functions.
Author: detour
Viewed: 5833
Rating: (22 votes)


/* exec.c written by detour@metalshell.com
 *
 * There are several different ways to run a program
 * using exec. If you use exec() be aware that it will
 * not return to your program. All arguments to exec must
 * be terminated with NULL.
 *
 * exec*l*: takes a list of args.
 * exec*p*: uses the environment path to search for the file.
 * exec*e*: passes current environment variables as well as
 *          ones you supply in a pointer.
 * exec*v*: takes the arguments as a pointer.
 *
 * http://www.metalshell.com
 *
 */
 
 
#include <stdio.h>
#include <unistd.h>
 
#define TYPE 0
 
int main() {
  char *args[] = { "/usr/bin/w", "-h",
                   NULL };
  char *envir[] = { "DUMMY=BEER" };
 
  switch(TYPE) {
    case 0:
      execl("/usr/bin/w", "/usr/bin/w", "-h", NULL);
    case 1:
      execlp("w", "w", "-h", NULL);
    case 2:
      execv("/usr/bin/w", args);
    case 3:
      execle("/usr/bin/w", "/usr/bin/w", "-h", NULL, envir);
    case 4:
      execvp("w", args);
    case 5:
      execve("/usr/bin/w", args, envir);
  }
 
  printf("This should never be seen.\n");
 
  return 0;
}