Пример #1
0
void    InitStd( void )
{
// Initialize standard i/o.
#if !defined( __UNIX__ ) && defined( __WATCOMC__ )
    // don't call setmode() since we don't want to affect higher level
    // i/o so that if C function gets called, printf() works ok
// There is no __GetIOMode in the C runtime!
#define __GetIOMode  __IOMode

    extern      unsigned        __GetIOMode(int);
    extern      void            __SetIOMode(int,unsigned);

    __SetIOMode( STDIN_FILENO, __GetIOMode( STDIN_FILENO ) | _BINARY );
    __SetIOMode( STDOUT_FILENO, __GetIOMode( STDOUT_FILENO ) | _BINARY );
    __SetIOMode( STDERR_FILENO, __GetIOMode( STDERR_FILENO ) | _BINARY );
#endif
#if defined( __RT__ )
    ChkRedirection( FStdIn );
    ChkRedirection( FStdOut );
    ChkRedirection( FStdErr );
    if( __DevicesCC() ) {
        FStdOut->attrs |= CC_NOLF;
    }
#endif
}
Пример #2
0
_WCRTLINK int dup( int old_hid )
{
    HANDLE      new_handle;
    int         hid;
    HANDLE      cprocess;

    __handle_check( old_hid, -1 );

    // First try to get the required slot.
    // No point in creating a new handle only to not use it.  JBS 99/11/01
    hid = __allocPOSIXHandle( DUMMY_HANDLE );
    if( hid == -1 ) {
        return( -1 );
    }

    cprocess = GetCurrentProcess();

    if( !DuplicateHandle( cprocess,  __getOSHandle( old_hid ), cprocess,
                        &new_handle, 0, TRUE, DUPLICATE_SAME_ACCESS ) ) {
        // Give back the slot we got
        __freePOSIXHandle( hid );
        return( __set_errno_nt() );
    }
    // Now use the slot we got
    __setOSHandle( hid, new_handle );   // JBS 99/11/01
    __SetIOMode( hid, __GetIOMode( old_hid ) );
    return( hid );
}
Пример #3
0
_WCRTLINK unsigned _dos_open( const char *name, unsigned mode, int *handle )
{
    APIRET      rc;
    OS_UINT     rwmode, actiontaken, openmode;
    HFILE       fhandle;
    int         share;
    unsigned    iomode_flags;

    while( *name == ' ' ) ++name;
    rwmode = mode & OPENMODE_ACCESS_MASK;
    if( rwmode == OPENMODE_ACCESS_WRONLY
#if defined(__OS2_286__)
        && !_RWD_osmode
        /* Can't open WRONLY file in bound application under DOS */
#endif
        ) {
        rwmode = OPENMODE_ACCESS_RDWR;
    }
    share = mode & OPENMODE_SHARE_MASK;
    if( share == OPENMODE_DENY_COMPAT ) {
        share = OPENMODE_DENY_NONE;
    }
    openmode = share+rwmode;
    rc = DosOpen( (PSZ)name, &fhandle, &actiontaken, 0ul,
        _A_NORMAL, OPENFLAG_OPEN_IF_EXISTS, openmode, 0ul );
    if( rc ) {
        return( __set_errno_dos_reterr( rc ) );
    }
    *handle = fhandle;
    if( rwmode == O_RDWR ) iomode_flags = _READ | _WRITE;
    if( rwmode == O_RDONLY) iomode_flags = _READ;
    if( rwmode == O_WRONLY) iomode_flags = _WRITE;
    __SetIOMode( fhandle, iomode_flags );
    return( 0 );
}
Пример #4
0
static void ioSuppTempOpen(             // OPEN TEMPORARY FILE
    void )
{
    auto char   fname[_MAX_PATH];

    for(;;) {
        tempFname( fname );
#if defined(__DOS__)
        {
            tiny_ret_t  rc;

            rc = TinyCreateNew( fname, 0 );
            if( TINY_ERROR( rc ) ) {
                temphandle = -1;
            } else {
                temphandle = TINY_INFO( rc );
                __SetIOMode( temphandle, _READ | _WRITE | _BINARY );
            }
        }
#else
        temphandle = open( fname, AMODE, PMODE_RW );
#endif
        if( temphandle != -1 ) break;
        if( workFile[5] == 'Z' ) {
            temphandle = -1;
            break;
        }
        switch( workFile[5] ) {
        case '9':
            workFile[5] = 'A';
            break;
        case 'I':
            workFile[5] = 'J';  /* file-system may be EBCDIC */
            break;
        case 'R':
            workFile[5] = 'S';  /* file-system may be EBCDIC */
            break;
        default:
            ++workFile[5];
            break;
        }
    }
#if defined(__UNIX__)
    /* Under POSIX it's legal to remove a file that's open. The file
       space will be reclaimed when the handle is closed. This makes
       sure that the work file always gets removed. */
    remove( fname );
    tempname = NULL;
#else
    tempname = FNameAdd( fname );
#endif
    if( temphandle == -1 ) {
        ioSuppError( ERR_UNABLE_TO_OPEN_WORK_FILE );
    }
}
Пример #5
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 );
}
Пример #6
0
_WCRTLINK int dup2( int handle1, int handle2 )
{
    tiny_ret_t rc;

    __handle_check( handle1, -1 );

    if( handle1 == handle2 ) {
        return( handle2 );
    }
    rc = TinyDup2( handle1, handle2 );
    if( TINY_ERROR(rc) ) {
        return( __set_errno_dos( TINY_INFO(rc) ) );
    }
    __SetIOMode( handle2, __GetIOMode( handle1 ) );
    return( 0 );    /* indicate success */
}
Пример #7
0
_WCRTLINK int dup2( int handle1, int handle2 )
{
    APIRET  rc;

    __handle_check( handle1, -1 );

    if( handle1 == handle2 ) {
        return( handle2 );
    }
    rc = DosDupHandle( handle1, (PHFILE)&handle2 );
    if( rc != 0 ) {
        return( __set_errno_dos( rc ) );
    }
    __SetIOMode( handle2, __GetIOMode( handle1 ) );
    return( 0 );    /* indicate success */
}
Пример #8
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 );
}
Пример #9
0
_WCRTLINK int setmode( int handle, int mode )
{
    unsigned        iomode_flags;
    unsigned        old_mode;
    __stream_link   *link;
    FILE            *fp;

    __handle_check( handle, -1 );

    iomode_flags = __GetIOMode( handle );
    if( iomode_flags == 0 ) {
        _RWD_errno = EBADF;
        return( -1 );
    }
    old_mode = (iomode_flags & _BINARY) ? O_BINARY : O_TEXT;
    if( mode != old_mode ) {
        if( mode == O_BINARY  ||  mode == O_TEXT ) {
            iomode_flags &= ~ _BINARY;
            if( mode == O_BINARY ) {
                iomode_flags |= _BINARY;
            }
            __SetIOMode( handle, iomode_flags );
            _AccessFileH( handle );
            for( link = _RWD_ostream; link != NULL; link = link->next ) {
                fp = link->stream;
                if( fp->_flag != 0 ) {      /* if file is open */
                    if( fileno( fp ) == handle ) {
                        fp->_flag &= ~ _BINARY;
                        if( mode == O_BINARY ) {
                            fp->_flag |= _BINARY;
                        }
                        break;
                    }
                }
            }
            _ReleaseFileH( handle );
        } else {
            _RWD_errno = EINVAL;
            old_mode = -1;
        }
    }
    return( old_mode );
}
Пример #10
0
_WCRTLINK unsigned _dos_open( const char *name, unsigned mode, int *posix_handle )
{
    HANDLE      handle;
    unsigned    rwmode;
    DWORD       share_mode;
    DWORD       desired_access, os_attr;
    unsigned    iomode_flags;
    int         hid;

    // First try to get the required slot.
    // No point in creating a file only to not use it.  JBS 99/11/01
    hid = __allocPOSIXHandle( DUMMY_HANDLE );
    if( hid == -1 ) {
        return( __set_errno_dos_reterr( ERROR_NOT_ENOUGH_MEMORY ) );
    }

    rwmode = mode & OPENMODE_ACCESS_MASK;

    __GetNTAccessAttr( rwmode, &desired_access, &os_attr );
    __GetNTShareAttr( mode & (OPENMODE_SHARE_MASK|OPENMODE_ACCESS_MASK),
                      &share_mode );
    handle = CreateFile( (LPTSTR) name, desired_access, share_mode, 0,
                        OPEN_EXISTING, os_attr, NULL );
    if( handle == (HANDLE)-1 ) {
        __freePOSIXHandle( hid );
        return( __set_errno_nt_reterr() );
    }
    // Now use the slot we got.
    __setOSHandle( hid, handle );   // JBS 99/11/01

    *posix_handle = hid;
    iomode_flags = 0;
    if( rwmode == O_RDWR )
        iomode_flags = _READ | _WRITE;
    if( rwmode == O_RDONLY )
        iomode_flags = _READ;
    if( rwmode == O_WRONLY )
        iomode_flags = _WRITE;
    __SetIOMode( hid, iomode_flags );
    return( 0 );
}
Пример #11
0
_WCRTLINK int _open_osfhandle( long osfhandle, int flags )
{
    int posix_handle;

    #if defined(__NT__)
        // Under Win32, we get an OS handle argument
        posix_handle = __allocPOSIXHandle( (HANDLE)osfhandle );
        if( posix_handle == -1 ) return( -1 );
    #else
        // Under everything else, we get a POSIX handle argument
        posix_handle = osfhandle;
    #endif
    #if !defined(__NETWARE__)
        if( check_mode( posix_handle, flags ) ) {
            return( -1 );
        }
        #if !defined(__UNIX__)
        {
            int         rwmode;
            unsigned    io_mode;

            rwmode = flags & ( O_RDONLY | O_WRONLY | O_RDWR );
            io_mode = __GetIOMode( posix_handle );
            io_mode &= ~(_READ|_WRITE|_APPEND|_BINARY);
            if( rwmode == O_RDWR )  io_mode |= _READ | _WRITE;
            if( rwmode == O_RDONLY) io_mode |= _READ;
            if( rwmode == O_WRONLY) io_mode |= _WRITE;
            if( flags & O_APPEND )   io_mode |= _APPEND;
            if( flags & O_BINARY ) {
               io_mode |= _BINARY;
            }
            __SetIOMode( posix_handle, io_mode );
        }
        #endif
    #endif

    return( posix_handle );
}
Пример #12
0
_WCRTLINK unsigned _dos_creat( const char *name, unsigned attribute, int *handle )
{
    APIRET  rc;
    OS_UINT actiontaken;
    HFILE   fhandle;

    while( *name == ' ' ) ++name;
    rc = DosOpen( (PSZ)name,
                     &fhandle,
                     &actiontaken,
                     0ul,
                     attribute,
                     OPENFLAG_REPLACE_IF_EXISTS |
                     OPENFLAG_CREATE_IF_NOT_EXISTS,
                     OPENMODE_ACCESS_RDWR | OPENMODE_DENY_NONE,
                     0ul );
    if( rc ) {
        return( __set_errno_dos_reterr( rc ) );
    }
    *handle = fhandle;
    __SetIOMode( fhandle, _READ | _WRITE );
    return( 0 );
}
Пример #13
0
static void ioSuppTempOpen(             // OPEN TEMPORARY FILE
    void )
{
    int         mode;
    auto char   fname[ _MAX_PATH ];

    mode = O_RDWR | O_CREAT | O_EXCL;
#ifdef __UNIX__
  #ifndef O_TEMP
    #define O_TEMP 0    /* Not a standard flag */
  #endif
    // Unix files are always binary
    mode |= O_TEMP;
#else
    mode |= O_BINARY;
#endif
    for(;;) {
        tempFname( fname );
        #if defined(__DOS__)
        {   tiny_ret_t  rc;
            rc = TinyCreateNew( fname, 0 );
            if( TINY_ERROR( rc ) ) {
                temphandle = -1;
            } else {
                temphandle = TINY_INFO( rc );
                __SetIOMode( temphandle, _READ | _WRITE | _BINARY );
            }
        }
        #else
            temphandle = open( fname, mode, S_IRUSR | S_IWUSR );
        #endif
        if( temphandle != -1 ) break;
        if( workFile[5] == 'Z' ) {
            temphandle = -1;
            break;
        }
        switch( workFile[5] ) {
        case '9':
            workFile[5] = 'A';
            break;
        case 'I':
            workFile[5] = 'J';  /* file-system may be EBCDIC */
            break;
        case 'R':
            workFile[5] = 'S';  /* file-system may be EBCDIC */
            break;
        default:
            ++workFile[5];
            break;
        }
    }
    #if defined(__UNIX__)
        /* Under POSIX it's legal to remove a file that's open. The file
           space will be reclaimed when the handle is closed. This makes
           sure that the work file always gets removed. */
        remove( fname );
        tempname = NULL;
    #else
        tempname = FNameAdd( fname );
    #endif
    if( temphandle == -1 ) {
        ioSuppError( ERR_UNABLE_TO_OPEN_WORK_FILE );
    }
}
Пример #14
0
static int __F_NAME(__sopen,__wsopen)( const CHAR_TYPE *name, int mode,
                                       int shflag, va_list args )
{
    int         rwmode;
    int         handle;
    int         attr;
    int         permission;
    unsigned    iomode_flags;
    tiny_ret_t  rc;
    char        dummy;
#ifdef __WIDECHAR__
    char        mbName[MB_CUR_MAX * _MAX_PATH];     /* single-byte char */
#endif

    handle = -1;
    rc = 0;
    while( *name == STRING( ' ' ) )
        ++name;
#ifdef __WIDECHAR__
    /*** If necessary, convert the wide filename to multibyte form ***/
    if( wcstombs( mbName, name, sizeof( mbName ) ) == -1 ) {
        mbName[0] = '\0';
    }
#endif
    rwmode = mode & ( O_RDONLY | O_WRONLY | O_RDWR | O_NOINHERIT );
    if( _dos_open( __F_NAME(name,mbName), rwmode | shflag, &handle ) == 0 ) {
        if( handle >= __NFiles ) {
            TinyClose( handle );
            __set_errno( EMFILE );
            return( -1 );
        }
    }
                                        /* 17-apr-90   05-sep-91 */
    if( (mode & (O_RDONLY | O_WRONLY | O_RDWR)) != O_RDONLY ) {
        if( handle != -1 ) {
            if( ! isatty( handle ) ) {      /* if not a device */
#if 0
                rc = TinyAccess( name, 0 ); /* check for existence */
                if( TINY_ERROR( rc ) ) {    /* file does not exist */
                    TinyClose( handle );    /* close whatever file we got */
                    handle = -1;
                } else if( mode & O_EXCL ) {    /* must not exist */
#else
    /*
    Don't need to do the access check, since the file was opened
    and therefore must exist (TinyOpen can't create a file).
    We don't want to do the check because there are classes of items
    in the file system namespace that are not devices, but the TinyAccess
    will fail on (e.g. named pipes).
    */
                /* must not exist if O_CREAT specified */
                if( (mode & O_EXCL) && (mode & O_CREAT) ) {
#endif
                    TinyClose( handle );
                    __set_errno( EEXIST );
                    return( -1 );
                } else if( mode & O_TRUNC ) {   /* truncate file */
                    rc = TinyWrite( handle, &dummy, 0 );
                    if( TINY_ERROR( rc ) ) {
                        TinyClose( handle );
                        return( __set_errno_dos( TINY_INFO( rc ) ) );
                    }
                }
            }
        }
    }
    if( handle == -1 ) {                    /* could not open */
        if( (mode & O_CREAT) == 0 || _RWD_doserrno != E_nofile ) {
            return( -1 );
        }
        /* creating the file */
        permission = va_arg( args, int );
        va_end( args );
        if( permission == 0 )
            permission = S_IWRITE | S_IREAD;
        permission &= ~_RWD_umaskval;               /* 05-jan-95 */
        attr = 0;
        if(( permission & S_IWRITE) == 0 )
            attr = _A_RDONLY;
        #if 0
            /* remove this support because it is not consistently available */
            if( _RWD_osmajor >= 5
                #ifdef __DOS_EXT__
                    && !_IsFlashTek()
                    && !_IsRational()
                #endif
                ) {
                /* this function is only available in version DOS 5 and up */
                /* this new way was added to handle the case of creating a */
                /* new file with read-only access, but with a writeable */
                /* file handle */
                #ifdef __WIDECHAR__
                    rc = TinyCreateEx( mbName, rwmode|shflag, attr, TIO_OPEN );
                #else
                    rc = TinyCreateEx( name, rwmode|shflag, attr, TIO_OPEN );
                #endif
                if( TINY_ERROR( rc ) ) {
                    return( __set_errno_dos( TINY_INFO( rc ) ) );
                }
                handle = TINY_INFO( rc );
            } else
        #endif
        {
            /* do it the old way */
            if( _dos_creat( __F_NAME(name,mbName), attr, &handle ) ) {
                return( -1 );
            }
            if( handle >= __NFiles ) {
                TinyClose( handle );
                __set_errno( EMFILE );
                return( -1 );
            }

            /* 21-nov-90 AFS: the file is created so now the file must be */
            /*                    opened with the correct share permissions */
            if( shflag != 0 ) {
                rc = TinyClose( handle );
                if( TINY_ERROR( rc ) ) {
                    return( __set_errno_dos( TINY_INFO( rc ) ) );
                }
                if( _dos_open( __F_NAME(name,mbName), rwmode | shflag, &handle ) ) {
                    return( -1 );
                }
                /* handle does not equal -1 now */
            }
        }
    }
    iomode_flags = __GetIOMode( handle );
    iomode_flags &= ~(_READ|_WRITE|_APPEND|_BINARY);     /* 11-aug-88 */
    if( isatty( handle ) )              iomode_flags |= _ISTTY;
    rwmode &= ~O_NOINHERIT;
    if( rwmode == O_RDWR )              iomode_flags |= _READ | _WRITE;
    if( rwmode == O_RDONLY)             iomode_flags |= _READ;
    if( rwmode == O_WRONLY)             iomode_flags |= _WRITE;
    if( mode & O_APPEND )               iomode_flags |= _APPEND;
    if( mode & (O_BINARY|O_TEXT) ) {
        if( mode & O_BINARY )           iomode_flags |= _BINARY;
    } else {
        if( _RWD_fmode == O_BINARY )    iomode_flags |= _BINARY;
    }
    __SetIOMode( handle, iomode_flags );
#ifdef DEFAULT_WINDOWING
    if( _WindowsNewWindow != 0 ) {
        if( !__F_NAME(stricmp,wcscmp)( name, STRING( "con" ) ) ) {
            _WindowsNewWindow( NULL, handle, -1 );
        }
    }
#endif
    return( handle );
}


#if 0 /* couldn't find any user; please re-enable if it's necessary */
#ifndef __WIDECHAR__                    /* compile one version only */
int __set_binary( int handle )
{
    unsigned        iomode_flags;

    __ChkTTYIOMode( handle );
    iomode_flags = __GetIOMode( handle );
    iomode_flags |= _BINARY;
    __SetIOMode( handle, iomode_flags );
    if( iomode_flags & _ISTTY ) {
        tiny_ret_t rc;

        rc = TinyGetDeviceInfo( handle );
        if( TINY_ERROR( rc ) ) {
            return( __set_errno_dos( TINY_INFO( rc ) ) );
        }
        rc = TinySetDeviceInfo( handle, TINY_INFO(rc) | TIO_CTL_RAW );
        if( TINY_ERROR( rc ) ) {
            return( __set_errno_dos( TINY_INFO( rc ) ) );
        }
    }
    return( 0 );
}