'\" te .\" Copyright (c) 2003, Sun Microsystems, Inc. All Rights Reserved. .\" Copyright 1989 AT&T .TH dup2 3C "19 Dec 2003" "SunOS 5.11" "Standard C Library Functions" .SH NAME dup2 \- duplicate an open file descriptor .SH SYNOPSIS .LP .nf #include \fBint\fR \fBdup2\fR(\fBint\fR \fIfildes\fR, \fBint\fR \fIfildes2\fR); .fi .SH DESCRIPTION .sp .LP The \fBdup2()\fR function causes the file descriptor \fIfildes2\fR to refer to the same file as \fIfildes\fR. The \fIfildes\fR argument is a file descriptor referring to an open file, and \fIfildes2\fR is a non-negative integer less than the current value for the maximum number of open file descriptors allowed the calling process. See \fBgetrlimit\fR(2). If \fIfildes2\fR already refers to an open file, not \fIfildes\fR, it is closed first. If \fIfildes2\fR refers to \fIfildes\fR, or if \fIfildes\fR is not a valid open file descriptor, \fIfildes2\fR will not be closed first. .sp .LP The \fBdup2()\fR function is equivalent to \fBfcntl\fR(\fIfildes\fR, \fBF_DUP2FD\fR, \fIfildes2\fR). .SH RETURN VALUES .sp .LP Upon successful completion a non-negative integer representing the file descriptor is returned. Otherwise, \fB\(mi1\fR is returned and \fBerrno\fR is set to indicate the error. .SH ERRORS .sp .LP The \fBdup2()\fR function will fail if: .sp .ne 2 .mk .na \fB\fBEBADF\fR\fR .ad .RS 10n .rt The \fIfildes\fR argument is not a valid open file descriptor. .RE .sp .ne 2 .mk .na \fB\fBEBADF\fR\fR .ad .RS 10n .rt The \fIfildes2\fR argument is negative or is not less than the current resource limit returned by \fBgetrlimit(RLIMIT_NOFILE, .\|.\|.)\fR. .RE .sp .ne 2 .mk .na \fB\fBEINTR\fR\fR .ad .RS 10n .rt A signal was caught during the \fBdup2()\fR call. .RE .sp .ne 2 .mk .na \fB\fBEMFILE\fR\fR .ad .RS 10n .rt The process has too many open files. See \fBfcntl\fR(2). .RE .SH ATTRIBUTES .sp .LP See \fBattributes\fR(5) for descriptions of the following attributes: .sp .sp .TS tab() box; cw(2.75i) |cw(2.75i) lw(2.75i) |lw(2.75i) . ATTRIBUTE TYPEATTRIBUTE VALUE _ Interface StabilityCommitted _ MT-LevelAsync-Signal-Safe _ StandardSee \fBstandards\fR(5). .TE .SH SEE ALSO .sp .LP \fBclose\fR(2), \fBcreat\fR(2), \fBexec\fR(2), \fBfcntl\fR(2), \fBgetrlimit\fR(2), \fBopen\fR(2), \fBpipe\fR(2), \fBattributes\fR(5), \fBstandards\fR(5)