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 ); }
_WCRTLINK int putch( int c ) { char ch; DWORD written; HANDLE h; ch = c; #ifdef DEFAULT_WINDOWING if( _WindowsPutch != 0 ) { LPWDATA res; res = _WindowsIsWindowedHandle( STDOUT_FILENO ); _WindowsPutch( res, c ); } else { #endif written = 0; h = __NTConsoleOutput(); // obtain a console output handle if( h != INVALID_HANDLE_VALUE ) { WriteConsole( h, &ch, 1, &written, NULL ); } if( written == 0 ) return( -1 ); #ifdef DEFAULT_WINDOWING } #endif return( c ); }
_WCRTLINK int getch( void ) { int c; APIRET rc; KBDKEYINFO info; if( (c = _RWD_cbyte) != 0 ) { _RWD_cbyte = 0; return( c ); } #ifdef DEFAULT_WINDOWING if( _WindowsGetch != NULL ) { // Default windowing LPWDATA res; res = _WindowsIsWindowedHandle( (int)STDIN_FILENO ); return( _WindowsGetch( res ) ); } #endif #if defined(__OS2_286__) if( _RWD_osmode == DOS_MODE ) { return( _dos( DOS_GET_CHAR_NO_ECHO_CHECK ) ); } #endif if( (c = _RWD_cbyte2) != 0 ) { _RWD_cbyte2 = 0; return( c ); } rc = KbdCharIn( &info, 0, 0 ); if( rc == ERROR_KBD_DETACHED ) return( EOF ); if( info.chChar == 0 || info.chChar == 0xe0 ) { _RWD_cbyte2 = info.chScan; } return( info.chChar ); }
_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 ); }
_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 ); }
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 ); }
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 ); }
_WCRTLINK int putch( int c ) { #ifdef DEFAULT_WINDOWING if( _WindowsPutch != NULL ) { LPWDATA res; res = _WindowsIsWindowedHandle( STDOUT_FILENO ); _WindowsPutch( res, c ); } #else _dos( DOS_OUTPUT_CHAR, c ); #endif return( c ); }
_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 ); }
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 ); }
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 ); }
_WCRTLINK int getche( void ) { int c; c = _RWD_cbyte; _RWD_cbyte = 0; if( c == 0 ) { #ifdef DEFAULT_WINDOWING if( _WindowsGetche != NULL ) { LPWDATA res; res = _WindowsIsWindowedHandle( STDOUT_FILENO ); c = _WindowsGetche( res ); } #else c = _dos( 1 ); #endif } return( c ); }
_WCRTLINK int kbhit( void ) { KBDKEYINFO info; if( _RWD_cbyte != 0 ) return( 1 ); #ifdef DEFAULT_WINDOWING if( _WindowsKbhit != NULL ) { // Default windowing LPWDATA res; res = _WindowsIsWindowedHandle( (int) STDIN_FILENO ); return( _WindowsKbhit( res ) ); } #endif #if defined(__OS2_286__) if( _RWD_osmode == DOS_MODE ) { return( _os_kbhit() ); } KbdPeek( &info, 0 ); return( ( info.fbStatus & 0xe0 ) != 0 ); #else KbdPeek( &info, 0 ); return( ( info.fbStatus & 0xe0 ) != 0 ); #endif }
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 ); }
_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 ); }