Duplicate or replace a file descriptor.
Syntax
#include <unistd.h>
int dup2 (int oldfd, int newfd);
Parameters
-
oldfd
The file descriptor to duplicate.
-
newfd
The file new file descriptor to use. If -1, the implementation will select a free file descriptor.
Return Value
The return value is the value of the new file descriptor if successful. Otherwise, the return value is negative and errno is set:
-
EINVAL
The
oldfd
argument is invalid: Negative or outside the allowed range. -
EBADF
Bad file descriptor.
Remarks
The reference count to oldfd
will be increased by one. It may be closed and
the file will still be available at the new file descriptor.
You may use dup2
to replace the usual standard input and output with a
different file/device. In order to start a new program with a different
set of input and output files, see posix_spawn
.