xref: /onnv-gate/usr/src/lib/libbc/libc/sys/common/dup2.c (revision 3224:c0c9287a0eec)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*3224Sraf  * Common Development and Distribution License (the "License").
6*3224Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*3224Sraf 
220Sstevel@tonic-gate /*
23*3224Sraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <fcntl.h>
300Sstevel@tonic-gate #include <unistd.h>
31*3224Sraf #include <sys/syscall.h>
32*3224Sraf #include <errno.h>
33*3224Sraf 
34*3224Sraf #define	OPEN_MAX	20		/* Taken from SVR4 limits.h */
350Sstevel@tonic-gate 
36*3224Sraf int
dup2(int fildes,int fildes2)37*3224Sraf dup2(
38*3224Sraf 	int fildes,		/* file descriptor to be duplicated */
39*3224Sraf 	int fildes2)		/* desired file descriptor */
400Sstevel@tonic-gate {
41*3224Sraf 	int	tmperrno;	/* local work area */
42*3224Sraf 	int	open_max;	/* max open files */
43*3224Sraf 	int	ret;		/* return value */
44*3224Sraf 	int	fds;		/* duplicate files descriptor */
450Sstevel@tonic-gate 
46*3224Sraf 	if ((open_max = ulimit(4, 0)) < 0)
47*3224Sraf 		open_max = OPEN_MAX;	/* take a guess */
480Sstevel@tonic-gate 
49*3224Sraf 	/* Be sure fildes is valid and open */
50*3224Sraf 	if (fcntl(fildes, F_GETFL, 0) == -1) {
51*3224Sraf 		errno = EBADF;
52*3224Sraf 		return (-1);
53*3224Sraf 	}
540Sstevel@tonic-gate 
55*3224Sraf 	/* Be sure fildes2 is in valid range */
56*3224Sraf 	if (fildes2 < 0 || fildes2 >= open_max) {
57*3224Sraf 		errno = EBADF;
58*3224Sraf 		return (-1);
59*3224Sraf 	}
600Sstevel@tonic-gate 
61*3224Sraf 	/* Check if file descriptors are equal */
62*3224Sraf 	if (fildes == fildes2) {
63*3224Sraf 		/* open and equal so no dup necessary */
64*3224Sraf 		return (fildes2);
65*3224Sraf 	}
66*3224Sraf 	/* Close in case it was open for another file */
67*3224Sraf 	/* Must save and restore errno in case file was not open */
68*3224Sraf 	tmperrno = errno;
69*3224Sraf 	close(fildes2);
70*3224Sraf 	errno = tmperrno;
710Sstevel@tonic-gate 
72*3224Sraf 	/* Do the dup */
73*3224Sraf 	if ((ret = fcntl(fildes, F_DUPFD, fildes2)) != -1) {
74*3224Sraf 		if ((fds = fd_get(fildes)) != -1)
75*3224Sraf 			fd_add(fildes2, fds);
76*3224Sraf 	}
77*3224Sraf 	return (ret);
780Sstevel@tonic-gate }
79