Esempio n. 1
0
int __close( int hid )
{
    int         is_closed;
    int         rc;
    HANDLE      h;
#ifdef DEFAULT_WINDOWING
    LPWDATA res;
#endif

    __handle_check( hid, -1 );

    is_closed = 0;
    rc = 0;
    h = __getOSHandle( hid );

    #ifdef DEFAULT_WINDOWING
        if( _WindowsCloseWindow != 0 ) {
            res = _WindowsIsWindowedHandle( hid );
            if( res != NULL ) {
                _WindowsRemoveWindowedHandle( hid );
                _WindowsCloseWindow( res );
                is_closed = 1;
            }
        }
    #endif
    if( !is_closed && !CloseHandle( h ) ) {
        rc = __set_errno_nt();
    }
    __freePOSIXHandle( hid );
    __SetIOMode_nogrow( hid, 0 );
    return( rc );
}
Esempio n. 2
0
_WCRTLINK int chsize( int hid, long size )
{
    long        curOffset;
    long        rc;
    __file_handle *fh;

    __handle_check( hid, -1 );
    fh = (__file_handle*) __getOSHandle( hid );

    _AccessFileH( hid );
    
    curOffset = __lseek( hid, 0L, SEEK_CUR );     /* get current offset */

    rc = set_file_size(fh->name, size);
     
    if(rc !=0 )
    {
      _ReleaseFileH( hid );
      if(rc==8)
        __set_errno( ENOSPC );
      else
        __set_errno( EACCES );
      return -1;
    };     

    if( curOffset > size ) curOffset = size;
    curOffset = __lseek( hid, curOffset, SEEK_SET );
    _ReleaseFileH( hid );
    if( curOffset == -1 ) {
        __set_errno(ENOSPC);
        return( -1 );
    }
    return( 0 );
}
Esempio n. 3
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 );
}
Esempio n. 4
0
_WCRTLINK long __lseek( int handle, long offset, int origin )
{
    long            pos;

    __handle_check( handle, -1 );

#if defined( __OS2__ )
    {
        APIRET          rc;

        rc = DosChgFilePtr( handle, offset, origin, (PULONG)&pos );
        if( rc != 0 ) {
            return( __set_errno_dos( rc ) );
        }
    }
#elif defined( __NT__ )
    pos = SetFilePointer( __getOSHandle( handle ), offset, 0, origin );
    if( pos == INVALID_SET_FILE_POINTER ) {
        return( __set_errno_nt() );
    }
#elif defined( __DOS__ ) || defined( __WINDOWS__ )
    {
        tiny_ret_t      rc;

        rc = TinyLSeek( handle, offset, origin, (u32_stk_ptr)&pos );
        if( TINY_ERROR( rc ) ) {
            return( __set_errno_dos( TINY_INFO( rc ) ) );
        }
    }
#endif
    return( pos );
}
Esempio n. 5
0
_WCRTLINK int fsync( int handle )
/*******************************/
{
    int                 ret = 0;

    __handle_check( handle, -1 );

    #if defined(__DOS__) || defined(__WINDOWS__)
    ret = _dos_commit( handle );
    #elif defined(__NT__)
    if( !FlushFileBuffers( __getOSHandle( handle ) ) ) 
    {
        __set_errno_nt();
        ret = -1;
    }
    #elif defined(__OS2__)
    if( DosBufReset( handle ) != 0 ) 
    {
        _RWD_errno = EBADF;
        ret = -1;
    }
    #elif defined(__NETWARE__)

    if( FEFlushWrite( handle ) != 0 ) 
    {
        _RWD_errno = EBADF;
        ret = -1;
    }
    #else
        #error Unknown target system
    #endif

    return( ret );
}
Esempio n. 6
0
_WCRTLINK long _get_osfhandle( int posix_handle )
{
    long os_handle;

    __handle_check( posix_handle, -1 );
    os_handle = (long) __getOSHandle( posix_handle );
    return( os_handle );
}
Esempio n. 7
0
_WCRTLINK int unlock( int hid, unsigned long offset, unsigned long nbytes )
{
    __handle_check( hid, -1 );

    if( !UnlockFile( __getOSHandle( hid ), offset, 0L, nbytes, 0L ) ) {
        return( __set_errno_nt() );
    }
    return( 0 );
}
Esempio n. 8
0
_WCRTLINK unsigned _dos_commit( int hid )
{
    __handle_check( hid, ERROR_INVALID_HANDLE );

    if( !FlushFileBuffers( __getOSHandle( hid ) ) ) {
        return( __set_errno_nt_reterr() );
    }
    return( 0 );
}
Esempio n. 9
0
_WCRTLINK long filelength( int handle )
{
    __file_handle *fh;
    FILEINFO info;

    __handle_check( handle, -1 );
    fh = (__file_handle*) __getOSHandle( handle);
    get_fileinfo(fh->name,&info);

    return( info.size );
}
Esempio n. 10
0
_WCRTLINK long _get_osfhandle( int posix_handle )
{
    long os_handle;

    __handle_check( posix_handle, -1 );
    #if defined(__NT__)
        os_handle = (long) __getOSHandle( posix_handle );
    #else
        os_handle = posix_handle;
    #endif
    return( os_handle );
}
Esempio n. 11
0
_WCRTLINK unsigned _dos_close( int hid )
{
    HANDLE  h;

    h = __getOSHandle( hid );
    __SetIOMode_nogrow( hid, 0 );
    __freePOSIXHandle( hid );
    if( !CloseHandle( h ) ) {
        return( __set_errno_nt_reterr() );
    }
    return( 0 );
}
Esempio n. 12
0
int __qread( int handle, void *buffer, unsigned len )
{
#if defined( __NT__ )
    DWORD           amount_read;
#elif defined(__OS2__)
    OS_UINT         amount_read;
    APIRET          rc;
#else
    unsigned        amount_read;
    tiny_ret_t      rc;
#endif

    __handle_check( handle, -1 );
#ifdef DEFAULT_WINDOWING
    if( _WindowsStdin != NULL ) {
        LPWDATA res;

        res = _WindowsIsWindowedHandle( handle );
        if( res ) {
            int rt;
            rt = _WindowsStdin( res, buffer, len );
            return( rt );
        }
    }
#endif
#if defined(__NT__)
    if( !ReadFile( __getOSHandle( handle ), buffer, len, &amount_read, NULL ) ) {
        DWORD       err;
        err = GetLastError();
        __set_errno_dos( err );
        if( err != ERROR_BROKEN_PIPE || amount_read != 0 ) {
            return( -1 );
        }
    }
#elif defined(__OS2__)
    rc = DosRead( handle, buffer, len, &amount_read );
    if( rc ) {
        return( __set_errno_dos( rc ) );
    }
#else
  #if defined( __WINDOWS_386__ )
    rc = __TinyRead( handle, buffer, len );
  #else
    rc = TinyRead( handle, buffer, len );
  #endif
    if( TINY_ERROR( rc ) ) {
        return( __set_errno_dos( TINY_INFO( rc ) ) );
    }
    amount_read = TINY_LINFO( rc );
#endif
    return( amount_read );
}
Esempio n. 13
0
_WCRTLINK unsigned _dos_getftime( int hid, unsigned *date, unsigned *time )
{
    FILETIME        ctime, atime, wtime;
    unsigned short  d, t;

    if( GetFileTime( __getOSHandle( hid ), &ctime, &atime, &wtime ) ) {
        __MakeDOSDT( &wtime, &d, &t );
        *date = d;
        *time = t;
        return( 0 );
    }
    return( __set_errno_nt_reterr() );
}
Esempio n. 14
0
static int os_write( int handle, const void *buffer, unsigned len, unsigned *amt )
/********************************************************************************/
{
#ifdef DEFAULT_WINDOWING
    LPWDATA     res;
#endif
#if defined(__NT__)
    HANDLE      h;
    int         rc;
#else
    tiny_ret_t  rc;
#endif

    rc = 0;
#ifdef DEFAULT_WINDOWING
    if( _WindowsStdout != 0 && (res = _WindowsIsWindowedHandle( handle )) != 0 ) {
        *amt = _WindowsStdout( res, buffer, len );
    } else
#endif
    {
#if defined(__NT__)
        h = __getOSHandle( handle );
        if( !WriteFile( h, (LPCVOID)buffer, (DWORD)len, (LPDWORD)amt, NULL ) ) {
            rc = __set_errno_nt();
        }
#elif defined(__OS2_286__)
        rc = DosWrite( handle, (PVOID)buffer, (USHORT)len, (PUSHORT)amt );
#elif defined(__OS2__)
        rc = DosWrite( handle, (PVOID)buffer, (ULONG)len, (PULONG)amt );
#else
        rc = TinyWrite( handle, buffer, len );
        *amt = TINY_LINFO( rc );
        if( TINY_OK( rc ) ) {
            rc = 0;
        }
#endif
    }
#if !defined(__NT__)
    if( TINY_ERROR( rc ) ) {
        rc = __set_errno_dos( TINY_INFO( rc ) );
    }
#endif
    if( *amt != len ) {
        rc = ENOSPC;
        __set_errno( rc );
    }
    return( rc );
}
Esempio n. 15
0
static int os_write( int handle, const void *buffer, unsigned len, unsigned *amt )
/********************************************************************************/
{
    int         rc;
#ifdef DEFAULT_WINDOWING
    LPWDATA     res;
#endif
#if defined(__NT__)
    HANDLE      h;
#elif defined(__OS2__)
    APIRET      rc1;
#else
    tiny_ret_t  rc1;
#endif

    rc = 0;
#ifdef DEFAULT_WINDOWING
    if( _WindowsStdout != NULL && (res = _WindowsIsWindowedHandle( handle )) != NULL ) {
        *amt = _WindowsStdout( res, buffer, len );
    } else
#endif
    {
#if defined(__NT__)
        h = __getOSHandle( handle );
        if( !WriteFile( h, (LPCVOID)buffer, (DWORD)len, (LPDWORD)amt, NULL ) ) {
            return( __set_errno_nt() );
        }
#elif defined(__OS2__)
        rc1 = DosWrite( handle, (PVOID)buffer, (OS_UINT)len, (OS_PUINT)amt );
        if( rc1 ) {
            return( __set_errno_dos( rc1 ) );
        }
#else
        rc1 = TinyWrite( handle, buffer, len );
        if( TINY_ERROR( rc1 ) ) {
            return( __set_errno_dos( TINY_INFO( rc1 ) ) );
        }
        *amt = TINY_LINFO( rc1 );
#endif
    }
    if( *amt != len ) {
        rc = ENOSPC;
        _RWD_errno = rc;
    }
    return( rc );
}
Esempio n. 16
0
int __qwrite( int handle, const void *buffer, unsigned len )
{
    int             atomic;
    __file_handle   *fh;
    unsigned        len_written;

    __handle_check( handle, -1 );

    fh = (__file_handle*) __getOSHandle( handle );

    atomic = 0;
    if( __GetIOMode( handle ) & _APPEND )
    {
      FILEINFO info;
        
      _AccessFileH( handle );
      atomic = 1;
      get_fileinfo(fh->name,&info);
      fh->offset = info.size;
    };

    if(write_file(fh->name,buffer,fh->offset,len,&len_written))
    {
      if ( len_written == 0)
      {
        if( atomic == 1 )
          _ReleaseFileH( handle );

        return (-1);
      };   
    };
    
    fh->offset+=len_written;
        

    if( atomic == 1 )
    {
        _ReleaseFileH( handle );
    }
    return( len_written );
}
Esempio n. 17
0
  _WCRTLINK int write( int handle, const void *buffer, unsigned len )
