caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Suggested addition to Unix module
@ 1997-08-02 22:07 Adam P. Jenkins
  1997-08-25 11:55 ` Judicael Courant
  0 siblings, 1 reply; 2+ messages in thread
From: Adam P. Jenkins @ 1997-08-02 22:07 UTC (permalink / raw)
  To: caml-list

Hi, 

I'm writing to suggest that there be descr_of_fd and fd_of_descr
functions added to the Unix module, to convert between the file_descr
type and its corresponding integer file descriptor.  This shouldn't
ever be necessary in a caml program, but it can be when communicating
with another program.

I recently needed this.  I have a program (that I didn't write) which
calls another program as a subprocess, with an opened file-descriptor
as a command-line argument; the child-process then reads and writes
this file descriptor to communicate with the parent.  This is the same
thing inetd does for instance except it can use a different descriptor
than 0.

Well, in my case I wrote the program being called as a child process
in O'Caml, so I needed a way to turn the file descriptor passed on the
command-line into something I could use in OCaml.  It's really just a
casting operation since file_descr is just the fd internally.  Here's
my implementation, though it's trivial.

Also, is there a way to cast in OCaml instead of doing what I've done
here, when you know the internal representations of two types are the
same?  I realize this could be abused, turning OCaml into C, but then
what I did below is kind of silly too.  Thank you.

Adam

-- 
Adam P. Jenkins 
mailto:ajenkins@cs.umass.edu

----------------------------------------------------------------

external descr_of_fd : int -> file_descr = "unix_descr_of_fd"
external fd_of_descr : file_descr -> int = "unix_fd_of_descr"

value unix_descr_of_fd(value fd)
{
  return descr;
}

value unix_fd_of_descr(value descr)
{
  return descr;
}






^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Suggested addition to Unix module
  1997-08-02 22:07 Suggested addition to Unix module Adam P. Jenkins
@ 1997-08-25 11:55 ` Judicael Courant
  0 siblings, 0 replies; 2+ messages in thread
From: Judicael Courant @ 1997-08-25 11:55 UTC (permalink / raw)
  To: ajenkins; +Cc: caml-list


Hello,

>
>Also, is there a way to cast in OCaml instead of doing what I've done
>here, when you know the internal representations of two types are the
>same?  I realize this could be abused, turning OCaml into C, but then
>what I did below is kind of silly too.  Thank you.
>

you should consider using the function magic of module Obj... it is a
more generic implementation of your functions fd_of_descr and
descr_of_fd.

Judicael.
-- 
Judicael.Courant@ens-lyon.fr, http://www.ens-lyon.fr/~jcourant/
tel : (+33) 04 72 72 85 82
Beware of bugs in the above code; I have only proved it correct, not tried it.
                -- Donald Knuth




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~1997-08-25 13:54 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-08-02 22:07 Suggested addition to Unix module Adam P. Jenkins
1997-08-25 11:55 ` Judicael Courant

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).