Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

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 Archives:
Bug reports: FAQ:
Beginner's list: