1254409Sjilles/*-
2254409Sjilles * Copyright (c) 2012 Jukka A. Ukkonen
3254409Sjilles * All rights reserved.
4254409Sjilles *
5254409Sjilles * This software was developed by Jukka Ukkonen for FreeBSD.
6254409Sjilles *
7254409Sjilles * Redistribution and use in source and binary forms, with or without
8254409Sjilles * modification, are permitted provided that the following conditions
9254409Sjilles * are met:
10254409Sjilles * 1. Redistributions of source code must retain the above copyright
11254409Sjilles *    notice, this list of conditions and the following disclaimer.
12254409Sjilles * 2. Redistributions in binary form must reproduce the above copyright
13254409Sjilles *    notice, this list of conditions and the following disclaimer in the
14254409Sjilles *    documentation and/or other materials provided with the distribution.
15254409Sjilles *
16254409Sjilles * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17254409Sjilles * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18254409Sjilles * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19254409Sjilles * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20254409Sjilles * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21254409Sjilles * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22254409Sjilles * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23254409Sjilles * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24254409Sjilles * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25254409Sjilles * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26254409Sjilles * SUCH DAMAGE.
27254409Sjilles */
28254409Sjilles
29254409Sjilles#include <sys/cdefs.h>
30254409Sjilles__FBSDID("$FreeBSD$");
31254409Sjilles
32254409Sjilles#include "namespace.h"
33254409Sjilles#include <unistd.h>
34254409Sjilles#include <fcntl.h>
35254409Sjilles#include <errno.h>
36254409Sjilles#include "un-namespace.h"
37254409Sjilles
38288028Srodrigcint __dup3(int, int, int);
39288008Srodrigc
40254409Sjillesint
41254409Sjilles__dup3(int oldfd, int newfd, int flags)
42254409Sjilles{
43254409Sjilles	int how;
44254409Sjilles
45254409Sjilles	if (oldfd == newfd) {
46254409Sjilles		errno = EINVAL;
47254409Sjilles		return (-1);
48254409Sjilles	}
49254409Sjilles
50254409Sjilles	if (flags & ~O_CLOEXEC) {
51254409Sjilles		errno = EINVAL;
52254409Sjilles		return (-1);
53254409Sjilles	}
54254409Sjilles
55254409Sjilles	how = (flags & O_CLOEXEC) ? F_DUP2FD_CLOEXEC : F_DUP2FD;
56254409Sjilles
57254409Sjilles	return (_fcntl(oldfd, how, newfd));
58254409Sjilles}
59254409Sjilles
60254409Sjilles__weak_reference(__dup3, dup3);
61254409Sjilles__weak_reference(__dup3, _dup3);
62