Version française
Home     About     Download     Resources     Contact us    
Browse thread
Suggested addition to Unix module
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Adam P. Jenkins <ajenkins@c...>
Subject: Suggested addition to Unix module
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;
}