#endif
/**********************************************************************/
{
    unsigned    iomode_flags;
    char        *buf;
    unsigned    buf_size;
    unsigned    len_written, i, j;
#if defined(__NT__)
    HANDLE      h;
    LONG        cur_ptr_low;
    LONG        cur_ptr_high;
    DWORD       rc1;
#else
    tiny_ret_t  rc1;
#endif
    int         rc2;

    __handle_check( handle, -1 );
    iomode_flags = __GetIOMode( handle );
    if( iomode_flags == 0 ) {
#if defined(__WINDOWS__) || defined(__WINDOWS_386__)
        // How can we write to the handle if we never opened it? JBS
        return( _lwrite( handle, buffer, len ) );
#else
        __set_errno( EBADF );
        return( -1 );
#endif
    }
    if( !(iomode_flags & _WRITE) ) {
        __set_errno( EACCES );     /* changed from EBADF to EACCES 23-feb-89 */
        return( -1 );
    }

#if defined(__NT__)
    h = __getOSHandle( handle );
#endif

    // put a semaphore around our writes

    _AccessFileH( handle );
    if( (iomode_flags & _APPEND) && !(iomode_flags & _ISTTY) ) {
#if defined(__NT__)
        if( GetFileType( h ) == FILE_TYPE_DISK ) {
            cur_ptr_low = 0;
            cur_ptr_high = 0;
            rc1 = SetFilePointer( h, cur_ptr_low, &cur_ptr_high, FILE_END );
            if( rc1 == INVALID_SET_FILE_POINTER ) { // this might be OK so
                if( GetLastError() != NO_ERROR ) {
                    _ReleaseFileH( handle );
                    return( __set_errno_nt() );
                }
            }
        }
#elif defined(__OS2__)
        {
            unsigned long       dummy;
            rc1 = DosChgFilePtr( handle, 0L, SEEK_END, &dummy );
            // should we explicitly ignore ERROR_SEEK_ON_DEVICE here?
        }
#else
        rc1 = TinySeek( handle, 0L, SEEK_END );
#endif
#if !defined(__NT__)
        if( TINY_ERROR( rc1 ) ) {
            _ReleaseFileH( handle );
            return( __set_errno_dos( TINY_INFO( rc1 ) ) );
        }
#endif
    }

    len_written = 0;
    rc2 = 0;

    // Pad the file with zeros if necessary
    if( iomode_flags & _FILEEXT ) {
        // turn off file extended flag
        __SetIOMode_nogrow( handle, iomode_flags&(~_FILEEXT) );

        // It is not required to pad a file with zeroes on an NTFS file system;
        // unfortunately it is required on FAT (and probably FAT32). (JBS)
        rc2 = zero_pad( handle );
    }

    if( rc2 == 0 ) {
        if( iomode_flags & _BINARY ) {  /* if binary mode */
            rc2 = os_write( handle, buffer, len, &len_written );
            /* end of binary mode part */
        } else {    /* text mode */
            i = stackavail();
            if( i < 0x00b0 ) {
                __STKOVERFLOW();    /* not enough stack space */
            }
            buf_size = 512;
            if( i < (512 + 48) ) {
                buf_size = 128;
            }
#if defined(__AXP__) || defined(__PPC__)
            buf = alloca( buf_size );
#else
            buf = __alloca( buf_size );
#endif
            j = 0;
            for( i = 0; i < len; ) {
                if( ((const char*)buffer)[i] == '\n' ) {
                    buf[j] = '\r';
                    ++j;
                    if( j == buf_size ) {
                        rc2 = os_write( handle, buf, buf_size, &j );
                        if( rc2 == -1 )
                            break;
                        len_written += j;
                        if( rc2 == ENOSPC )
                            break;
                        len_written = i;
                        j = 0;
                    }
                }
                buf[j] = ((const char*)buffer)[i];
                ++i;
                ++j;
                if( j == buf_size ) {
                    rc2 = os_write( handle, buf, buf_size, &j );
                    if( rc2 == -1 )
                        break;
                    len_written += j;
                    if( rc2 == ENOSPC )
                        break;
                    len_written = i;
                    j = 0;
                }
            }
            if( j ) {
                rc2 = os_write( handle, buf, j, &i );
                if( rc2 == ENOSPC ) {
                    len_written += i;
                } else {
                    len_written = len;
                }
            }
            /* end of text mode part */
        }
    }
    _ReleaseFileH( handle );
    if( rc2 == -1 ) {
        return( rc2 );
    } else {
        return( len_written );
    }
}
Esempio n. 18
0
  _WCRTLINK int read( int handle, void *buf, unsigned len )
