summaryrefslogtreecommitdiff
path: root/otherlibs/unix/read_win32.c
blob: 71846151b572fbe683d72009c444b625eea2b7ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**************************************************************************/
/*                                                                        */
/*                                 OCaml                                  */
/*                                                                        */
/*             Xavier Leroy, projet Cristal, INRIA Rocquencourt           */
/*                                                                        */
/*   Copyright 1996 Institut National de Recherche en Informatique et     */
/*     en Automatique.                                                    */
/*                                                                        */
/*   All rights reserved.  This file is distributed under the terms of    */
/*   the GNU Lesser General Public License version 2.1, with the          */
/*   special exception on linking described in the file LICENSE.          */
/*                                                                        */
/**************************************************************************/

#include <string.h>
#include <caml/mlvalues.h>
#include <caml/memory.h>
#include <caml/signals.h>
#include "unixsupport.h"

CAMLprim value caml_unix_read(value fd, value buf, value ofs, value vlen)
{
  CAMLparam1(buf);
  intnat len;
  DWORD numbytes, numread;
  char iobuf[UNIX_BUFFER_SIZE];
  DWORD err = 0;

  len = Long_val(vlen);
  numbytes = len > UNIX_BUFFER_SIZE ? UNIX_BUFFER_SIZE : len;
  if (Descr_kind_val(fd) == KIND_SOCKET) {
    int ret;
    SOCKET s = Socket_val(fd);
    caml_enter_blocking_section();
    ret = recv(s, iobuf, numbytes, 0);
    if (ret == SOCKET_ERROR) err = WSAGetLastError();
    caml_leave_blocking_section();
    numread = ret;
  } else {
    HANDLE h = Handle_val(fd);
    caml_enter_blocking_section();
    if (! ReadFile(h, iobuf, numbytes, &numread, NULL))
      err = GetLastError();
    caml_leave_blocking_section();
  }
  if (err) {
    if (err == ERROR_BROKEN_PIPE) {
      // The write handle for an anonymous pipe has been closed. We match the
      // Unix behavior, and treat this as a zero-read instead of a Unix_error.
      err = 0;
      numread = 0;
    } else {
      caml_win32_maperr(err);
      caml_uerror("read", Nothing);
    }
  }
  memmove (&Byte(buf, Long_val(ofs)), iobuf, numread);
  CAMLreturn(Val_int(numread));
}