- HANDLE curr_process = GetCurrentProcess ();
- HANDLE old_handle = (HANDLE) _get_osfhandle (fd);
- HANDLE new_handle;
- int nfd;
-
- if (old_handle == INVALID_HANDLE_VALUE)
- /* fd is closed, or is open to no handle at all.
- We cannot duplicate fd in this case, because _open_osfhandle fails for
- an INVALID_HANDLE_VALUE argument. */
- return -1;
-
- if (!DuplicateHandle (curr_process, /* SourceProcessHandle */
- old_handle, /* SourceHandle */
- curr_process, /* TargetProcessHandle */
- (PHANDLE) &new_handle, /* TargetHandle */
- (DWORD) 0, /* DesiredAccess */
- FALSE, /* InheritHandle */
- DUPLICATE_SAME_ACCESS)) /* Options */
- error (EXIT_FAILURE, 0, _("DuplicateHandle failed with error code 0x%08x"),
- (unsigned int) GetLastError ());
-
- nfd = _open_osfhandle ((long) new_handle, O_BINARY | O_NOINHERIT);
- if (nfd < 0)