Scheme 48 Manual | Contents | In Chapter: Access to POSIX
Previous: I/O | Next: I/O

## I/O

These procedures are in structures `posix-i/o` and `posix`.

- `(open-pipe) -> input-port + output-port`

`Open-pipe` creates a new pipe and returns the two ends as an input port and an output port.

A file descriptor port (or `fd-port`) is a port that reads to or writes from an OS file descriptor. Fd-ports are returned by `open-input-file`, `open-output-file`, `open-file`, `open-pipe`, and other procedures.

- `(fd-port? port) -> boolean`
- `(port->fd port) -> integer or `#f``

`Fd-port?` returns true if its argument is an fd-port. `Port->fd` returns the file descriptor associated with or `#f` if `port` is not an fd-port.

- `(remap-file-descriptors fd-spec ...)`

`Remap-file-descriptors` reassigns file descriptors to ports. The `fd-specs` indicate which port is to be mapped to each file descriptor: the first gets file descriptor `0`, the second gets `1`, and so forth. A `fd-spec` is either a port that reads from or writes to a file descriptor, or `#f`, with `#f` indicating that the corresponding file descriptor is not used. Any open ports not listed are marked `close-on-exec'. The same port may be moved to multiple new file descriptors.

For example,

>     (remap-file-descriptors (current-output-port)
>                             #f
>                             (current-input-port))

moves the current output port to file descriptor `0` and the current input port to file descriptor `2`.

- `(dup fd-port) -> fd-port`
- `(dup-switching-mode fd-port) -> fd-port`
- `(dup2 fd-port file-descriptor) -> fd-port`

These change `fd-port`'s file descriptor and return a new port that uses `ports`'s old file descriptor. `Dup` uses the lowest unused file descriptor and `dup2` uses the one provided. `Dup-switching-mode` is the same as `dup` except that the returned port is an input port if the argument was an output port and vice versa. If any existing port uses the file descriptor passed to `dup2`, that port is closed.

- `(close-all-but port ...)`

`Close-all-but` closes all file descriptors whose associated ports are not passed to it as arguments.

- `(close-on-exec? port) -> boolean`
- `(set-close-on-exec?! port boolean)`

`Close-on-exec?` returns true if `port` will be closed when a new program is exec'ed. `Set-close-on-exec?!` sets `port`'s close-on-exec flag.

- `(i/o-flags port) -> file-options`
- `(set-i/o-flags! port file-options)`

These two procedures read and write various options for `port`. The options that can be read are `append`, `nonblocking`, `read-only`, `write-only`, and `read/write`. Only the `append` and `nonblocking` can be written.

- `(port-is-a-terminal? port) -> boolean`
- `(port-terminal-name port) -> string`

`Port-is-a-terminal?` returns true if `port` has an underlying file descriptor that is associated with a terminal. For such ports `port-terminal-name` returns the name of the terminal, for all others it returns `#f`.

Previous: I/O | Next: I/O