home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / c / other / learn / exec.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-31  |  2.2 KB  |  78 lines

  1. /* EXEC.C illustrates the different versions of exec including:
  2.  *      execl           execle          execlp          execlpe
  3.  *      execv           execve          execvp          execvpe
  4.  *
  5.  * Although EXEC.C can exec any program, you can verify how different
  6.  * versions handle arguments and environment by compiling and
  7.  * specifying the sample program ARGS.C. See SPAWN.C for examples
  8.  * of the similar spawn functions.
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include <conio.h>
  13. #include <process.h>
  14.  
  15. char *my_env[] =                /* Environment for exec?e */
  16. {
  17.     "THIS=environment will be",
  18.     "PASSED=to child by the",
  19.     "SPAWN=functions",
  20.     NULL
  21. };
  22.  
  23. main()
  24. {
  25.     char *args[4], prog[80];
  26.     int ch;
  27.  
  28.     printf( "Enter name of program to exec: " );
  29.     gets( prog );
  30.     printf( " 1. execl   2. execle   3. execlp   4. execlpe\n" );
  31.     printf( " 5. execv   6. execve   7. execvp   8. execvpe\n" );
  32.     printf( "Type a number from 1 to 8 (or 0 to quit): " );
  33.     ch = getche();
  34.     if( (ch < '1') || (ch > '8') )
  35.         exit( 1 );
  36.     printf( "\n\n" );
  37.  
  38.     /* Arguments for execv? */
  39.     args[0] = prog;             /* First argument ignored under most */
  40.     args[1] = "exec??";         /*   recent versions of DOS          */
  41.     args[2] = "two";
  42.     args[3] = NULL;
  43.  
  44.     switch( ch )
  45.     {
  46.         case '1':
  47.             execl( prog, prog, "execl", "two", NULL );
  48.             break;
  49.         case '2':
  50.             execle( prog, prog, "execle", "two", NULL, my_env );
  51.             break;
  52.         case '3':
  53.             execlp( prog, prog, "execlp", "two", NULL );
  54.             break;
  55.         case '4':
  56.             execlpe( prog, prog, "execlpe", "two", NULL, my_env );
  57.             break;
  58.         case '5':
  59.             execv( prog, args );
  60.             break;
  61.         case '6':
  62.             execve( prog, args, my_env );
  63.             break;
  64.         case '7':
  65.             execvp( prog, args );
  66.             break;
  67.         case '8':
  68.             execvpe( prog, args, my_env );
  69.             break;
  70.         default:
  71.             break;
  72.     }
  73.  
  74.     /* This point is only reached if exec fails. */
  75.     printf( "\nProcess was not execed." );
  76.     exit( 0 );
  77. }
  78.