@@ -3490,7 +3490,7 @@ JANET_CORE_FN(janet_cfun_ev_all_tasks,
34903490JANET_CORE_FN (janet_cfun_to_stream ,
34913491 "(ev/to-stream file)" ,
34923492 "Convert a core/file to a core/stream. On POSIX operating systems, this will mark "
3493- "the underlying open file description as non-blocking." ) {
3493+ "the underlying open file descriptor as non-blocking." ) {
34943494 janet_fixarity (argc , 1 );
34953495 int32_t flags = 0 ;
34963496 int32_t stream_flags = 0 ;
@@ -3500,10 +3500,13 @@ JANET_CORE_FN(janet_cfun_to_stream,
35003500 if (flags & JANET_FILE_NOT_CLOSEABLE ) stream_flags |= JANET_STREAM_NOT_CLOSEABLE ;
35013501 if (flags & JANET_FILE_CLOSED ) janet_panic ("file is closed" );
35023502#ifdef JANET_WINDOWS
3503- int fno = _fileno (file );
3504- int dupped_fno = _dup (fno );
3505- if (dupped_fno == -1 ) janet_panic (janet_strerror (errno ));
3506- JanetStream * stream = janet_stream (_get_osfhandle (dupped_fno ), stream_flags , NULL );
3503+ HANDLE handle = (HANDLE ) _get_osfhandle (_fileno (file ));
3504+ HANDLE prochandle = GetCurrentProcess ();
3505+ HANDLE dupped_handle = INVALID_HANDLE_VALUE ;
3506+ if (!DuplicateHandle (prochandle , handle , prochandle , & dupped_handle , 0 , FALSE, DUPLICATE_SAME_ACCESS )) {
3507+ janet_panic ("cannot duplicate handle to file" );
3508+ }
3509+ JanetStream * stream = janet_stream (dupped_handle , stream_flags , NULL );
35073510#else
35083511 int handle = fileno (file );
35093512 int dupped_handle = 0 ;
0 commit comments