Skip to content

Commit b27c830

Browse files
committed
Use win32 DuplicatHandle instead of _dup
There are constraints when using the posix API on win32 that aren't present with normal win32.
1 parent 92a852f commit b27c830

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

src/core/ev.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3490,7 +3490,7 @@ JANET_CORE_FN(janet_cfun_ev_all_tasks,
34903490
JANET_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;

src/core/os.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,15 +1254,15 @@ static Janet os_execute_impl(int32_t argc, Janet *argv, JanetExecuteMode mode) {
12541254
} else if (new_in != JANET_HANDLE_NONE) {
12551255
startupInfo.hStdInput = new_in;
12561256
} else {
1257-
startupInfo.hStdInput = (HANDLE) _get_osfhandle(0);
1257+
startupInfo.hStdInput = (HANDLE) _get_osfhandle(_fileno(stdin));
12581258
}
12591259

12601260
if (pipe_out != JANET_HANDLE_NONE) {
12611261
startupInfo.hStdOutput = pipe_out;
12621262
} else if (new_out != JANET_HANDLE_NONE) {
12631263
startupInfo.hStdOutput = new_out;
12641264
} else {
1265-
startupInfo.hStdOutput = (HANDLE) _get_osfhandle(1);
1265+
startupInfo.hStdOutput = (HANDLE) _get_osfhandle(_fileno(stdout));
12661266
}
12671267

12681268
if (pipe_err != JANET_HANDLE_NONE) {
@@ -1272,7 +1272,7 @@ static Janet os_execute_impl(int32_t argc, Janet *argv, JanetExecuteMode mode) {
12721272
} else if (stderr_is_stdout) {
12731273
startupInfo.hStdError = startupInfo.hStdOutput;
12741274
} else {
1275-
startupInfo.hStdError = (HANDLE) _get_osfhandle(2);
1275+
startupInfo.hStdError = (HANDLE) _get_osfhandle(_fileno(stderr));
12761276
}
12771277

12781278
int cp_failed = 0;

0 commit comments

Comments
 (0)