1let cat file = 2 let fd = Unix.openfile file [Unix.O_RDONLY] 0 in 3 let buf = Bytes.create 1024 in 4 let rec cat () = 5 let n = Unix.read fd buf 0 (Bytes.length buf) in 6 if n > 0 then (ignore(Unix.write Unix.stdout buf 0 n); cat ()) 7 in cat (); Unix.close fd 8 9let out fd txt = 10 ignore (Unix.write_substring fd txt 0 (String.length txt)) 11 12let _ = 13 let fd = 14 Unix.(openfile "./tmp.txt" 15 [O_WRONLY;O_TRUNC;O_CREAT;O_SHARE_DELETE] 16 0o600) in 17 out fd "---\n"; 18 Unix.dup2 ~cloexec:true fd Unix.stderr; 19 Unix.close fd; 20 out Unix.stderr "Some output\n"; 21 cat "./tmp.txt"; 22 Sys.remove "./tmp.txt" 23 24 25