Re: flush file buffer, etc

From: Xavier Leroy (
Date: Tue Aug 31 1999 - 21:32:05 MET DST

Date: Tue, 31 Aug 1999 21:32:05 +0200
From: Xavier Leroy <>
To: John Skaller <>,
Subject: Re: flush file buffer, etc
In-Reply-To: <>; from John Skaller on Tue, Aug 31, 1999 at 03:36:48AM +1000

> How do I flush a Unix.file_descr?

Depends what you mean by "flush". Writes to Unix.file_descr are
unbuffered at the level of the OCaml runtime system (unlike
{in,out}_channels, which are buffered) and thus go straight to the
kernel I/O buffers. So no explicit flushing to the kernel buffers is

On the other hand, if you want to really commit the writes to the hard
disk, there is no function in the Unix module for this. But I'm not
even sure there exists a standardized Unix system call to do this.

> Deterimine if a file_descr isatty?

Your best bet is to make isatty() available in Caml. The following C
wrapper and Caml declaration are all you need:

value caml_isatty(value fd)
{ return Val_bool(isatty(Int_val(fd))); }

extern isatty : Unix.file_descr -> bool = "caml_isatty"

> Get the Unix fd as an integer?

You can't, because a Unix file descriptor is fundamentally not an
integer: it makes no sense to add or multiply two file descriptors,
for instance. For most Unix system programming tasks, an abstract
file_descr type works just fine and is so much safer.

I know there are some tasks where a correspondence between file
descriptors and integers is essential, e.g. reimplementing the Bourne
shell in OCaml. If you really must, you could define a coercion function as

    external fd_of_file_descr: Unix.file_descr -> int = "%identity"

but it's not very safe in that if the internal representation of file
descriptors in the Caml/Unix interface ever changes (e.g. becomes a
heap-allocated block rather than just an integer), your code will
break horribly.

This archive was generated by hypermail 2b29 : Sun Jan 02 2000 - 11:58:25 MET