English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
flush file buffer, etc
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 1999-09-01 (08:00)
From: Xavier Leroy <Xavier.Leroy@i...>
Subject: Re: flush file buffer, etc
> 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.