Module Os.Fd

File descriptors operations.

val unix_buffer_size : int

unix_buffer_size is the value of the OCaml runtime system buffer size for I/O operations.

Available in 5.4 as Sys.io_buffer_size.

val close_noerr : Unix.file_descr -> unit

close_noerr fd uses Unix.close on fd, retries on EINTR and silently catches any error it may raise. Typically used with Fun.protect.

Read and write convenience

val copy : ?buf:Stdlib.Bytes.t -> Unix.file_descr -> dst:Unix.file_descr -> unit

copy ~buf src ~dst reads src and writes it to dst using buf as a buffer; if unspecified a buffer of length unix_buffer_size is created for the call. Raise Unix.Unix_error if that happens.

val to_string : Unix.file_descr -> string

to_string fd reads fd to a string. Raises Unix.Unix_error in case of error.

val read_file : string -> Unix.file_descr -> string

read_file fn fd reads fd to a string assuming it is a file descriptor open on file path fn. Raises Failure in case of error with an error message that mentions fn.

Uninterrupted

val read : Unix.file_descr -> bytes -> first:int -> length:int -> int

read fd b ~first ~length reads at most length bytes of fd into b starting at first. Raises Unix.Unix_error but handles Unix.EINTR.

val write : Unix.file_descr -> bytes -> first:int -> length:int -> unit

write fd b ~first ~length writes length byte of b starting at first on fd. Returns when the bytes have been fully written. Raises Unix.Unix_error but handles Unix.EINTR.

val write_string : Unix.file_descr -> string -> unit

write_string fd s is like write but write the string s.

Terminals and pseudoterminals

val openpty : unit -> Unix.file_descr * Unix.file_descr

openpty () allocates a pseudoterminal device pair (pty, tty) with:

  • pty the (controlling) pseudoterminal device.
  • tty the (controlled) terminal device. Usually plugged into the stdio of a spawned process.

Note. Both file descriptor have close on exec set to true. But if any of these is given to a spawn's stdio explicitely, it survives closure.

See also More.Os.Pty.

TODO. Implement on Windows using conPTY.

val with_raw_mode : Unix.file_descr -> (unit -> 'a) -> ('a, string) Stdlib.result

with_raw_mode fd f sets fd's input to raw mode and calls f. When f returns the initial parameters are restored.

TODO. This will fail on Windows, see nojb's code in down