Esempio n. 1
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. 2
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 ) 
    {
        __set_errno( EBADF );
        ret = -1;
    }
    #elif defined(__NETWARE__)

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

    return( ret );
}
Esempio n. 3
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. 4
0
_WCRTLINK int read( int handle, void *buffer, unsigned len )
{
    unsigned    total = 0;
    unsigned    readamt;
    int         rc;

    __handle_check( handle, -1 );

    while( len > 0 ) {
        if( len > MAXBUFF ) {
            readamt = MAXBUFF;
        } else {
            readamt = len;
        }
        rc = __read( handle, buffer, readamt );
        if( rc == -1 )
            return( rc );
        total += (unsigned)rc;
        if( rc != readamt )
            return( total );

        len -= readamt;
        buffer = ((char *)buffer) + readamt;
    }
    return( total );

}
Esempio n. 5
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. 6
0
int __close( int handle )
{
    tiny_ret_t rc;
#ifdef DEFAULT_WINDOWING
    LPWDATA res;
#endif
    int     rv;

    __handle_check( handle, -1 );
    rv = 0;
    rc = TinyClose( handle );
    if( TINY_OK(rc) ) {
#ifdef DEFAULT_WINDOWING
        if( _WindowsCloseWindow != 0 ) {
            res = _WindowsIsWindowedHandle( handle );
            if( res != NULL ) {
                _WindowsRemoveWindowedHandle( handle );
                _WindowsCloseWindow( res );
            }
        }
#endif
    } else {
        __set_errno( EBADF );
        rv = -1;
    }
    __SetIOMode_nogrow( handle, 0 );
    return( rv );
}
Esempio n. 7
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. 8
0
_WCRTLINK int write( int handle, const void *buffer, unsigned len )
/*****************************************************************/
{
    unsigned    total = 0;
    unsigned    writeamt;
    int         rc;

    __handle_check( handle, -1 );

    // allow file to be truncated
    if( len == 0 )
        return( __write( handle, buffer, 0 ) );

    while( len > 0 ) {
        if( len > MAXBUFF ) {
            writeamt = MAXBUFF;
        } else {
            writeamt = len;
        }
        rc = __write( handle, buffer, writeamt );
        if( rc == -1 )
            return( rc );
        total += (unsigned)rc;
        if( rc != writeamt )
            return( total );

        len -= writeamt;
        buffer = ((const char *)buffer) + writeamt;
    }
    return( total );
}
Esempio n. 9
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. 10
0
_WCRTLINK int chsize( int handle, long size )
{
    long        curOffset, endOffset;
    char        buf[512];
    long        diff;
    unsigned    amount;
    int         retCode;

    __handle_check( handle, -1 );

    /*** Prepare to change the size ***/
    _AccessFileH( handle );
    curOffset = __lseek( handle, 0L, SEEK_CUR );  /* get current offset */
    if( curOffset == -1 ) {
        _ReleaseFileH( handle );
        return( -1 );
    }
    endOffset = __lseek( handle, 0L, SEEK_END );  /* get file size */
    if( endOffset == -1 ) {
        _ReleaseFileH( handle );
        return( -1 );
    }

    if( size > endOffset ) {
        /*** Increase file size ***/
        diff = size - endOffset;                /* number of bytes to pad */
        memset( buf, 0, 512 );                  /* zero buffer */
        do {
            if( diff >= 512 ) {
                amount = 512;
            } else {
                amount = (unsigned)diff;
            }
            retCode = write( handle, buf, amount );
            if( retCode != amount ) {
                __set_errno( ENOSPC );
                retCode = -1;
                break;
            }
            diff -= amount;
        } while( diff != 0 );
        if( retCode != -1 ) retCode = 0;
    } else {
        /*** Shrink file ***/
        retCode = DosNewSize( handle, size );
        if( retCode != 0 ) {
            __set_errno_dos( retCode );
            retCode = -1;
        }
        if( curOffset > size ) curOffset = size;
    }

    /*** Clean up and go home ***/
    curOffset = __lseek( handle, curOffset, SEEK_SET );
    if( curOffset == -1 ) retCode = -1;
    _ReleaseFileH( handle );
    return( retCode );
}
Esempio n. 11
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. 12
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. 13
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. 14
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. 15
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. 16
0
_WCRTLINK int eof( int handle )         /* determine if at EOF */
{
    long current_posn, file_len;

    __handle_check( handle, -1 );
    file_len = filelength( handle );
    if( file_len == -1L )
        return( -1 );
    current_posn = tell( handle );
    if( current_posn == -1L )
        return( -1 );
    if( current_posn == file_len )
        return( 1 );
    return( 0 );
}
Esempio n. 17
0
_WCRTLINK int isatty( int handle )
{
    tiny_ret_t rc;

    __handle_check( handle, 0 );
#ifdef DEFAULT_WINDOWING
    if( _WindowsIsWindowedHandle != 0 ) {
        if( _WindowsIsWindowedHandle( handle ) ) {
            return( 1 );
        }
    }
#endif
    rc = TinyGetDeviceInfo( handle );
    return( ( TINY_INFO( rc ) & TIO_CTL_DEVICE ) != 0 );
}
Esempio n. 18
0
_WCRTLINK off_t lseek( int handle, off_t offset, int origin )
{
#if !defined( __LINUX__ )
    unsigned            iomode_flags;

    __handle_check( handle, -1 );

    /*** Set the _FILEEXT iomode_flags bit if positive offset ***/
    iomode_flags = __GetIOMode( handle );

    if( offset > 0 && !(iomode_flags & _APPEND) )
        __SetIOMode_nogrow( handle, iomode_flags | _FILEEXT );

#endif
    return( __lseek( handle, offset, origin ) );
}
Esempio n. 19
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 */
}
Esempio n. 20
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 */
}
Esempio n. 21
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 );
}
Esempio n. 22
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. 23
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. 24
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
}
Esempio n. 25
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. 26
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. 27
0
_WCRTLINK int (setmode)( int handle, int mode )
{
    __handle_check( handle, -1 );
    return( O_BINARY ); /* files are always binary, no CR/LF processing */
}
Esempio n. 28
0
_WCRTLINK int fstat( int handle, struct stat *buf )
{
    unsigned        iomode_flags;
    tiny_ret_t      rc;

    __handle_check( handle, -1 );

    rc = TinyGetDeviceInfo( handle );
    if( TINY_ERROR( rc ) ) {
        return( __set_errno_dos( TINY_INFO( rc ) ) );
    }
    /* isolate drive number */
    buf->st_dev = TINY_INFO( rc ) & TIO_CTL_DISK_DRIVE_MASK;
    buf->st_rdev = buf->st_dev;

    buf->st_nlink = 1;
    buf->st_uid = buf->st_gid = 0;
    buf->st_ino = handle;
    buf->st_mode = 0;
    iomode_flags = __GetIOMode( handle );
    if( iomode_flags & _READ ) {
        buf->st_mode |= S_IRUSR | S_IRGRP | S_IROTH;
    }
    if( iomode_flags & _WRITE ) {
        buf->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
    }
    if( TINY_INFO( rc ) & TIO_CTL_DEVICE ) {
        buf->st_size = 0;
        buf->st_atime = buf->st_ctime = buf->st_mtime = 0;
        buf->st_mode |= S_IFCHR;
    } else {                /* file */
#ifdef __WATCOM_LFN__
        lfninfo_t       lfni;

        rc = 0;
        if( _RWD_uselfn && TINY_OK( rc = _getfileinfo_lfn( handle, &lfni ) ) ) {
            buf->st_mtime = _cvt_stamp2ttime_lfn( &lfni.writetimestamp );
            if( lfni.creattimestamp ) {
                buf->st_ctime = _cvt_stamp2ttime_lfn( &lfni.creattimestamp );
            } else {
                buf->st_ctime = buf->st_mtime;
            }
            if( lfni.accesstimestamp ) {
                buf->st_atime = _cvt_stamp2ttime_lfn( &lfni.accesstimestamp );
            } else {
                buf->st_atime = buf->st_mtime;
            }
            buf->st_size = lfni.lfilesize;
            buf->st_attr = lfni.attributes;
        } else if( IS_LFN_ERROR( rc ) ) {
            return( __set_errno_dos( TINY_INFO( rc ) ) );
        } else {
#endif
            long    rc1;

            rc1 = __getfilestamp_sfn( handle );
            if( rc1 == -1 ) {
                return( -1 );
            }
            buf->st_mtime = _d2ttime( rc1 >> 16, rc1 );
            buf->st_atime = buf->st_mtime;
            buf->st_ctime = buf->st_mtime;
            buf->st_size = filelength( handle );
            buf->st_attr = 0;
#ifdef __WATCOM_LFN__
        }
#endif
        buf->st_mode |= S_IFREG;
    }
    buf->st_btime = buf->st_mtime;
    buf->st_archivedID = 0;
    buf->st_updatedID = 0;
    buf->st_inheritedRightsMask = 0;
    buf->st_originatingNameSpace = 0;
    return( 0 );
}
Esempio n. 29
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. 30
0
_WCRTLINK int fstat( int handle, struct stat *buf )
#endif
{
    APIRET          error;
    OS_UINT         hand_type;
    OS_UINT         device_attr;
    FF_BUFFER       info;
    unsigned        iomode_flags;

    __handle_check( handle, -1 );

    buf->st_mode = 0;
    iomode_flags = __GetIOMode( handle );
    if( iomode_flags & _READ ) {
        buf->st_mode |= S_IRUSR | S_IRGRP | S_IROTH;
    }
    if( iomode_flags & _WRITE ) {
        buf->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
    }

    error = DosQHandType( handle, &hand_type, &device_attr );
    if( error ) {
        return( __set_errno_dos( error ) );
    }
    if( ( hand_type & ~HANDTYPE_NETWORK ) == HANDTYPE_FILE ) {
        /* handle file */
        error = DosQFileInfo( handle, FF_LEVEL, (PBYTE)&info, sizeof( info ) );
        if( error ) {
            return( __set_errno_dos( error ) );
        }
        buf->st_dev = buf->st_rdev = 0;
        /* handle timestamps */
        buf->st_ctime = _d2ttime( TODDATE( info.fdateCreation ),
                                  TODTIME( info.ftimeCreation ) );
        buf->st_atime = _d2ttime( TODDATE( info.fdateLastAccess ),
                                  TODTIME( info.ftimeLastAccess ) );
        buf->st_mtime = _d2ttime( TODDATE( info.fdateLastWrite ),
                                  TODTIME( info.ftimeLastWrite ) );
        buf->st_btime = buf->st_mtime;
#if defined( __INT64__ ) && !defined( _M_I86 )
        if( _FILEAPI64() ) {
#endif
            buf->st_attr = info.attrFile;
            buf->st_mode |= at2mode( info.attrFile );
            buf->st_size = info.cbFile;
#if defined( __INT64__ ) && !defined( _M_I86 )
        } else {
            buf->st_attr = ((FF_BUFFER_32 *)&info)->attrFile;
            buf->st_mode |= at2mode( ((FF_BUFFER_32 *)&info)->attrFile );
            buf->st_size = ((FF_BUFFER_32 *)&info)->cbFile;
        }
#endif
    } else {
        /* handle device */
        /* handle attributes */
        buf->st_attr = 0;
        buf->st_mode |= S_IRUSR | S_IRGRP | S_IROTH;
        if( ( hand_type & ~HANDTYPE_NETWORK ) == HANDTYPE_DEVICE ) {
            buf->st_mode |= S_IFCHR;
        } else if( ( hand_type & ~HANDTYPE_NETWORK ) == HANDTYPE_PIPE ) {
            buf->st_mode |= S_IFIFO;
        }
        buf->st_dev = buf->st_rdev = 1;
        /* handle timestamps */
        buf->st_atime = 0;
        buf->st_ctime = 0;
        buf->st_mtime = 0;
        buf->st_btime = 0;
        /* handle size */
        buf->st_size = 0;
    }
    buf->st_nlink = 1;
    buf->st_ino = handle;
    buf->st_uid = buf->st_gid = 0;

    buf->st_archivedID = 0;
    buf->st_updatedID = 0;
    buf->st_inheritedRightsMask = 0;
    buf->st_originatingNameSpace = 0;
    return( 0 );
}