1997-09-03 07:38:02 -07:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
2011-07-27 07:17:02 -07:00
|
|
|
/* OCaml */
|
1997-09-03 07:38:02 -07:00
|
|
|
/* */
|
|
|
|
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
|
|
|
|
/* */
|
|
|
|
/* Copyright 1996 Institut National de Recherche en Informatique et */
|
1999-11-17 10:59:06 -08:00
|
|
|
/* en Automatique. All rights reserved. This file is distributed */
|
2001-12-07 05:41:02 -08:00
|
|
|
/* under the terms of the GNU Library General Public License, with */
|
|
|
|
/* the special exception on linking described in file ../../LICENSE. */
|
1997-09-03 07:38:02 -07:00
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <mlvalues.h>
|
|
|
|
#include <memory.h>
|
|
|
|
#include <signals.h>
|
|
|
|
#include "unixsupport.h"
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value unix_write(value fd, value buf, value vofs, value vlen)
|
1997-09-03 07:38:02 -07:00
|
|
|
{
|
2005-09-22 07:21:50 -07:00
|
|
|
intnat ofs, len, written;
|
1997-09-03 07:38:02 -07:00
|
|
|
DWORD numbytes, numwritten;
|
|
|
|
char iobuf[UNIX_BUFFER_SIZE];
|
2006-10-18 01:26:54 -07:00
|
|
|
DWORD err = 0;
|
1997-09-03 07:38:02 -07:00
|
|
|
|
|
|
|
Begin_root (buf);
|
|
|
|
ofs = Long_val(vofs);
|
|
|
|
len = Long_val(vlen);
|
|
|
|
written = 0;
|
|
|
|
while (len > 0) {
|
|
|
|
numbytes = len > UNIX_BUFFER_SIZE ? UNIX_BUFFER_SIZE : len;
|
2000-11-23 05:45:03 -08:00
|
|
|
memmove (iobuf, &Byte(buf, ofs), numbytes);
|
2002-04-30 08:00:48 -07:00
|
|
|
if (Descr_kind_val(fd) == KIND_SOCKET) {
|
2002-07-23 07:12:03 -07:00
|
|
|
int ret;
|
|
|
|
SOCKET s = Socket_val(fd);
|
|
|
|
enter_blocking_section();
|
|
|
|
ret = send(s, iobuf, numbytes, 0);
|
2006-10-18 01:26:54 -07:00
|
|
|
if (ret == SOCKET_ERROR) err = WSAGetLastError();
|
2002-07-23 07:12:03 -07:00
|
|
|
leave_blocking_section();
|
|
|
|
numwritten = ret;
|
2002-04-30 08:00:48 -07:00
|
|
|
} else {
|
2002-07-23 07:12:03 -07:00
|
|
|
HANDLE h = Handle_val(fd);
|
|
|
|
enter_blocking_section();
|
2006-10-18 01:17:32 -07:00
|
|
|
if (! WriteFile(h, iobuf, numbytes, &numwritten, NULL))
|
2006-10-18 01:26:54 -07:00
|
|
|
err = GetLastError();
|
2002-07-23 07:12:03 -07:00
|
|
|
leave_blocking_section();
|
2006-10-18 01:17:32 -07:00
|
|
|
}
|
2006-10-18 01:26:54 -07:00
|
|
|
if (err) {
|
|
|
|
win32_maperr(err);
|
2006-10-18 01:17:32 -07:00
|
|
|
uerror("write", Nothing);
|
1997-09-03 07:38:02 -07:00
|
|
|
}
|
|
|
|
written += numwritten;
|
|
|
|
ofs += numwritten;
|
|
|
|
len -= numwritten;
|
|
|
|
}
|
|
|
|
End_roots();
|
|
|
|
return Val_long(written);
|
|
|
|
}
|
2004-07-13 05:25:21 -07:00
|
|
|
|
|
|
|
CAMLprim value unix_single_write(value fd, value buf, value vofs, value vlen)
|
|
|
|
{
|
2005-09-22 07:21:50 -07:00
|
|
|
intnat ofs, len, written;
|
2004-07-13 05:25:21 -07:00
|
|
|
DWORD numbytes, numwritten;
|
|
|
|
char iobuf[UNIX_BUFFER_SIZE];
|
2009-09-25 08:03:06 -07:00
|
|
|
DWORD err = 0;
|
2004-07-13 05:25:21 -07:00
|
|
|
|
|
|
|
Begin_root (buf);
|
|
|
|
ofs = Long_val(vofs);
|
|
|
|
len = Long_val(vlen);
|
|
|
|
written = 0;
|
|
|
|
if (len > 0) {
|
|
|
|
numbytes = len > UNIX_BUFFER_SIZE ? UNIX_BUFFER_SIZE : len;
|
|
|
|
memmove (iobuf, &Byte(buf, ofs), numbytes);
|
|
|
|
if (Descr_kind_val(fd) == KIND_SOCKET) {
|
|
|
|
int ret;
|
|
|
|
SOCKET s = Socket_val(fd);
|
|
|
|
enter_blocking_section();
|
|
|
|
ret = send(s, iobuf, numbytes, 0);
|
2006-10-18 01:26:54 -07:00
|
|
|
if (ret == SOCKET_ERROR) err = WSAGetLastError();
|
2004-07-13 05:25:21 -07:00
|
|
|
leave_blocking_section();
|
|
|
|
numwritten = ret;
|
|
|
|
} else {
|
|
|
|
HANDLE h = Handle_val(fd);
|
|
|
|
enter_blocking_section();
|
2006-10-18 01:17:32 -07:00
|
|
|
if (! WriteFile(h, iobuf, numbytes, &numwritten, NULL))
|
2006-10-18 01:26:54 -07:00
|
|
|
err = GetLastError();
|
2004-07-13 05:25:21 -07:00
|
|
|
leave_blocking_section();
|
2006-10-18 01:17:32 -07:00
|
|
|
}
|
2006-10-18 01:26:54 -07:00
|
|
|
if (err) {
|
|
|
|
win32_maperr(err);
|
2006-10-18 01:17:32 -07:00
|
|
|
uerror("single_write", Nothing);
|
2004-07-13 05:25:21 -07:00
|
|
|
}
|
|
|
|
written = numwritten;
|
|
|
|
}
|
|
|
|
End_roots();
|
|
|
|
return Val_long(written);
|
|
|
|
}
|