1*95f35978Schristos /* $NetBSD: compat_dup3.c,v 1.2 2024/05/20 01:33:40 christos Exp $ */
2381b1356Schristos
3381b1356Schristos /*-
4381b1356Schristos * Copyright (c) 2024 The NetBSD Foundation, Inc.
5381b1356Schristos * All rights reserved.
6381b1356Schristos *
7381b1356Schristos * This code is derived from software contributed to The NetBSD Foundation
8381b1356Schristos * by Christos Zoulas.
9381b1356Schristos *
10381b1356Schristos * Redistribution and use in source and binary forms, with or without
11381b1356Schristos * modification, are permitted provided that the following conditions
12381b1356Schristos * are met:
13381b1356Schristos * 1. Redistributions of source code must retain the above copyright
14381b1356Schristos * notice, this list of conditions and the following disclaimer.
15381b1356Schristos * 2. Redistributions in binary form must reproduce the above copyright
16381b1356Schristos * notice, this list of conditions and the following disclaimer in the
17381b1356Schristos * documentation and/or other materials provided with the distribution.
18381b1356Schristos *
19381b1356Schristos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20381b1356Schristos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21381b1356Schristos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22381b1356Schristos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23381b1356Schristos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24381b1356Schristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25381b1356Schristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26381b1356Schristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27381b1356Schristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28381b1356Schristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29381b1356Schristos * POSSIBILITY OF SUCH DAMAGE.
30381b1356Schristos */
31381b1356Schristos #include <sys/cdefs.h>
32381b1356Schristos #if defined(LIBC_SCCS) && !defined(lint)
33*95f35978Schristos __RCSID("$NetBSD: compat_dup3.c,v 1.2 2024/05/20 01:33:40 christos Exp $");
34381b1356Schristos #endif /* LIBC_SCCS and not lint */
35381b1356Schristos
36381b1356Schristos #include "namespace.h"
37381b1356Schristos #define __LIBC12_SOURCE__
38381b1356Schristos #include <fcntl.h>
39381b1356Schristos #include <compat/include/unistd.h>
40381b1356Schristos
41381b1356Schristos __warn_references(dup3,
42381b1356Schristos "warning: reference to compatibility dup3(); include <unistd.h> to generate correct reference")
43381b1356Schristos
44381b1356Schristos int
dup3(int oldfd,int newfd,int flags)45381b1356Schristos dup3(int oldfd, int newfd, int flags)
46381b1356Schristos {
47381b1356Schristos if (oldfd != newfd) {
48*95f35978Schristos return __dup3100(oldfd, newfd, flags);
49381b1356Schristos }
50381b1356Schristos if (flags & (O_NONBLOCK|O_NOSIGPIPE)) {
51381b1356Schristos int e = fcntl(newfd, F_GETFL, 0);
52381b1356Schristos if (e == -1)
53381b1356Schristos return -1;
54381b1356Schristos e |= flags & (O_NONBLOCK|O_NOSIGPIPE);
55381b1356Schristos e = fcntl(newfd, F_SETFL, e);
56381b1356Schristos if (e == -1)
57381b1356Schristos return -1;
58381b1356Schristos }
59381b1356Schristos if (flags & O_CLOEXEC)
60381b1356Schristos return fcntl(newfd, F_SETFD, FD_CLOEXEC);
61381b1356Schristos return 0;
62381b1356Schristos }
63