示例#1
0
_WCRTLINK FILE *__F_NAME(fdopen,_wfdopen)( int handle, const CHAR_TYPE *access_mode )
{
    unsigned        flags;
    FILE *          fp;
#if !defined(__NETWARE__)
    int             extflags;
#endif

    if( handle == -1 ) {
        __set_errno( EBADF );           /* 5-dec-90 */
        return( NULL );                 /* 19-apr-90 */
    }
    #ifdef __NETWARE__
        flags = __F_NAME(__open_flags,__wopen_flags)( access_mode );
    #else
        flags = __F_NAME(__open_flags,__wopen_flags)( access_mode,
                                                      &extflags );
    #endif
    if( flags == 0 ) return( NULL );

#if !defined(__NETWARE__)
    /* make sure the handle has the same text/binary mode */
    if( __iomode( handle, flags ) == -1 ) {
        return( NULL );
    }
#endif
    fp = __allocfp( handle );               /* JBS 30-aug-91 */
    if( fp ) {
        fp->_flag &= ~(_READ | _WRITE); /* 2-dec-90 */
        fp->_flag |= flags;
        fp->_cnt = 0;
        _FP_BASE(fp) = NULL;
        fp->_bufsize = 0;                   /* was BUFSIZ JBS 91/05/31 */
        #ifndef __NETWARE__
            _FP_ORIENTATION(fp) = _NOT_ORIENTED; /* initial orientation */
            _FP_EXTFLAGS(fp) = extflags;
        #endif
        #if defined(__NT__) || defined(__OS2__)
            _FP_PIPEDATA(fp).isPipe = 0;    /* not a pipe */
        #endif
        fp->_handle = handle;               /* BJS 91-07-23 */
        if( __F_NAME(tolower,towlower)( *access_mode ) == 'a' ) {
            fseek( fp, 0L, SEEK_END );
        }
        __chktty( fp );                     /* JBS 31-may-91 */
#if !defined(__UNIX__) && !defined(__NETWARE__)
        __SetIOMode( handle, flags );
#endif
    }
    return( fp );
}
示例#2
0
static int spawn_it( FILE *fp, const CHAR_TYPE *command )
/*******************************************************/
{
    int                 pid;
    int                 numWords;
    CHAR_TYPE           **words;    /* note: [0] and [1] used for "cmd.exe /c" */

    /*** Create an argv array from the command string ***/
    numWords = parse_words( command, NULL );
    if( numWords == -1 ) {
        return( 0 );
    }
    words = alloca( (numWords + 2 + 1) * sizeof( CHAR_TYPE * ) );
    if( words == NULL ) {
        return( 0 );
    }
    numWords = parse_words( command, &words[2] );
    if( numWords == -1 ) {
        return( 0 );
    }

    /*** Use CMD.EXE under NT and OS/2, and COMMAND.COM under Win95 ***/
#if defined( __OS2__ )
    words[0] = __F_NAME("cmd.exe",L"cmd.exe");
#else
    if( WIN32_IS_WIN95 ) {
        words[0] = __F_NAME("command.com",L"command.com");  /* 95 */
    } else {
        words[0] = __F_NAME("cmd.exe",L"cmd.exe");          /* NT */
    }
#endif
    words[1] = __F_NAME("/c",L"/c");

    /*** Spawn the process ***/
    pid = __F_NAME(spawnvp,_wspawnvp)( P_NOWAIT, words[0],
        (const CHAR_TYPE **)&words[0] );
    if( pid == -1 ) {
        return( 0 );
    }
    _FP_PIPEDATA(fp).pid = pid;

    /*** Free any memory used by parse_words ('words' freed on return) ***/
    for( numWords--; numWords>=2; numWords-- ) {
        lib_free( words[numWords] );
    }
    return( 1 );
}
示例#3
0
_WCRTLINK FILE *__F_NAME(fdopen,_wfdopen)( int handle, const CHAR_TYPE *access_mode )
{
    int             file_flags;
    FILE            *fp;
    int             extflags;

    if( handle == -1 ) {
        _RWD_errno = EBADF;
        return( NULL );
    }
    file_flags = __F_NAME(__open_flags,__wopen_flags)( access_mode, &extflags );
    if( file_flags == 0 )
        return( NULL );

#ifndef __NETWARE__
    /* make sure the handle has the same text/binary mode */
    if( __iomode( handle, file_flags ) == -1 ) {
        return( NULL );
    }
#endif
    fp = __allocfp();
    if( fp != NULL ) {
        fp->_flag |= file_flags;
        fp->_cnt = 0;
        _FP_BASE( fp ) = NULL;
        fp->_bufsize = 0;                   /* was BUFSIZ JBS 91/05/31 */
#ifndef __NETWARE__
        _FP_ORIENTATION(fp) = _NOT_ORIENTED; /* initial orientation */
        _FP_EXTFLAGS(fp) = extflags;
#endif
#if defined( __NT__ ) || defined( __OS2__ ) || defined(__UNIX__)
        _FP_PIPEDATA(fp).isPipe = 0;    /* not a pipe */
#endif
        fp->_handle = handle;               /* BJS 91-07-23 */
        if( __F_NAME(tolower,towlower)( (UCHAR_TYPE)*access_mode ) == STRING( 'a' ) ) {
            fseek( fp, 0, SEEK_END );
        }
        __chktty( fp );                     /* JBS 31-may-91 */
#if !defined( __UNIX__ ) && !defined( __NETWARE__ )
        __SetIOMode( handle, file_flags );
#endif
    }
    return( fp );
}
示例#4
0
static FILE *__F_NAME(__doopen,__wdoopen)( const CHAR_TYPE *name,
                       CHAR_TYPE    mode,
                       int          file_flags,
                       int          extflags,
                       int          shflag,     /* sharing flag */
                       FILE *       fp )
{
    int open_mode;
    int p_mode;

    SetupTGCSandNCS( RETURN_ARG( FILE *, 0 ) );     /* for NW386 */
    fp->_flag &= ~(_READ | _WRITE);
    fp->_flag |= file_flags;

    /* we need the mode character to indicate if the original */
    /* intention is to open for read or for write */
    mode = __F_NAME(tolower,towlower)( mode );
    if( mode == 'r' ) {
        open_mode = O_RDONLY;
        if( file_flags & _WRITE ) {         /* if "r+" mode */
            open_mode = O_RDWR;
        }
#if defined( __NETWARE__ )
        open_mode |= O_BINARY;
#elif defined( __UNIX__ )
#else
        if( file_flags & _BINARY ) {
            open_mode |= O_BINARY;
        } else {
            open_mode |= O_TEXT;
        }
#endif
        p_mode = 0;
    } else {        /* mode == 'w' || mode == 'a' */
        if( file_flags & _READ ) {          /* if "a+" or "w+" mode */
            open_mode = O_RDWR | O_CREAT;
        } else {
            open_mode = O_WRONLY | O_CREAT;
        }
        if( file_flags & _APPEND ) {
            open_mode |= O_APPEND;
        } else {                    /* mode == 'w' */
            open_mode |= O_TRUNC;
        }
#if defined( __NETWARE__ )
        open_mode |= O_BINARY;
#elif defined( __UNIX__ )
#else
        if( file_flags & _BINARY ) {
            open_mode |= O_BINARY;
        } else {
            open_mode |= O_TEXT;
        }
#endif
        p_mode = PMODE;
    }
    fp->_handle = __F_NAME(sopen,_wsopen)( name, open_mode, shflag, p_mode );
    if( fp->_handle == -1 ) {
        // since we couldn't open the file, release the FILE struct
        __freefp( fp );
        return( NULL );
    }
    fp->_cnt = 0;
    fp->_bufsize = 0;                       /* was BUFSIZ JBS 31-may-91 */
#ifndef __NETWARE__
    _FP_ORIENTATION(fp) = _NOT_ORIENTED; /* initial orientation */
    _FP_EXTFLAGS(fp) = extflags;
#endif
#if defined( __NT__ ) || defined( __OS2__ )
    _FP_PIPEDATA(fp).isPipe = 0;        /* not a pipe */
#endif
    _FP_BASE(fp) = NULL;
    if( file_flags & _APPEND ) {
        fseek( fp, 0L, SEEK_END );
    }
    __chktty( fp );                         /* JBS 28-aug-90 */
    return( fp );
}
示例#5
0
_WCRTLINK FILE *__F_NAME(_popen,_wpopen)( const CHAR_TYPE *command, const CHAR_TYPE *mode )
/*****************************************************************************************/
{
#if defined(__NT__)
    HANDLE              osHandle;
    BOOL                rc;
    int                 handleMode;
#elif defined(__OS2__) && defined(__386__)
    APIRET              rc;
    ULONG               handleState;
#elif defined(__OS2__) && !defined(__386__)
    USHORT              rc;
    USHORT              handleState;
#endif
    FILE *              fp;
    int                 handles[2];
    CHAR_TYPE           textOrBinary;
    CHAR_TYPE           readOrWrite;


    /*** Parse the mode string ***/
    switch( mode[0] ) {         /* read or write */
    case 'r':
        readOrWrite = 'r';
        break;
    case 'w':
        readOrWrite = 'w';
        break;
    default:
        return( NULL );
    }
    switch( mode[1] ) {         /* text or binary */
    case 't':
        textOrBinary = 't';
        break;
    case 'b':
        textOrBinary = 'b';
        break;
    default:
        textOrBinary = _RWD_fmode == _O_BINARY ? 'b' : 't';
    }

    /*** Create the pipe at the OS level ***/
    if( _pipe( handles, 0, textOrBinary == 't' ? _O_TEXT : _O_BINARY ) == -1 ) {
        return( NULL );
    }

    /*** Make read handle non-inheritable if reading ***/
    if( readOrWrite == 'r' ) {
#if defined( __NT__ )
        rc = DuplicateHandle( GetCurrentProcess(),
                              (HANDLE)_os_handle(handles[0]),
                              GetCurrentProcess(), &osHandle, 0,
                              FALSE, DUPLICATE_SAME_ACCESS );
        if( rc == FALSE ) {
            return( 0 );
        }
        close( handles[0] );        /* don't need this any more */
        handleMode = _O_RDONLY  |  (textOrBinary == 't' ? _O_TEXT : _O_BINARY);
        handles[0] = _hdopen( (int)osHandle, handleMode );
        if( handles[0] == -1 ) {
            CloseHandle( osHandle );
            close( handles[1] );
            return( 0 );
        }
#elif defined( __OS2__ )
        rc = DosQFHandState( (HFILE)_os_handle(handles[0]), &handleState );
        if( rc != NO_ERROR ) {
            return( 0 );
        }
        handleState |= OPEN_FLAGS_NOINHERIT;
        handleState &= 0x00007F88;  /* some bits must be zero */
        rc = DosSetFHandState( (HFILE)_os_handle(handles[0]), handleState );
        if( rc != NO_ERROR ) {
            return( 0 );
        }
#endif
    }

    /*** Make write handle non-inheritable if writing ***/
    else {
#if defined (__NT__ )
        rc = DuplicateHandle( GetCurrentProcess(),
                              (HANDLE)_os_handle(handles[1]),
                              GetCurrentProcess(), &osHandle, 0,
                              FALSE, DUPLICATE_SAME_ACCESS );
        if( rc == FALSE ) {
            return( 0 );
        }
        close( handles[1] );        /* don't need this any more */
        handleMode = _O_WRONLY | (textOrBinary == 't' ? _O_TEXT : _O_BINARY);
        handles[1] = _hdopen( (int)osHandle, handleMode );
        if( handles[1] == -1 ) {
            CloseHandle( osHandle );
            close( handles[0] );
            return( 0 );
        }
#elif defined( __OS2__ )
        rc = DosQFHandState( (HFILE)_os_handle(handles[1]), &handleState );
        if( rc != NO_ERROR ) {
            return( 0 );
        }
        handleState |= OPEN_FLAGS_NOINHERIT;
        handleState &= 0x00007F88;  /* some bits must be zero */
        rc = DosSetFHandState( (HFILE)_os_handle(handles[1]), handleState );
        if( rc != NO_ERROR ) {
            return( 0 );
        }
#endif
    }

    /*** Create the pipe's FILE* ***/
    fp = __F_NAME(fdopen,_wfdopen)( handles[readOrWrite == 'r' ? 0 : 1], mode );
    if( fp == NULL ) {
        close( handles[0] );
        close( handles[1] );
        return( NULL );
    }
    _FP_PIPEDATA(fp).isPipe = 1;
    _FP_PIPEDATA(fp).pid = -1;

    /*** Spawn the process ***/
    if( connect_pipe( fp, command, handles, readOrWrite, textOrBinary ) ) {
        return( fp );
    } else {
        close( handles[0] );
        close( handles[1] );
        return( NULL );
    }
}