summaryrefslogtreecommitdiffstats
path: root/otherlibs/win32unix
diff options
context:
space:
mode:
Diffstat (limited to 'otherlibs/win32unix')
-rw-r--r--otherlibs/win32unix/channels.c20
-rw-r--r--otherlibs/win32unix/dup2.c6
-rw-r--r--otherlibs/win32unix/startup.c9
-rw-r--r--otherlibs/win32unix/unix.ml11
-rw-r--r--otherlibs/win32unix/unixsupport.c1
-rw-r--r--otherlibs/win32unix/unixsupport.h3
6 files changed, 30 insertions, 20 deletions
diff --git a/otherlibs/win32unix/channels.c b/otherlibs/win32unix/channels.c
index 62b428e77..176aab9f5 100644
--- a/otherlibs/win32unix/channels.c
+++ b/otherlibs/win32unix/channels.c
@@ -18,14 +18,26 @@
#include "unixsupport.h"
#include <fcntl.h>
+extern long _get_osfhandle(int);
+extern int _open_osfhandle(long, int);
+
CAMLprim value win_fd_handle(value handle)
{
- int fd = _open_osfhandle((long) Handle_val(handle), O_BINARY);
- if (fd == -1) uerror("channel_of_descr", Nothing);
+ int fd;
+ if (CRT_fd_val(handle) != NO_CRT_FD) {
+ fd = CRT_fd_val(handle);
+ } else {
+ fd = _open_osfhandle((long) Handle_val(handle), O_BINARY);
+ if (fd == -1) uerror("channel_of_descr", Nothing);
+ CRT_fd_val(handle) = fd;
+ }
return Val_int(fd);
}
-CAMLprim value win_handle_fd(value fd)
+CAMLprim value win_handle_fd(value vfd)
{
- return win_alloc_handle_or_socket((HANDLE) _get_osfhandle(Int_val(fd)));
+ int crt_fd = Int_val(vfd);
+ value res = win_alloc_handle_or_socket((HANDLE) _get_osfhandle(crt_fd));
+ CRT_fd_val(res) = crt_fd;
+ return res;
}
diff --git a/otherlibs/win32unix/dup2.c b/otherlibs/win32unix/dup2.c
index c9612620c..4be2d819f 100644
--- a/otherlibs/win32unix/dup2.c
+++ b/otherlibs/win32unix/dup2.c
@@ -16,6 +16,9 @@
#include <mlvalues.h>
#include "unixsupport.h"
+extern value win_fd_handle(value);
+extern int _dup2(int, int);
+
CAMLprim value unix_dup2(value fd1, value fd2)
{
HANDLE oldh, newh;
@@ -33,5 +36,8 @@ CAMLprim value unix_dup2(value fd1, value fd2)
else
CloseHandle(oldh);
Descr_kind_val(fd2) = Descr_kind_val(fd1);
+ /* Reflect the dup2 on the CRT fds, if any */
+ if (CRT_fd_val(fd1) != NO_CRT_FD || CRT_fd_val(fd2) != NO_CRT_FD)
+ _dup2(Int_val(win_fd_handle(fd1)), Int_val(win_fd_handle(fd2)));
return Val_unit;
}
diff --git a/otherlibs/win32unix/startup.c b/otherlibs/win32unix/startup.c
index 98ab45a99..ae584e569 100644
--- a/otherlibs/win32unix/startup.c
+++ b/otherlibs/win32unix/startup.c
@@ -41,12 +41,3 @@ CAMLprim value win_cleanup(unit)
(void) WSACleanup();
return Val_unit;
}
-
-static int std_handles[3] = {
- STD_INPUT_HANDLE, STD_OUTPUT_HANDLE, STD_ERROR_HANDLE
-};
-
-CAMLprim value win_stdhandle(value nhandle)
-{
- return win_alloc_handle_or_socket(GetStdHandle(std_handles[Int_val(nhandle)]));
-}
diff --git a/otherlibs/win32unix/unix.ml b/otherlibs/win32unix/unix.ml
index 5bec474e2..ed9c809cb 100644
--- a/otherlibs/win32unix/unix.ml
+++ b/otherlibs/win32unix/unix.ml
@@ -151,13 +151,11 @@ let nice prio = invalid_arg "Unix.nice not implemented"
(* Basic file input/output *)
-type standard_handle = STD_INPUT | STD_OUTPUT | STD_ERROR
-
-external stdhandle : standard_handle -> file_descr = "win_stdhandle"
+external filedescr_of_fd : int -> file_descr = "win_handle_fd"
-let stdin = stdhandle STD_INPUT
-let stdout = stdhandle STD_OUTPUT
-let stderr = stdhandle STD_ERROR
+let stdin = filedescr_of_fd 0
+let stdout = filedescr_of_fd 1
+let stderr = filedescr_of_fd 2
type open_flag =
O_RDONLY
@@ -200,7 +198,6 @@ external fd_of_in_channel : in_channel -> int = "channel_descriptor"
external fd_of_out_channel : out_channel -> int = "channel_descriptor"
external open_handle : file_descr -> int = "win_fd_handle"
-external filedescr_of_fd : int -> file_descr = "win_handle_fd"
let in_channel_of_descr handle =
open_read_descriptor(open_handle handle)
diff --git a/otherlibs/win32unix/unixsupport.c b/otherlibs/win32unix/unixsupport.c
index 50c756f60..a8558f816 100644
--- a/otherlibs/win32unix/unixsupport.c
+++ b/otherlibs/win32unix/unixsupport.c
@@ -52,6 +52,7 @@ value win_alloc_handle(HANDLE h)
value res = alloc_custom(&win_handle_ops, sizeof(struct filedescr), 0, 1);
Handle_val(res) = h;
Descr_kind_val(res) = KIND_HANDLE;
+ CRT_fd_val(res) = NO_CRT_FD;
return res;
}
diff --git a/otherlibs/win32unix/unixsupport.h b/otherlibs/win32unix/unixsupport.h
index fc0ace1bd..2b1ff71ea 100644
--- a/otherlibs/win32unix/unixsupport.h
+++ b/otherlibs/win32unix/unixsupport.h
@@ -31,16 +31,19 @@ struct filedescr {
SOCKET socket;
} fd;
enum { KIND_HANDLE, KIND_SOCKET } kind;
+ int crt_fd;
};
#define Handle_val(v) (((struct filedescr *) Data_custom_val(v))->fd.handle)
#define Socket_val(v) (((struct filedescr *) Data_custom_val(v))->fd.socket)
#define Descr_kind_val(v) (((struct filedescr *) Data_custom_val(v))->kind)
+#define CRT_fd_val(v) (((struct filedescr *) Data_custom_val(v))->crt_fd)
extern value win_alloc_handle_or_socket(HANDLE);
extern value win_alloc_handle(HANDLE);
extern value win_alloc_socket(SOCKET);
+#define NO_CRT_FD (-1)
#define Nothing ((value) 0)
extern void win32_maperr(unsigned long errcode);