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

  1. /* SPAWN.C illustrates the different versions of spawn including:
  2.  *      spawnl          spawnle         spawnlp         spawnlpe
  3.  *      spawnv          spawnve         spawnvp         spawnvpe
  4.  *
  5.  * Although SPAWN.C can spawn any program, you can verify how different
  6.  * versions handle arguments and environment by compiling and
  7.  * specifying the sample program ARGS.C. See EXEC.C for examples
  8.  * of the similar exec functions.
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include <conio.h>
  13. #include <process.h>
  14.  
  15. char *my_env[] =                        /* Environment for spawn?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, r;
  27.  
  28.     printf( "Enter name of program to spawn: " );
  29.     gets( prog );
  30.     printf( " 1. spawnl   2. spawnle   3. spawnlp   4. spawnlpe\n" );
  31.     printf( " 5. spawnv   6. spawnve   7. spawnvp   8. spawnvpe\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 spawnv? */
  39.     args[0] = prog;             /* First argument ignored under most */
  40.     args[1] = "spawn??";        /*   recent versions of DOS          */
  41.     args[2] = "two";
  42.     args[2] = "two";
  43.     args[3] = NULL;
  44.  
  45.     switch( ch )
  46.     {
  47.         case '1':
  48.             r = spawnl( P_WAIT, prog, prog, "spawnl", "two", NULL );
  49.             break;
  50.         case '2':
  51.             r = spawnle( P_WAIT, prog, prog, "spawnle", "two", NULL, my_env );
  52.             break;
  53.         case '3':
  54.             r = spawnlp( P_WAIT, prog, prog, "spawnlp", "two", NULL );
  55.             break;
  56.         case '4':
  57.             r = spawnlpe( P_WAIT, prog, prog, "spawnlpe", "two", NULL, my_env );
  58.             break;
  59.         case '5':
  60.             r = spawnv( P_WAIT, prog, args );
  61.             break;
  62.         case '6':
  63.             r = spawnve( P_WAIT, prog, args, my_env );
  64.             break;
  65.         case '7':
  66.             r = spawnvp( P_WAIT, prog, args );
  67.             break;
  68.         case '8':
  69.             r = spawnvpe( P_WAIT, prog, args, my_env );
  70.             break;
  71.         default:
  72.             break;
  73.     }
  74.     if( r == -1 )
  75.         printf( "Couldn't spawn process" );
  76.     else
  77.         printf( "\nReturned from SPAWN!" );
  78.     exit( r );
  79. }
  80.