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