ocaml-core / base / core / extended / lib_test / ktee.ml

(* OCaml-version of ktee.c in same directory *)

(* Example usage: cat /etc/termcap | ktee.exe foo1.log | cat > foo2.log *)

open Core.Std
open Core_extended.Std
open Unix
open Linux_ext

let main () =
  let ofd = openfile Sys.argv.(1) ~mode:[O_WRONLY; O_CREAT; O_TRUNC] ~perm:0o644 in
  let tee_flags = Splice.make_flags [| |] in
  let splice_flags = Splice.make_flags [| Splice.MOVE |] in
  let rec loop () =
    match
      try
        Some (
          Splice.tee ~assume_fd_is_nonblocking:true
            ~fd_in:stdin ~fd_out:stdout max_int tee_flags)
      with Unix_error (EAGAIN, _, _) -> None
    with
    | None -> loop ()
    | Some len when len = 0 -> ()
    | Some len ->
        let rec splice_loop len =
          if len > 0 then
            let slen, _, _ =
              Splice.splice ~fd_in:stdin ~fd_out:ofd ~len splice_flags
            in
            splice_loop (len - slen)
          else loop ()
        in
        splice_loop len
  in
  loop ()

let () =
  try main ()
  with exc -> printf "%s\n%!" (Exn.to_string exc)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.