1
0
mirror of https://github.com/janet-lang/janet synced 2025-04-15 23:33:17 +00:00

Use win32 DuplicatHandle instead of _dup

There are constraints when using the posix API on win32
that aren't present with normal win32.
This commit is contained in:
Calvin Rose 2025-04-11 21:36:08 -05:00
parent 92a852f2df
commit b27c830d90
2 changed files with 11 additions and 8 deletions

View File

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

View File

@ -1254,7 +1254,7 @@ static Janet os_execute_impl(int32_t argc, Janet *argv, JanetExecuteMode mode) {
} else if (new_in != JANET_HANDLE_NONE) {
startupInfo.hStdInput = new_in;
} else {
startupInfo.hStdInput = (HANDLE) _get_osfhandle(0);
startupInfo.hStdInput = (HANDLE) _get_osfhandle(_fileno(stdin));
}
if (pipe_out != JANET_HANDLE_NONE) {
@ -1262,7 +1262,7 @@ static Janet os_execute_impl(int32_t argc, Janet *argv, JanetExecuteMode mode) {
} else if (new_out != JANET_HANDLE_NONE) {
startupInfo.hStdOutput = new_out;
} else {
startupInfo.hStdOutput = (HANDLE) _get_osfhandle(1);
startupInfo.hStdOutput = (HANDLE) _get_osfhandle(_fileno(stdout));
}
if (pipe_err != JANET_HANDLE_NONE) {
@ -1272,7 +1272,7 @@ static Janet os_execute_impl(int32_t argc, Janet *argv, JanetExecuteMode mode) {
} else if (stderr_is_stdout) {
startupInfo.hStdError = startupInfo.hStdOutput;
} else {
startupInfo.hStdError = (HANDLE) _get_osfhandle(2);
startupInfo.hStdError = (HANDLE) _get_osfhandle(_fileno(stderr));
}
int cp_failed = 0;