p To get an independent handle with its own seek position and settings, an additional .Xr open 2 call must be issued. (This is not generally possible for pipes and sockets.)
p The .Fn dup call chooses the new descriptor: it is the lowest-numbered descriptor not currently in use. The .Fn dup2 and .Fn dup3 calls allow the caller to choose the new descriptor by passing .Fa newfd , which must be within the range of valid descriptors. If .Fa newfd is the same as .Fa oldfd , the call has no effect. Otherwise, if .Fa newfd is already in use, it is closed as if .Xr close 2 had been called.
p File descriptors are small non-negative integers that index into the per-process file table. Values 0, 1, and 2 have the special property that they are treated as standard input, standard output, and standard error respectively. (The constants .Dv STDIN_FILENO , .Dv STDOUT_FILENO , and .Dv STDERR_FILENO are provided as symbolic forms for these values.) The maximum value for a file descriptor is one less than the file table size. The file table size can be interrogated with .Xr getdtablesize 3 and can to some extent be adjusted with .Xr setrlimit 2 .
p The .Fn dup3 call includes an additional .Fa flags argument supporting a subset of the .Xr open 2 flags: l -tag -width O_NOSIGPIPE -offset indent t Dv O_CLOEXEC Set the close-on-exec flag on .Fa newfd . t Dv O_NONBLOCK Sets non-blocking I/O. t Dv O_NOSIGPIPE For pipes and sockets, do not raise .Dv SIGPIPE when a write is made to a broken pipe. Instead, the write will fail with .Er EPIPE . .El As described above, only the close-on-exec flag is per-file-descriptor, so passing any of the other .Fa flags will affect both .Fa oldfd and .Fa newfd . These settings are, however, applied atomically along with the rest of the .Fn dup3 operation.
p In the case of .Fn dup and .Fn dup2 the close-on-exec flag on the new file descriptor is always left unset and all the modes and settings of the underlying object are left unchanged.
p Functionality similar to .Fn dup with slightly different semantics is also available via .Xr fcntl 2 . .Sh RETURN VALUES These calls return the new file descriptor value. In the case of .Fn dup2 and .Fn dup3 this is always the same as .Fa newfd . If an error occurs, the value -1 is returned and .Va errno is set to indicate what happened. .Sh EXAMPLES A common use for these functions is to set up a pipe as the standard input or standard output of a subprocess. That is done approximately as follows (error handling omitted for clarity): d -literal -offset indent #include \*[Lt]unistd.h\*[Gt] int fds[2]; pid_t pid; pipe(fds); pid = fork(); if (pid == 0) { /* child; use read end of pipe to stdin */ dup2(fds[0], STDIN_FILENO); close(fds[0]); close(fds[1]); execv("/some/program", args); } /* parent process; return write end of pipe */ close(fds[0]); return fds[1]; .Ed .Sh ERRORS These functions fail if: l -tag -width Er t Bq Er EBADF .Fa oldfd is not a valid active descriptor, or for .Fn dup2 and .Fn dup3 , .Fa newfd is not in the range of valid file descriptors. t Bq Er EINVAL .Fa flags contained an invalid value. Only .Fn dup3 can generate this error. t Bq Er EMFILE Too many descriptors are active. Only .Fn dup can generate this error. .El .Sh SEE ALSO .Xr accept 2 , .Xr close 2 , .Xr fcntl 2 , .Xr getrlimit 2 , .Xr open 2 , .Xr pipe 2 , .Xr setrlimit 2 , .Xr socket 2 , .Xr socketpair 2 , .Xr getdtablesize 3 .Sh STANDARDS The .Fn dup and .Fn dup2 functions conform to .St -p1003.1-90 . .Sh HISTORY The .Fn dup3 function originated in Linux and appeared in .Nx 6.0 .