C Language and Libraries Help (clang.hlp) (Table of Contents; Topic list)
Important Notice
The pages on this site contain documentation for very old MS-DOS software, purely for historical purposes. If you're looking for up-to-date documentation, particularly for programming, you should not rely on the information found here, as it will be woefully out of date.
SPAWN.C
                                             Up Contents Index Back
─────Run-Time Library───────────────────────────────────────────────────────
 
/* SPAWN.C illustrates the different versions of spawn including:
 *      _spawnl         _spawnle        _spawnlp        _spawnlpe
 *      _spawnv         _spawnve        _spawnvp        _spawnvpe
 *
 * Although SPAWN.C can spawn any program, you can verify how different
 * versions handle arguments and environment by compiling and
 * specifying the sample program ARGS.C. See EXEC.C for examples
 * of the similar exec functions.
 */
 
#include <stdio.h>
#include <conio.h>
#include <process.h>
 
char *my_env[] =                        /* Environment for _spawn?e */
{
    "THIS=environment will be",
    "PASSED=to child by the",
    "SPAWN=functions",
    NULL
};
 
void main()
{
    char *args[4], prog[80];
    int ch, r;
 
    printf( "Enter name of program to spawn: " );
    gets( prog );
    printf( " 1. _spawnl   2. _spawnle   3. _spawnlp   4. _spawnlpe\n" );
    printf( " 5. _spawnv   6. _spawnve   7. _spawnvp   8. _spawnvpe\n" );
    printf( "Type a number from 1 to 8 (or 0 to quit): " );
    ch = _getche();
    if( (ch < '1') || (ch > '8') )
        exit( -1 );
    printf( "\n\n" );
 
    /* Arguments for _spawnv? */
    args[0] = prog;
    args[1] = "_spawn??";
    args[2] = "two";
    args[3] = NULL;
 
    switch( ch )
    {
        case '1':
            r = _spawnl( _P_WAIT, prog, prog, "_spawnl", "two", NULL );
            break;
        case '2':
            r = _spawnle( _P_WAIT, prog, prog, "_spawnle", "two",
                         NULL, my_env );
            break;
        case '3':
            r = _spawnlp( _P_WAIT, prog, prog, "_spawnlp", "two", NULL );
            break;
        case '4':
            r = _spawnlpe( _P_WAIT, prog, prog, "_spawnlpe", "two",
                          NULL, my_env );
            break;
        case '5':
            r = _spawnv( _P_WAIT, prog, args );
            break;
        case '6':
            r = _spawnve( _P_WAIT, prog, args, my_env );
            break;
        case '7':
            r = _spawnvp( _P_WAIT, prog, args );
            break;
        case '8':
            r = _spawnvpe( _P_WAIT, prog, args, my_env );
            break;
        default:
            break;
    }
    if( r == -1 )
        printf( "Couldn't spawn process" );
    else
        printf( "\nReturned from SPAWN!" );
    exit( r );
}
                                    -♦-