mirror of
https://github.com/janet-lang/janet
synced 2025-04-28 13:43:19 +00:00
Fix ev/to-file
on Windows
This commit is contained in:
parent
687b987f7e
commit
17d5fb3210
@ -3290,23 +3290,30 @@ static JanetFile *get_file_for_stream(JanetStream *stream) {
|
|||||||
int currindex = index;
|
int currindex = index;
|
||||||
fmt[index++] = (currindex == 0) ? 'w' : '+';
|
fmt[index++] = (currindex == 0) ? 'w' : '+';
|
||||||
}
|
}
|
||||||
|
if (index == 0) return NULL;
|
||||||
/* duplicate handle when converting stream to file */
|
/* duplicate handle when converting stream to file */
|
||||||
#ifdef JANET_WINDOWS
|
#ifdef JANET_WINDOWS
|
||||||
HANDLE prochandle = GetCurrentProcess();
|
int htype = 0;
|
||||||
HANDLE newHandle = INVALID_HANDLE_VALUE;
|
if (fmt[0] == 'r' && fmt[1] == '+') {
|
||||||
if (!DuplicateHandle(prochandle, handle, prochandle, &newHandle, 0, FALSE, DUPLICATE_SAME_ACCESS)) {
|
htype = _O_RDWR;
|
||||||
return NULL;
|
} else if (fmt[0] == 'r') {
|
||||||
|
htype = _O_RDONLY;
|
||||||
|
} else if (fmt[0] == 'w') {
|
||||||
|
htype = _O_WRONLY;
|
||||||
}
|
}
|
||||||
FILE *f = _fdopen(newHandle, fmt);
|
int fd = _open_osfhandle((intptr_t) stream->handle, htype);
|
||||||
|
if (fd < 0) return NULL;
|
||||||
|
int fd_dup = _dup(fd);
|
||||||
|
if (fd_dup < 0) return NULL;
|
||||||
|
FILE *f = _fdopen(fd_dup, fmt);
|
||||||
if (NULL == f) {
|
if (NULL == f) {
|
||||||
_close(newHandle);
|
/* the stream isn't duplicated so this would close the stream
|
||||||
|
/* _close(fd); */
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
int newHandle = dup(stream->handle);
|
int newHandle = dup(stream->handle);
|
||||||
if (newHandle < 0) {
|
if (newHandle < 0) return NULL;
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
FILE *f = fdopen(newHandle, fmt);
|
FILE *f = fdopen(newHandle, fmt);
|
||||||
if (NULL == f) {
|
if (NULL == f) {
|
||||||
close(newHandle);
|
close(newHandle);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user