summaryrefslogtreecommitdiff
path: root/testsuite/tests/lib-unix/common/pipe_eof.ml
blob: 3bd6ae5f1af3e882d56267a451fc37fc85c8d5bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
(* TEST
* hasunix
include unix
** bytecode
** native
*)

let drain pipe =
  let max = 2048 in
  let buf = Buffer.create 2048 in
  let tmp = Bytes.create max in
  while begin
    try
      let len = Unix.read pipe tmp 0 max in
      Buffer.add_subbytes buf tmp 0 len;
      len > 0
    with Unix.Unix_error (Unix.EPIPE, _, _) when false ->
      false
  end do () done;
  Buffer.contents buf
;;

let run exe args =
  let out_in, out_out = Unix.pipe () in
  let err_in, err_out = Unix.pipe () in
  let args = Array.append [| exe |] args in
  let pid = Unix.create_process exe args Unix.stdin out_out err_out in
  Unix.close out_out;
  Unix.close err_out;
  let output = drain out_in in
  let error = drain err_in in
  Unix.close out_in;
  Unix.close err_in;
  let _pid, status = Unix.waitpid [ ] pid in
  status, output, error
;;

let _ =
  ignore (run "cp" [||]);
  print_endline "success"
;;