xref: /freebsd/lib/libc/gen/dup3.c (revision d5bf9eb5184a1d6ce35335fd2d3448a9ab7c1fd2)
167560dcfSJilles Tjoelker /*-
267560dcfSJilles Tjoelker  * Copyright (c) 2012 Jukka A. Ukkonen
367560dcfSJilles Tjoelker  * All rights reserved.
467560dcfSJilles Tjoelker  *
567560dcfSJilles Tjoelker  * This software was developed by Jukka Ukkonen for FreeBSD.
667560dcfSJilles Tjoelker  *
767560dcfSJilles Tjoelker  * Redistribution and use in source and binary forms, with or without
867560dcfSJilles Tjoelker  * modification, are permitted provided that the following conditions
967560dcfSJilles Tjoelker  * are met:
1067560dcfSJilles Tjoelker  * 1. Redistributions of source code must retain the above copyright
1167560dcfSJilles Tjoelker  *    notice, this list of conditions and the following disclaimer.
1267560dcfSJilles Tjoelker  * 2. Redistributions in binary form must reproduce the above copyright
1367560dcfSJilles Tjoelker  *    notice, this list of conditions and the following disclaimer in the
1467560dcfSJilles Tjoelker  *    documentation and/or other materials provided with the distribution.
1567560dcfSJilles Tjoelker  *
1667560dcfSJilles Tjoelker  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1767560dcfSJilles Tjoelker  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1867560dcfSJilles Tjoelker  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1967560dcfSJilles Tjoelker  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2067560dcfSJilles Tjoelker  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2167560dcfSJilles Tjoelker  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2267560dcfSJilles Tjoelker  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2367560dcfSJilles Tjoelker  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2467560dcfSJilles Tjoelker  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2567560dcfSJilles Tjoelker  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2667560dcfSJilles Tjoelker  * SUCH DAMAGE.
2767560dcfSJilles Tjoelker  */
2867560dcfSJilles Tjoelker 
2967560dcfSJilles Tjoelker #include <sys/cdefs.h>
3067560dcfSJilles Tjoelker __FBSDID("$FreeBSD$");
3167560dcfSJilles Tjoelker 
3267560dcfSJilles Tjoelker #include "namespace.h"
3367560dcfSJilles Tjoelker #include <unistd.h>
3467560dcfSJilles Tjoelker #include <fcntl.h>
3567560dcfSJilles Tjoelker #include <errno.h>
3667560dcfSJilles Tjoelker #include "un-namespace.h"
3767560dcfSJilles Tjoelker 
38*d5bf9eb5SCraig Rodrigues int __dup3(int, int, int);
39e0b54d01SCraig Rodrigues 
4067560dcfSJilles Tjoelker int
4167560dcfSJilles Tjoelker __dup3(int oldfd, int newfd, int flags)
4267560dcfSJilles Tjoelker {
4367560dcfSJilles Tjoelker 	int how;
4467560dcfSJilles Tjoelker 
4567560dcfSJilles Tjoelker 	if (oldfd == newfd) {
4667560dcfSJilles Tjoelker 		errno = EINVAL;
4767560dcfSJilles Tjoelker 		return (-1);
4867560dcfSJilles Tjoelker 	}
4967560dcfSJilles Tjoelker 
5067560dcfSJilles Tjoelker 	if (flags & ~O_CLOEXEC) {
5167560dcfSJilles Tjoelker 		errno = EINVAL;
5267560dcfSJilles Tjoelker 		return (-1);
5367560dcfSJilles Tjoelker 	}
5467560dcfSJilles Tjoelker 
5567560dcfSJilles Tjoelker 	how = (flags & O_CLOEXEC) ? F_DUP2FD_CLOEXEC : F_DUP2FD;
5667560dcfSJilles Tjoelker 
5767560dcfSJilles Tjoelker 	return (_fcntl(oldfd, how, newfd));
5867560dcfSJilles Tjoelker }
5967560dcfSJilles Tjoelker 
6067560dcfSJilles Tjoelker __weak_reference(__dup3, dup3);
6167560dcfSJilles Tjoelker __weak_reference(__dup3, _dup3);
62