Welcome to little lamb

Code » limb » master » tree

[master] / src / doc / djbunix.h / fd_copy.3.md

% limb manual
% fd_copy(3)
% limb 0.2.0
% 2024-01-09

# NAME

fd_copy, fd_copy2, fd_move, fd_move2 - copy/move file descriptors

# SYNOPSIS

    #include <limb/djbunix.h>

```pre hl
int fd_copy(int <em>to</em>, int <em>from</em>)
int fd_copy2(int <em>to1</em>, int <em>from1</em>, int <em>to2</em>, int <em>from2</em>)

int fd_move(int <em>to</em>, int <em>from</em>)
int fd_move2(int <em>to1</em>, int <em>from1</em>, int <em>to2</em>, int <em>from2</em>)
```

# DESCRIPTION

The `fd_copy`() function duplicates the file descriptor `from` into file
descriptor `to`, so that both refer to the same open file, that cannot be
interrupted by signals.

The `fd_copy2`() function is similar to `fd_copy`() but duplicating file
descriptor `from1` to file descriptor `to1` and file descriptor `from2` to file
descriptor `to2`.
Either both file descriptors will be copied, or none will have been.


The `fd_move`() function is similar to [fd_copy](3) but closing `from`
afterwards.

The `fd_move2`() function is similar to `fd_move`() but moving file descriptor
`from1` to file descriptor `to1` and file descriptor `from2` to file
descriptor `to2`.
Either both file descriptors will be moved, or none will have been.

It is supported to "re-use" file descriptors, e.g. `to2` can be `from1` and/or
`from2` can be `to1`, as long as it makes sense.

# RETURN VALUE

These functions return 0 on success. Otherwise they return -1 and set `errno` to
indicate the error.

# ERRORS

The `fd_copy`(), `fd_copy2`() and `fd_move2`() functions may fail if :

: *EINVAL*
:: File descriptors are invalid, e.g. `from` and `to` are the same for
:: `fd_copy`(); `to1` and `from2` are the same for `fd_copy2`(), `from1` and
:: `from2` are the same /and/ `to1` and `to2` are the same for `fd_move2`()...

These functions may also fail and set `errno` for any of the errors specified
for [dup2](3), except for *EINTR*.

# SEE ALSO

[fd_same](3)