#endif
{
    unsigned read_len, total_len;
    unsigned reduce_idx, finish_idx;
    unsigned iomode_flags;
    char *buffer = buf;
#if defined(__NT__)
    DWORD   amount_read;
    BOOL    rc;
    HANDLE  h;
#elif defined(__WARP__)
    ULONG amount_read;
#elif defined(__OS2_286__)
    USHORT amount_read;
#else
    unsigned amount_read;
#endif
#if !defined(__NT__)
    tiny_ret_t rc;
#endif
#ifdef DEFAULT_WINDOWING
    LPWDATA res;
#endif

    __handle_check( handle, -1 );
    __ChkTTYIOMode( handle );
    iomode_flags = __GetIOMode( handle );
    if( iomode_flags == 0 ) {
#if defined( __WINDOWS__ ) || defined( __WINDOWS_386__ )
        return( _lread( handle, buffer, len ) );
#else
        _RWD_errno = EBADF;
        return( -1 );
#endif
    }
    if( !(iomode_flags & _READ) ) {
        _RWD_errno = EACCES;     /* changed from EBADF to EACCES 23-feb-89 */
        return( -1 );
    }
#ifdef __NT__
    h = __getOSHandle( handle );
#endif
    if( iomode_flags & _BINARY ) {       /* if binary mode */
#ifdef DEFAULT_WINDOWING
        if( _WindowsStdin != 0 &&
                (res = _WindowsIsWindowedHandle( handle )) != 0 ) {
            total_len = _WindowsStdin( res, buffer, len );
            rc = 0;
        } else
#endif
        {
#if defined(__NT__)
            rc = ReadFile( h, buffer, len, &amount_read, NULL );
            total_len = amount_read;
            if( !rc ) {
                if (GetLastError() == ERROR_BROKEN_PIPE)
                    return total_len;

                return( __set_errno_nt() );
            }
#elif defined( __OS2__ )
            rc = DosRead( handle, buffer, len, &amount_read );
            total_len = amount_read;
#else
            rc = TinyRead( handle, buffer, len );
            total_len = TINY_LINFO( rc );
#endif
        }
#if !defined(__NT__)
        if( TINY_ERROR( rc ) ) {
            return( __set_errno_dos( TINY_INFO( rc ) ) );
        }
#endif
    } else {
        _AccessFileH( handle );
        total_len = 0;
        read_len = len;
        do {
#ifdef DEFAULT_WINDOWING
            if( _WindowsStdin != 0 &&
                    (res = _WindowsIsWindowedHandle( handle )) != 0L ) {
                amount_read = _WindowsStdin( res, buffer, read_len );
                rc = 0;
            } else
#endif
            {
#if defined(__NT__)
                rc = ReadFile( h, buffer, read_len, &amount_read, NULL );
                if( !rc ) {
                    _ReleaseFileH( handle );

                    if( GetLastError() == ERROR_BROKEN_PIPE )
                        return total_len;

                    return( __set_errno_nt() );
                }
#elif defined( __OS2__ )
                rc = DosRead( handle, buffer, read_len, &amount_read );
#else
                rc = TinyRead( handle, buffer, read_len );
                amount_read = TINY_LINFO( rc );
#endif
            }
#if !defined(__NT__)
            if( TINY_ERROR( rc ) ) {
                _ReleaseFileH( handle );
                return( __set_errno_dos( TINY_INFO( rc ) ) );
            }
#endif
            if( amount_read == 0 ) {                    /* EOF */
                break;
            }
            reduce_idx = 0;
            finish_idx = reduce_idx;
            for( ; reduce_idx < amount_read; ++reduce_idx ) {
                if( buffer[ reduce_idx ] == 0x1a ) {    /* EOF */
                    __lseek( handle,
                           ((long)reduce_idx - (long)amount_read)+1L,
                           SEEK_CUR );
                    total_len += finish_idx;
                    _ReleaseFileH( handle );
                    return( total_len );
                }
                if( buffer[ reduce_idx ] != '\r' ) {
                    buffer[ finish_idx++ ] = buffer[ reduce_idx ];
                }
            }
            total_len += finish_idx;
            buffer += finish_idx;
            read_len -= finish_idx;
            if( iomode_flags & _ISTTY ) {
                break;  /* 04-feb-88, FWC */
            }
        } while( read_len != 0 );
        _ReleaseFileH( handle );
    }
    return( total_len );
}
Esempio n. 19
0
static int zero_pad( int handle )           /* 09-jan-95 */
/*******************************/
{
#if defined(__NT__)
    HANDLE      h;
    DWORD       dw_ptr;
    DWORD       dw_error;
    DWORD       number_of_bytes_written;
    unsigned    write_amt;
    __i64       cur_ptr;
    __i64       end_ptr;
    BOOL        rc;
    char        zeroBuf[PAD_SIZE];

    h = __getOSHandle( handle );
    dw_error = NO_ERROR;

    cur_ptr._64 = 0;
    dw_ptr = SetFilePointer( h, cur_ptr._32[0], &cur_ptr._32[1], FILE_CURRENT );
    if( dw_ptr == INVALID_SET_FILE_POINTER ) { // this might be OK so
        dw_error = GetLastError() ;
    }
    if( dw_error != NO_ERROR )
        return( -1 );
    cur_ptr._32[0] = dw_ptr;

    end_ptr._64 = 0;
    dw_ptr = SetFilePointer( h, end_ptr._32[0], &end_ptr._32[1], FILE_END );
    if( dw_ptr == INVALID_SET_FILE_POINTER ) { // this might be OK so
        dw_error = GetLastError() ;
    }
    if( dw_error != NO_ERROR )
        return( -1 );
    end_ptr._32[0] = dw_ptr;

    memset( zeroBuf, 0x00, PAD_SIZE );

    while( end_ptr._64 < cur_ptr._64 ) {
        if( (end_ptr._64 + PAD_SIZE) < cur_ptr._64 ) {
            write_amt = PAD_SIZE;
        } else {
            write_amt = cur_ptr._64 - end_ptr._64;
        }
        rc = WriteFile( h, zeroBuf, write_amt, &number_of_bytes_written, NULL );
        dw_error = GetLastError() ;
        if( rc == 0 )
            return( -1 );
        end_ptr._64 = end_ptr._64 + write_amt;
    }

    if( cur_ptr._64 != end_ptr._64 ) {
        dw_ptr = SetFilePointer( h, cur_ptr._32[0], &cur_ptr._32[1], FILE_BEGIN );
        if( dw_ptr == INVALID_SET_FILE_POINTER ) { // this might be OK so
            dw_error = GetLastError() ;
        }
        if( dw_error != NO_ERROR ) {
            return( -1 );
        }
    }
    return( 0 );
#else
    int         rc;
    long        curPos, eodPos;
    long        bytesToWrite;
    unsigned    writeAmt;
    char        zeroBuf[PAD_SIZE];

    // Pad with zeros due to lseek() past EOF (POSIX)
    curPos = __lseek( handle, 0L, SEEK_CUR );   /* current offset */
    if( curPos == -1 )
        return( -1 );
    eodPos = __lseek( handle, 0L, SEEK_END );   /* end of data offset */
    if( eodPos == -1 )
        return( -1 );

    if( curPos > eodPos ) {
        bytesToWrite = curPos - eodPos;         /* amount to pad by */

        if( bytesToWrite > 0 ) {                /* only write if needed */
            memset( zeroBuf, 0x00, PAD_SIZE );  /* zero out a buffer */
            do {                                /* loop until done */
                if( bytesToWrite > PAD_SIZE )
                    writeAmt = 512;
                else
                    writeAmt = (unsigned)bytesToWrite;
                rc = write( handle, zeroBuf, writeAmt );
                if( rc < 0 )
                    return( rc );
                bytesToWrite -= writeAmt;       /* more bytes written */
            } while( bytesToWrite != 0 );
        }
    } else {
        curPos = __lseek( handle, curPos, SEEK_SET );
        if( curPos == -1 ) {
            return( -1 );
        }
    }

    return( 0 );                /* return success code */
#endif
}
Esempio n. 20
0
int __qwrite( int handle, const void *buffer, unsigned len )
{
    int             atomic;
#if defined(__NT__)
    DWORD           len_written;
    HANDLE          h;
    int             error;
#elif defined(__WARP__)
    ULONG           len_written;
#elif defined(__OS2_286__)
    USHORT          len_written;
#else
    unsigned        len_written;
#endif
#if !defined(__NT__)
    tiny_ret_t      rc;
#endif

    __handle_check( handle, -1 );

#if defined(__NT__)
    h = __getOSHandle( handle );
#endif
    atomic = 0;
    if( __GetIOMode( handle ) & _APPEND ) {
        _AccessFileH( handle );
        atomic = 1;
#if defined(__NT__)
        if( SetFilePointer( h, 0, NULL, FILE_END ) == -1 ) {
            error = GetLastError();
            _ReleaseFileH( handle );
            return( __set_errno_dos( error ) );
        }
#elif defined(__OS2__)
        {
            unsigned long       dummy;
            rc = DosChgFilePtr( handle, 0L, SEEK_END, &dummy );
        }
#else
        rc = TinySeek( handle, 0L, SEEK_END );
#endif
#if !defined(__NT__)
        if( TINY_ERROR( rc ) ) {
            _ReleaseFileH( handle );
            return( __set_errno_dos( TINY_INFO( rc ) ) );
        }
#endif
    }
#ifdef DEFAULT_WINDOWING
    if( _WindowsStdout != 0 ) {
        LPWDATA res;

        res = _WindowsIsWindowedHandle( handle );
        if( res ) {
            int rt;
            rt = _WindowsStdout( res, buffer, len );
            return( rt );
        }
    }
#endif
#if defined(__NT__)
    if( !WriteFile( h, buffer, len, &len_written, NULL ) ) {
        error = GetLastError();
        if( atomic == 1 ) {
            _ReleaseFileH( handle );
        }
        return( __set_errno_dos( error ) );
    }
#elif defined(__OS2__)
    rc = DosWrite( handle, (PVOID)buffer, len, &len_written );
#elif defined(__WINDOWS_386__)
    rc = __TinyWrite( handle, buffer, len );
    len_written = TINY_LINFO( rc );
#else
    rc = TinyWrite( handle, buffer, len );
    len_written = TINY_LINFO( rc );
#endif
#if !defined(__NT__)
    if( TINY_ERROR( rc ) ) {
        if( atomic == 1 ) {
            _ReleaseFileH( handle );
        }
        return( __set_errno_dos( TINY_INFO( rc ) ) );
    }
#endif
    if( len_written != len ) {
        __set_errno( ENOSPC );
    }
    if( atomic == 1 ) {
        _ReleaseFileH( handle );
    }
    return( len_written );
}
Esempio n. 21
0
_WCRTLINK int read( int handle, void *buf, unsigned len )
{
    unsigned read_len, total_len;
    unsigned reduce_idx, finish_idx;
    unsigned iomode_flags;
    char *buffer = buf;
    BOOL    rc;
    HANDLE  h;
    unsigned amount_read;
    int err;
    
    __file_handle *fh;

    
    __handle_check( handle, -1 );
    __ChkTTYIOMode( handle );
    iomode_flags = __GetIOMode( handle );
    if( iomode_flags == 0 )
    {
        __set_errno( EBADF );
        return( -1 );
    }
    if( !(iomode_flags & _READ) )
    {
        __set_errno( EACCES );     /* changed from EBADF to EACCES 23-feb-89 */
        return( -1 );
    }


    fh = (__file_handle*) __getOSHandle( handle );
 
    if( iomode_flags & _BINARY )   /* if binary mode */
    {

      err=read_file(fh->name,buffer,fh->offset,len,&amount_read);
      fh->offset+=amount_read;
      total_len = amount_read;
      
      if(err)
        if ( amount_read == 0)
          return (-1);   
      
    }
    else
    {
        total_len = 0;
        read_len = len;
        do
        {
          err=read_file(fh->name,buffer,fh->offset,len,&amount_read);
          fh->offset+=amount_read;
            
          if( amount_read == 0 )
            break;                   /* EOF */
            
          reduce_idx = 0;
          finish_idx = reduce_idx;
          for( ; reduce_idx < amount_read; ++reduce_idx )
          {
            if( buffer[ reduce_idx ] == 0x1a )     /* EOF */
            {
               __lseek( handle,
                           ((long)reduce_idx - (long)amount_read)+1L,
                           SEEK_CUR );
               total_len += finish_idx;
               return( total_len );
            }
            if( buffer[ reduce_idx ] != '\r' )
            {
                buffer[ finish_idx++ ] = buffer[ reduce_idx ];
            };    
          }

          total_len += finish_idx;
          buffer += finish_idx;
          read_len -= finish_idx;
          if( iomode_flags & _ISTTY )
          {
                break;  /* 04-feb-88, FWC */
          }
        } while( read_len != 0 );
    }
    return( total_len );
}
Esempio n. 22
0
_WCRTLINK __int64 __lseeki64( int handle, __int64 offset, int origin )
{
#if defined( __NT__ ) || defined( __OS2__ ) || defined( __LINUX__ )
    __int64         pos;

    __handle_check( handle, -1 );

  #if defined( __OS2__ )
    {
    #if !defined( _M_I86 )
        APIRET          rc;

        if( __os2_DosSetFilePtrL != NULL ) {
            rc = __os2_DosSetFilePtrL( handle, offset, origin, &pos );
            if( rc != 0 ) {
                return( __set_errno_dos( rc ) );
            }
        } else {
    #endif
            if( offset > LONG_MAX || offset < LONG_MIN ) {
                __set_errno( EINVAL );
                return( -1LL );
            }
            pos = (unsigned long)__lseek( handle, offset, origin );
            if( pos == INVALID_SET_FILE_POINTER ) {
                pos = -1LL;
            }
    #if !defined( _M_I86 )
        }
    #endif
    }
  #elif defined( __NT__ )
    {
        DWORD           rc;
        LONG            offset_hi;
        int             error;
    
        offset_hi = HIDWORD( offset );
        rc = SetFilePointer( __getOSHandle( handle ), LODWORD( offset ), &offset_hi, origin );
        if( rc == INVALID_SET_FILE_POINTER ) {  // this might be OK so
            error = GetLastError();             // check for sure JBS 04-nov-99
            if( error != NO_ERROR ) {
                return( __set_errno_dos( error ) );
            }
        }
        U64Set( (unsigned_64 *)&pos, rc, offset_hi );
    }
  #elif defined( __LINUX__ )
    if( _llseek( handle, LODWORD( offset ), HIDWORD( offset ), &pos, origin ) ) {
        pos = -1LL;
    }
  #endif
    return( pos );
#else
    long            pos;

    if( offset > LONG_MAX || offset < LONG_MIN ) {
        __set_errno( EINVAL );
        return( -1LL );
    }
    pos = __lseek( handle, offset, origin );
    if( pos == INVALID_SET_FILE_POINTER ) {
        return( -1LL );
    }
    return( (unsigned long)pos );
#endif
}