All pastes #271232 Raw Edit

Someone

public text v1 · immutable
·published 2006-12-08 03:00 UTC
rendered paste body
#include <stdlib.h>
#include <dos.h>
#include <errno.h>
#include <process.h>
#include <string.h>
#include <dir.h>
#include <io.h>
#include <limits.h>

static const char __exe[] = ".EXE";
static const char __com[] = ".COM";
static const char __bat[] = ".BAT";
static char __batflag = 0;

static char *__getcomexe( const char *path )
{
    char *p;
    static char fullpath[ PATH_MAX ];

    strcpy( fullpath, path );

    if( ( p = strrchr( fullpath, '\\' ) ) == NULL ) p = fullpath;

    while( *p && *p != '.' ) p++;

    if( *p == '.' ) {
        if( strcmp( p, __exe ) != 0 &&
            strcmp( p, __com ) != 0 &&
            strcmp( p, __bat ) != 0 ) while( *p ) p++;
        else {
            return( ( char * )__retsfn( fullpath ) );
        }
    }

    strcpy( p, __com );
    if( access( fullpath, F_OK ) == 0 )
        return( ( char * )__retsfn( fullpath ) );
    strcpy( p, __exe );
    if( access( fullpath, F_OK ) == 0 )
        return( ( char * )__retsfn( fullpath ) );
    strcpy( p, __bat );
    __batflag = 1;
    if( access( fullpath, F_OK ) == 0 )
        return( ( char * )__retsfn( fullpath ) );

    return( NULL );
}

int spawnv( int mode, const char *path, char **argv )
{
    char *fullpath = __getcomexe( path );
    char cmdline[ _POSIX_ARG_MAX ];
    int  retval;

    cmdline[ 0 ] = '\0';

    if( mode == P_NOWAIT ) {
        errno = EINVAL;

        return( -1 );
    }

    if( fullpath == NULL ) {
        errno = ENOENT;

        return( -1 );
    }

    if( __batflag ) {
        strcpy( cmdline, "/c " );
        strcat( cmdline, fullpath );
        fullpath = getenv( "COMSPEC" );

        if( fullpath == NULL ) {
            errno = ENOENT;

            return( -1 );
        }
    }

    if( *argv == NULL ) goto skip;
    argv++; /* We don't use argv[ 0 ] */

    for( ; *argv != NULL; argv++ ) {
        strcat( cmdline, " \"" );
        strcat( cmdline, *argv );
        strcat( cmdline, "\""  );
    }
skip:

    retval = _exec( fullpath, cmdline );

    if( mode == P_OVERLAY ) exit( retval );

    return( retval );
}