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
[Caml-list] Unix.open_process "cat"
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-05-15 (11:13)
From: Martin Jambon <martin_jambon@e...>
Subject: [Caml-list] Unix.open_process "cat"

I would like to apply some external filters (e.g. cat, grep, sed, ...)
that will terminate successfully only if they reach an
end-of-file. For this I would like to use Unix.open_process, but I need to
close the output first (= the input of the command).
Is it legal to use close_out on the out_channel returned by
Unix.open_process, and later use Unix.close_process on the pair of

Here is my code (which seems to work):

#load "unix.cma"

let feed command data f =
  let (ic, oc) as channels = Unix.open_process command in
  output_string oc data;
  close_out oc;  (** <-- is this OK? **)
     while true do
       f (input_char ic)
   with End_of_file -> ());
  match Unix.close_process channels with
      Unix.WEXITED 0 -> ()
    | _ -> invalid_arg ("feed: " ^ command)

let _ = feed "cat" "mouse" print_char


To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners