static char *ReadInTrap( tiny_handle_t fh ) { dos_exe_header hdr; memptr relocbuff[NUM_BUFF_RELOCS]; unsigned relocnb; unsigned imagesize; unsigned hdrsize; rm_call_struct read; unsigned offset; if( TINY_ERROR( TinyRead( fh, &hdr, sizeof( hdr ) ) ) ) { return( TC_ERR_CANT_LOAD_TRAP ); } if( hdr.signature != DOS_SIGNATURE ) { return( TC_ERR_BAD_TRAP_FILE ); } hdrsize = hdr.hdr_size * 16; imagesize = ( hdr.file_size * 0x200 ) - (-hdr.mod_size & 0x1ff) - hdrsize; TrapMem.dpmi_adr = DPMIAllocateDOSMemoryBlock( _NBPARAS( imagesize ) + hdr.min_16 ); if( TrapMem.segm.pm == 0 ) { return( TC_ERR_OUT_OF_DOS_MEMORY ); } TinySeek( fh, hdrsize, TIO_SEEK_SET ); memset( &read, 0, sizeof( read ) ); for( offset = 0; offset < imagesize; offset += (unsigned_16)read.eax ) { read.ss = RMData.segm.rm; read.sp = offsetof( rm_data, stack ) + STACK_SIZE; read.edx = offset; read.ebx = fh; read.ds = TrapMem.segm.rm; read.ecx = imagesize - offset; read.eax = 0x3f00; #if 1 relocnb = DPMISimulateRealModeInterrupt( 0x21, 0, 0, &read ); if( (read.flags & 1) || (unsigned_16)read.eax == 0 ) { return( TC_ERR_CANT_LOAD_TRAP ); } #else read.eax = TinyRead( fh, (void *)((TrapMem.segm.rm << 4) + offset), imagesize - offset ); if( (signed_32)read.eax < 0 ) { return( TC_ERR_CANT_LOAD_TRAP ); } #endif } TinySeek( fh, hdr.reloc_offset, TIO_SEEK_SET ); for( relocnb = NUM_BUFF_RELOCS; hdr.num_relocs > 0; --hdr.num_relocs, ++relocnb ) { if( relocnb >= NUM_BUFF_RELOCS ) { if( TINY_ERROR( TinyRead( fh, relocbuff, sizeof( memptr ) * NUM_BUFF_RELOCS ) ) ) { return( TC_ERR_CANT_LOAD_TRAP ); } relocnb = 0; } *(addr_seg __far *)MK_PM( TrapMem.segm.rm + relocbuff[relocnb].s.segment, relocbuff[relocnb].s.offset ) += TrapMem.segm.rm; } return( NULL ); }
static const char *GetHelpName( const char *exe_name ) { /* if executable is: PE format, subsystem PE_SS_PL_DOSSTYLE (0x42) then PEDHELP.EXP or: PE format, other subsystems then PENHELP.EXP otherwise: PLSHELP.EXP */ tiny_ret_t rc; tiny_handle_t handle; unsigned_32 off; union { dos_exe_header dos; pe_header pe; } head; handle = -1; rc = TinyOpen( exe_name, 0 ); if( TINY_ERROR( rc ) ) goto exp; handle = TINY_INFO( rc ); TinyRead( handle, &head.dos, sizeof( head.dos ) ); if( head.dos.signature != DOS_SIGNATURE ) goto exp; TinySeek( handle, OS2_NE_OFFSET, SEEK_SET ); TinyRead( handle, &off, sizeof( off ) ); TinySeek( handle, off, SEEK_SET ); TinyRead( handle, &head.pe, sizeof( head.pe ) ); TinyClose( handle ); handle = -1; switch( head.pe.signature ) { case PE_SIGNATURE: case PL_SIGNATURE: if( head.pe.subsystem == PE_SS_PL_DOSSTYLE ) { _DBG_Writeln( "Want PEDHELP" ); return( HELPNAME_DS ); } _DBG_Writeln( "Want PENHELP" ); return( HELPNAME_NS ); } exp: if( handle != -1 ) TinyClose( handle ); _DBG_Writeln( "Want PLSHELP" ); return( HELPNAME ); }
static char *ReadInTrap( tiny_handle_t fh ) { dos_exe_header hdr; unsigned size; unsigned hdr_size; struct { unsigned_16 off; unsigned_16 seg; } buff[ NUM_BUFF_RELOCS ], __far *p; unsigned_16 start_seg; unsigned_16 __far *fixup; tiny_ret_t ret; unsigned relocs; hdr.signature = 0; if( TINY_ERROR( TinyFarRead( fh, &hdr, sizeof( hdr ) ) ) ) { return( TC_ERR_BAD_TRAP_FILE ); } if( hdr.signature != DOS_SIGNATURE ) { return( TC_ERR_BAD_TRAP_FILE ); } hdr_size = hdr.hdr_size * 16; size = (hdr.file_size * 0x200) - (-hdr.mod_size & 0x1ff) - hdr_size; ret = TinyAllocBlock( (size + 15) >> 4 ); if( TINY_ERROR( ret ) ) { return( TC_ERR_OUT_OF_DOS_MEMORY ); } start_seg = TINY_INFO( ret ); TrapCode = MK_FP( start_seg, 0 ); TinySeek( fh, hdr_size, TIO_SEEK_SET ); TinyFarRead( fh, TrapCode, size ); TinySeek( fh, hdr.reloc_offset, TIO_SEEK_SET ); p = &buff[NUM_BUFF_RELOCS]; for( relocs = hdr.num_relocs; relocs != 0; --relocs ) { if( p >= &buff[ NUM_BUFF_RELOCS ] ) { if( TINY_ERROR( TinyFarRead( fh, buff, sizeof( buff ) ) ) ) { return( TC_ERR_BAD_TRAP_FILE ); } p = buff; } fixup = MK_FP( p->seg + start_seg, p->off ); *fixup += start_seg; ++p; } return( NULL ); }
unsigned long QFileSize( f_handle file ) /*********************************************/ { unsigned long curpos; unsigned long size; curpos = QPos( file ); if( TINY_ERROR( TinyLSeek( file, 0L, TIO_SEEK_END, (void __near *)&size ) ) ) size = 0; TinySeek( file, curpos, TIO_SEEK_START ); return( size ); }
_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 ); } }
static void AddModHandle( char *name, epsp_t *epsp ) /**************************************************/ { mod_t *mod; tiny_ret_t rc; int handle; object_record obj; unsigned_32 off; os2_flat_header os2_hdr; addr_off new_base; unsigned i; if( ModHandles == NULL ) { ModHandles = malloc( sizeof( mod_t ) ); } else { ModHandles = realloc( ModHandles, ( NumModHandles + 1 ) * sizeof( mod_t ) ); } mod = &ModHandles[NumModHandles]; ++NumModHandles; mod->epsp = epsp; mod->loaded = TRUE; mod->SegCount = 0; mod->ObjInfo = NULL; if( XVersion >= 0x404 ) { name = epsp->FileName; } rc = TinyOpen( name, TIO_READ ); if( TINY_ERROR( rc ) ) { return; } handle = TINY_INFO( rc ); TinySeek( handle, OS2_NE_OFFSET, SEEK_SET ); TinyRead( handle, &off, sizeof( off ) ); TinySeek( handle, off, SEEK_SET ); TinyRead( handle, &os2_hdr, sizeof( os2_hdr ) ); TinySeek( handle, os2_hdr.objtab_off + off, SEEK_SET ); mod->SegCount = os2_hdr.num_objects; mod->ObjInfo = malloc( os2_hdr.num_objects * sizeof( seg_t ) ); new_base = 0; for( i = 0; i < os2_hdr.num_objects; ++i ) { TinyRead( handle, &obj, sizeof( obj ) ); mod->ObjInfo[i].flags = obj.flags; mod->ObjInfo[i].base = obj.addr; mod->ObjInfo[i].size = obj.size; mod->ObjInfo[i].new_base = new_base; new_base += ALIGN4K( obj.size ); if( NumModHandles == 1 ) { // main executable if( obj.flags & OBJ_BIG ) { if( obj.flags & OBJ_EXECUTABLE ) { if( flatCode == FLAT_SEL ) { flatCode = ( mod->epsp->SegBase + i * 8 ) | 3; } } else { if( flatData == FLAT_SEL ) { flatData = ( mod->epsp->SegBase + i * 8 ) | 3; } } } } } TinyClose( handle ); }
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 ); }