From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by sympa.inria.fr (Postfix) with ESMTPS id C60CB8027D for ; Sat, 14 Oct 2017 17:54:16 +0200 (CEST) X-IronPort-AV: E=Sophos;i="5.43,377,1503352800"; d="scan'208";a="240993162" Received: from zmbs5.inria.fr ([128.93.142.18]) by mail3-relais-sop.national.inria.fr with ESMTP; 14 Oct 2017 17:54:16 +0200 Date: Sat, 14 Oct 2017 17:54:16 +0200 (CEST) From: Thierry Martinez To: Laurent =?iso-8859-1?Q?Th=E9venoux?= Cc: caml-list@inria.fr Message-ID: <861535789.6026880.1507996456261.JavaMail.zimbra@inria.fr> In-Reply-To: <3380AF33-0E0F-46E9-BF03-E61B65E6B838@inria.fr> References: <3380AF33-0E0F-46E9-BF03-E61B65E6B838@inria.fr> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [176.139.114.74] X-Mailer: Zimbra 8.0.9_GA_6191 (ZimbraWebClient - FF56 (Mac)/8.0.9_GA_6191) Thread-Topic: Interfacing C with OCaml and file descriptors Thread-Index: tByQR47dYcwP5hwIFIPKSTWNGCfbOQ== Subject: Re: [Caml-list] Interfacing C with OCaml and file descriptors Laurent: > So, if possible, how to convert the =E2=80=98value channel=E2=80=99 to a = =E2=80=98FILE*=E2=80=99? As the other answers already said, there does not seem to exist a portable way to do that. FWIW, here is what I do for Pyml, which appears to works both on Unix and Windows: the function file_of_file_descr takes two arguments: - an OCaml value which can be either in_channel or out_channel, - and a corresponding mode ("r" or "w", typically), and returns a FILE *. Of course, since the API for channels is not documented, this solution can break on future OCaml versions. #ifdef _WIN32 #include extern int win_CRT_fd_of_filedescr(value handle); static FILE * file_of_file_descr(value file_descr, const char *mode) { CAMLparam1(file_descr); int fd =3D win_CRT_fd_of_filedescr(file_descr); FILE *result =3D _fdopen(dup(fd), mode); CAMLreturnT(FILE *, result); } #else=20 static FILE * file_of_file_descr(value file_descr, const char *mode) { CAMLparam1(file_descr); int fd =3D Int_val(file_descr); FILE *result =3D fdopen(dup(fd), mode); CAMLreturnT(FILE *, result); } #endif Cheers. --=20 Thierry. ----- Original Message ----- > From: "Laurent Th=C3=A9venoux" > To: caml-list@inria.fr > Sent: Friday, October 13, 2017 12:57:33 PM > Subject: [Caml-list] Interfacing C with OCaml and file descriptors >=20 > Hi, >=20 > I have a naive question about file descriptors and C interface (when > interfacing C with OCaml). >=20 > For instance, a file.ml file which looks like: >=20 >=20 > external my_c_interface : out_channel -> int =3D c_function >=20 >=20 > where c_function writes something to a file and return the number of writ= ten > char. > The c_function takes a FILE* file descriptor, as for example: >=20 >=20 > CAMLprim value c_function (value channel) > { > CAMLparam1 (channel); > FILE* fd =3D ???; > CAMLreturn (Val_int (call_to_c_func (fd))); > } >=20 >=20 > So, if possible, how to convert the =E2=80=98value channel=E2=80=99 to a = =E2=80=98FILE*=E2=80=99? >=20 > Thanks for your help, > Laurent >=20 > -- > Caml-list mailing list. Subscription management and archives: > https://sympa.inria.fr/sympa/arc/caml-list > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs