'\"macro stdmacro
.if n .pH g3c.dup2 @(#)dup2	40.10 of 10/26/89
.\" Copyright 1989 AT&T
.nr X
.if \nX=0 .ds x} dup2 3C "" "" "\&"
.if \nX=1 .ds x} dup2 3C "" ""
.if \nX=2 .ds x} dup2 3C "" "" "\&"
.if \nX=3 .ds x} dup2 3C "" "" "\&"
.TH \*(x}
.SH NAME
\f4dup2\f1 \- duplicate an open file descriptor
.SH SYNOPSIS
\f4#include <unistd.h>\f1
.PP
\f4int dup2 (int fildes, int fildes2);\f1
.SH DESCRIPTION
\f2fildes\f1 is a 
file descriptor referring to an open file, and 
\f2fildes2\f1 is a non-negative integer less than
\f4{OPEN_MAX}\f1
(the maximum number of open files).
\f4dup2\fP causes \f2fildes2\f1 to refer 
to the same file as \f2fildes\f1. If \f2fildes2\f1  
already referred to an open file, not \f2fildes\fP, it is closed first.
If \f2fildes2\fP refers to \f2fildes\fP, or if \f2fildes\fP is not a valid
open file descriptor, \f2fildes2\fP will not be closed first.
.PP
\f4dup2\fP
will fail if one or more of the following are true:
.TP 15
\f4EBADF\f1
.I fildes\^
is not a valid open
file descriptor.
.TP
\f4EBADF\f1
\f2fildes2\fP is negative or greater than or equal to \f4{OPEN_MAX}\f1.
.TP
\f4EINTR\f1
a signal was caught during the \f4dup2\fP call.
.TP
\f4%[EMFILE\f1
\f4{OPEN_MAX}\f1
file descriptors
are currently open.
.SH "SEE ALSO"
\f4creat\fP(2), \f4close\fP(2), \f4exec\fP(2),
\f4fcntl\fP(2), \f4open\fP(2), \f4pipe\fP(2), \f4lockf\fP(3C), \f4limits\fP(4).
.SH "DIAGNOSTICS"
Upon successful completion a non-negative integer,
namely, the
file descriptor,
is returned.
Otherwise, a value of \-1 is returned and
\f4errno\f1
is set to indicate the error.
.\"	@(#)dup.2	6.2 of 9/6/83
.Ee
