1 /**************************************************************************/
2 /*                                                                        */
3 /*                                 OCaml                                  */
4 /*                                                                        */
5 /*             Xavier Leroy, projet Cristal, INRIA Rocquencourt           */
6 /*                                                                        */
7 /*   Copyright 1996 Institut National de Recherche en Informatique et     */
8 /*     en Automatique.                                                    */
9 /*                                                                        */
10 /*   All rights reserved.  This file is distributed under the terms of    */
11 /*   the GNU Lesser General Public License version 2.1, with the          */
12 /*   special exception on linking described in the file LICENSE.          */
13 /*                                                                        */
14 /**************************************************************************/
15 
16 #include <caml/mlvalues.h>
17 #include "unixsupport.h"
18 #include <caml/io.h>
19 
unix_close(value fd)20 CAMLprim value unix_close(value fd)
21 {
22   if (Descr_kind_val(fd) == KIND_SOCKET) {
23     if (closesocket(Socket_val(fd)) != 0) {
24       win32_maperr(WSAGetLastError());
25       uerror("close", Nothing);
26     }
27   } else {
28     /* If we have an fd then closing it also closes
29      * the underlying handle. Also, closing only
30      * the handle and not the fd leads to fd leaks. */
31     if (CRT_fd_val(fd) != NO_CRT_FD) {
32       if (_close(CRT_fd_val(fd)) != 0)
33          uerror("close", Nothing);
34     } else {
35       if (! CloseHandle(Handle_val(fd))) {
36         win32_maperr(GetLastError());
37         uerror("close", Nothing);
38       }
39     }
40   }
41   return Val_unit;
42 }
43