_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 ); }
_WCRTLINK unsigned _dos_open( const char *name, unsigned mode, int *posix_handle ) { HANDLE handle; unsigned rwmode; DWORD share_mode; DWORD desired_access, os_attr; unsigned iomode_flags; int hid; // First try to get the required slot. // No point in creating a file only to not use it. JBS 99/11/01 hid = __allocPOSIXHandle( DUMMY_HANDLE ); if( hid == -1 ) { return( __set_errno_dos_reterr( ERROR_NOT_ENOUGH_MEMORY ) ); } rwmode = mode & OPENMODE_ACCESS_MASK; __GetNTAccessAttr( rwmode, &desired_access, &os_attr ); __GetNTShareAttr( mode & (OPENMODE_SHARE_MASK|OPENMODE_ACCESS_MASK), &share_mode ); handle = CreateFile( (LPTSTR) name, desired_access, share_mode, 0, OPEN_EXISTING, os_attr, NULL ); if( handle == (HANDLE)-1 ) { __freePOSIXHandle( hid ); return( __set_errno_nt_reterr() ); } // Now use the slot we got. __setOSHandle( hid, handle ); // JBS 99/11/01 *posix_handle = hid; iomode_flags = 0; if( rwmode == O_RDWR ) iomode_flags = _READ | _WRITE; if( rwmode == O_RDONLY ) iomode_flags = _READ; if( rwmode == O_WRONLY ) iomode_flags = _WRITE; __SetIOMode( hid, iomode_flags ); return( 0 ); }
_WCRTLINK int _open_osfhandle( long osfhandle, int flags ) { int posix_handle; #if defined(__NT__) // Under Win32, we get an OS handle argument posix_handle = __allocPOSIXHandle( (HANDLE)osfhandle ); if( posix_handle == -1 ) return( -1 ); #else // Under everything else, we get a POSIX handle argument posix_handle = osfhandle; #endif #if !defined(__NETWARE__) if( check_mode( posix_handle, flags ) ) { return( -1 ); } #if !defined(__UNIX__) { int rwmode; unsigned io_mode; rwmode = flags & ( O_RDONLY | O_WRONLY | O_RDWR ); io_mode = __GetIOMode( posix_handle ); io_mode &= ~(_READ|_WRITE|_APPEND|_BINARY); if( rwmode == O_RDWR ) io_mode |= _READ | _WRITE; if( rwmode == O_RDONLY) io_mode |= _READ; if( rwmode == O_WRONLY) io_mode |= _WRITE; if( flags & O_APPEND ) io_mode |= _APPEND; if( flags & O_BINARY ) { io_mode |= _BINARY; } __SetIOMode( posix_handle, io_mode ); } #endif #endif return( posix_handle ); }