Esempio n. 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 );
}
Esempio n. 2
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 );
}
Esempio n. 3
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 );
}
Esempio n. 4
0
    _WCRTLINK wint_t (ungetwc)( wint_t c, FILE *fp )
#endif
    {
        if( c == __F_NAME(EOF,WEOF) ) {   /* cannot push EOF */
            return( c );
        }
        _ValidFile( fp, __F_NAME(EOF,WEOF) );
        _AccessFile( fp );

        /*** Deal with stream orientation ***/
        #ifndef __NETWARE__
            #ifdef __WIDECHAR__
                if( _FP_ORIENTATION(fp) != _WIDE_ORIENTED ) {
                    if( _FP_ORIENTATION(fp) == _NOT_ORIENTED ) {
                        _FP_ORIENTATION(fp) = _WIDE_ORIENTED;
                    } else {
                        _ReleaseFile( fp );
                        return( WEOF );
                    }
                }
            #else
                if( _FP_ORIENTATION(fp) != _BYTE_ORIENTED ) {
                    if( _FP_ORIENTATION(fp) == _NOT_ORIENTED ) {
                        _FP_ORIENTATION(fp) = _BYTE_ORIENTED;
                    } else {
                        _ReleaseFile( fp );
                        return( EOF );
                    }
                }
            #endif
        #endif

        if( fp->_flag & _DIRTY ) {        /* cannot unget after a put */
            _ReleaseFile( fp );
            return( __F_NAME(EOF,WEOF) );
        }
        if(( fp->_flag & _READ ) == 0 ) { /* not open for input */
            _ReleaseFile( fp );
            return( __F_NAME(EOF,WEOF) );
        }
        if( _FP_BASE(fp) == NULL ) {      /* no buffer allocated */
            __ioalloc( fp );
        }
        #ifdef __WIDECHAR__
            if( fp->_flag & _BINARY ) {
                /*** Leave the character in wide form ***/
                if( fp->_cnt == 0 ) {           /* read buffer is empty */
                    fp->_cnt = sizeof(wchar_t);
                    fp->_ptr = _FP_BASE(fp) + fp->_bufsize - sizeof(wchar_t);
                    fp->_flag |= _UNGET;                    /* 10-mar-90 */
                    memcpy( fp->_ptr, &c, sizeof(wchar_t) );
                } else if( fp->_ptr != _FP_BASE(fp) ) {
                    fp->_cnt += sizeof(wchar_t);
                    fp->_ptr -= sizeof(wchar_t);
                    fp->_flag |= _UNGET;
                    memcpy( fp->_ptr, &c, sizeof(wchar_t) );
                } else {                        /* read buffer is full */
                    _ReleaseFile( fp );
                    return( WEOF );
                }
            } else {
                char    mbc[MB_CUR_MAX];
                int     mbcLen;

                /*** Convert the character to multibyte form ***/
                if( wctomb( mbc, c ) == -1 ) {
                    __set_errno( EILSEQ );
                    return( WEOF );
                }
                mbcLen = _mbclen( mbc );

                /*** Store the converted character ***/
                if( fp->_cnt == 0 ) {           /* read buffer is empty */
                    fp->_cnt = mbcLen;
                    fp->_ptr = _FP_BASE(fp) + fp->_bufsize - mbcLen;
                    fp->_flag |= _UNGET;                    /* 10-mar-90 */
                    _mbccpy( fp->_ptr, mbc );
                } else if( fp->_ptr != _FP_BASE(fp) ) {
                    fp->_cnt += mbcLen;
                    fp->_ptr -= mbcLen;
                    fp->_flag |= _UNGET;
                    _mbccpy( fp->_ptr, mbc );
                } else {                        /* read buffer is full */
                    _ReleaseFile( fp );
                    return( WEOF );
                }
            }
        #else
            if( fp->_cnt == 0 ) {               /* read buffer is empty */
                fp->_cnt = CHARSIZE;
                fp->_ptr = _FP_BASE(fp) + fp->_bufsize - CHARSIZE;
                fp->_flag |= _UNGET;                                 /* 10-mar-90 */
                *(CHAR_TYPE*)(fp->_ptr) = c;
            } else if( fp->_ptr != _FP_BASE(fp) ) {
                fp->_cnt += CHARSIZE;
                fp->_ptr -= CHARSIZE;
                if( *(CHAR_TYPE*)(fp->_ptr) != c )  fp->_flag |= _UNGET; /* 10-mar-90 */
                *(CHAR_TYPE*)(fp->_ptr) = c;
            } else {                            /* read buffer is full */
                _ReleaseFile( fp );
                return( EOF );
            }
        #endif
        fp->_flag &= ~ _EOF;

        _ReleaseFile( fp );
        return( (UCHAR_TYPE) c );
    }