xref: /illumos-gate/usr/src/lib/libc/port/gen/dup.c (revision 5dbfd19a)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57257d1b4Sraf  * Common Development and Distribution License (the "License").
67257d1b4Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217257d1b4Sraf 
22*5dbfd19aSTheo Schlossnagle /* Copyright 2013, OmniTI Computer Consulting, Inc. All rights reserved. */
23*5dbfd19aSTheo Schlossnagle 
247c478bd9Sstevel@tonic-gate /*
258fd04b83SRoger A. Faulkner  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
267c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
307c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
317c478bd9Sstevel@tonic-gate 
327257d1b4Sraf #include 	"lint.h"
337c478bd9Sstevel@tonic-gate #include 	<sys/types.h>
347c478bd9Sstevel@tonic-gate #include	<fcntl.h>
35*5dbfd19aSTheo Schlossnagle #include	<errno.h>
367c478bd9Sstevel@tonic-gate 
378fd04b83SRoger A. Faulkner #pragma weak _dup = dup
388fd04b83SRoger A. Faulkner int
dup(int fildes)398fd04b83SRoger A. Faulkner dup(int fildes)
408fd04b83SRoger A. Faulkner {
418fd04b83SRoger A. Faulkner 	return (fcntl(fildes, F_DUPFD, 0));
428fd04b83SRoger A. Faulkner }
438fd04b83SRoger A. Faulkner 
448fd04b83SRoger A. Faulkner #pragma weak _dup2 = dup2
457c478bd9Sstevel@tonic-gate int
dup2(int fildes,int fildes2)467c478bd9Sstevel@tonic-gate dup2(int fildes, int fildes2)
477c478bd9Sstevel@tonic-gate {
487c478bd9Sstevel@tonic-gate 	return (fcntl(fildes, F_DUP2FD, fildes2));
497c478bd9Sstevel@tonic-gate }
50*5dbfd19aSTheo Schlossnagle 
51*5dbfd19aSTheo Schlossnagle int
dup3(int fildes,int fildes2,int flags)52*5dbfd19aSTheo Schlossnagle dup3(int fildes, int fildes2, int flags)
53*5dbfd19aSTheo Schlossnagle {
54*5dbfd19aSTheo Schlossnagle 	/*
55*5dbfd19aSTheo Schlossnagle 	 * The only valid flag is O_CLOEXEC.
56*5dbfd19aSTheo Schlossnagle 	 */
57*5dbfd19aSTheo Schlossnagle 	if (flags & ~O_CLOEXEC) {
58*5dbfd19aSTheo Schlossnagle 		errno = EINVAL;
59*5dbfd19aSTheo Schlossnagle 		return (-1);
60*5dbfd19aSTheo Schlossnagle 	}
61*5dbfd19aSTheo Schlossnagle 
62*5dbfd19aSTheo Schlossnagle 	/*
63*5dbfd19aSTheo Schlossnagle 	 * This call differs from dup2 such that it is an error when
64*5dbfd19aSTheo Schlossnagle 	 * fildes == fildes2
65*5dbfd19aSTheo Schlossnagle 	 */
66*5dbfd19aSTheo Schlossnagle 	if (fildes == fildes2) {
67*5dbfd19aSTheo Schlossnagle 		errno = EINVAL;
68*5dbfd19aSTheo Schlossnagle 		return (-1);
69*5dbfd19aSTheo Schlossnagle 	}
70*5dbfd19aSTheo Schlossnagle 
71*5dbfd19aSTheo Schlossnagle 	return (fcntl(fildes, (flags == 0) ? F_DUP2FD : F_DUP2FD_CLOEXEC,
72*5dbfd19aSTheo Schlossnagle 	    fildes2));
73*5dbfd19aSTheo Schlossnagle }
74