Ejemplo 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 );
}
Ejemplo n.º 2
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 );
}
Ejemplo n.º 3
0
_WCRTLINK FILE *__F_NAME(freopen,_wfreopen)( const CHAR_TYPE *name,
                                const CHAR_TYPE *access_mode, FILE *fp )
{
    int             hdl;
    int             file_flags;
    int             extflags;

    _ValidFile( fp, 0 );

    /* validate access_mode */
    file_flags = __F_NAME(__open_flags,__wopen_flags)( access_mode, &extflags );
    if( file_flags == 0 ) {
        return( NULL );
    }

    hdl = fileno( fp );
    _AccessFileH( hdl );

#ifdef DEFAULT_WINDOWING
    if( _WindowsRemoveWindowedHandle != 0 ) {
        _WindowsRemoveWindowedHandle( hdl );
    }
#endif
    fp = close_file( fp );
    if( fp != NULL ) {
        fp->_flag &= _DYNAMIC;                      /* 24-jul-92 */
        fp = __F_NAME(__doopen,__wdoopen)( name, *access_mode,
                                           file_flags, extflags,
                                           0, fp );
    }
    _ReleaseFileH( hdl );
    return( fp );
}
Ejemplo n.º 4
0
_WCRTLINK int getch( void )
{
    int         c;
    HANDLE      h;
    DWORD       mode;

    if( (c = _RWD_cbyte) != 0 ) {
        _RWD_cbyte = 0;
        return( c );
    }
#ifdef DEFAULT_WINDOWING
    if( _WindowsGetch != NULL ) {
        LPWDATA res;
        res = _WindowsIsWindowedHandle( (int)STDIN_FILENO );
        c = _WindowsGetch( res );
    } else {
#endif
        _AccessFileH( STDIN_FILENO );
        h = __NTConsoleInput();
        GetConsoleMode( h, &mode );
        SetConsoleMode( h, 0 );
        c = do_getch( h );
        SetConsoleMode( h, mode );
        _ReleaseFileH( STDIN_FILENO );
#ifdef DEFAULT_WINDOWING
    }
#endif
    return( c );
}
Ejemplo n.º 5
0
_WCRTLINK int kbhit( void )
{
    DWORD n;
    HANDLE h;
    INPUT_RECORD r;

#ifdef DEFAULT_WINDOWING
    if( _WindowsKbhit != 0 ) {
        LPWDATA res;
        res = _WindowsIsWindowedHandle( (int) STDIN_FILENO );
        return( _WindowsKbhit( res ) );
    }
#endif
    _AccessFileH( STDIN_FILENO );
    h = __NTConsoleInput();
    for(;;) {
        PeekConsoleInput( h, &r, 1, &n );
        if( n == 0 ) break;
        if( __NTRealKey( &r ) ) break;
        // flush out mouse, window, and key up events
        ReadConsoleInput( h, &r, 1, &n );
    }
    // n != 0 if there is a key waiting
    _ReleaseFileH( STDIN_FILENO );
    return( n != 0 );
}
Ejemplo n.º 6
0
_WCRTLINK char *cgets( char *buff )
{
    char *p;
    char len;
    DWORD n;
    HANDLE h;
    INPUT_RECORD r;

#ifdef DEFAULT_WINDOWING
    if( _WindowsStdin != 0 ) {  // Default windowing...
        __qread( STDIN_FILENO, buff + 2, *buff - 1 );
        p = buff + 2;
        len = *buff;
        for(;;) {
            if( len <= 1 ) break;
            if( *p == '\r' || *p == '\0' ) break;
            ++p;
            --len;
        }
        *p = '\0';
        buff[1] = p - buff - 2;
        return( buff + 2 );
    }
#endif
    _AccessFileH( STDIN_FILENO );
    h = __NTConsoleInput();     // obtain a console input handle
    for( p = buff + 2, len = *buff; ; ) {
        ReadConsoleInput( h, &r, 1, &n );
        if( __NTRealKey( &r ) ) {       // Only interested in real keys
            if( r.Event.KeyEvent.uChar.AsciiChar == CRLF ) {
                *p = '\0';
                break;
            }
            for( ; r.Event.KeyEvent.wRepeatCount > 0;
                 --r.Event.KeyEvent.wRepeatCount ) {
                // Deal with backspace first...
                if( r.Event.KeyEvent.uChar.AsciiChar == BACKSPACE ) {
                    if( p > buff + 2 ) {
                        putch( BACKSPACE );
                        putch( SPACE );
                        putch( BACKSPACE );
                        --p;
                        ++len;
                    }
                } else if( len > 1 ) { // Other real chars...
                    *p = r.Event.KeyEvent.uChar.AsciiChar;
                    putch( r.Event.KeyEvent.uChar.AsciiChar );
                    ++p;
                    --len;
                } // Otherwise: len <= 1, can't type more.
            }
        }
    }
    _ReleaseFileH( STDIN_FILENO );
    buff[1] = p - buff - 2;
    return( buff + 2 );
}
Ejemplo n.º 7
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 );
}
Ejemplo n.º 8
0
static void initConsoleHandles( void )
{
    _AccessFileH( STDIN_FILENO );
    if( console_in == (HANDLE)-1 ) {
        console_in = CreateFile( "conin$",
                                 GENERIC_READ, FILE_SHARE_READ, NULL,
                                 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 );
    }
    if( console_out == (HANDLE)-1 ) {
        console_out = CreateFile( "conout$",
                                  GENERIC_WRITE, FILE_SHARE_WRITE, NULL,
                                  OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 );
    }
    _ReleaseFileH( STDIN_FILENO );
}
Ejemplo n.º 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 );
}
Ejemplo n.º 10
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 );
    }
}
Ejemplo n.º 11
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 );
}
Ejemplo n.º 12
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 );
}