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 ); }
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 ); }
unsigned _dos_close( int handle ) { APIRET rc; __SetIOMode_nogrow( handle, 0 ); rc = DosClose( handle ); if( rc ) { return( __set_errno_dos_reterr( rc ) ); } return( 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 ); }
_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 ) ); }
_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 ); } }