xref: /aosp_15_r20/external/musl/src/unistd/dup3.c (revision c9945492fdd68bbe62686c5b452b4dc1be3f8453)
1 #define _GNU_SOURCE
2 #include <unistd.h>
3 #include <errno.h>
4 #include <fcntl.h>
5 #include "syscall.h"
6 
__dup3(int old,int new,int flags)7 int __dup3(int old, int new, int flags)
8 {
9 	int r;
10 #ifdef SYS_dup2
11 	if (old==new) return __syscall_ret(-EINVAL);
12 	if (flags) {
13 		while ((r=__syscall(SYS_dup3, old, new, flags))==-EBUSY);
14 		if (r!=-ENOSYS) return __syscall_ret(r);
15 		if (flags & ~O_CLOEXEC) return __syscall_ret(-EINVAL);
16 	}
17 	while ((r=__syscall(SYS_dup2, old, new))==-EBUSY);
18 	if (r >= 0 && (flags & O_CLOEXEC))
19 		__syscall(SYS_fcntl, new, F_SETFD, FD_CLOEXEC);
20 #else
21 	while ((r=__syscall(SYS_dup3, old, new, flags))==-EBUSY);
22 #endif
23 	return __syscall_ret(r);
24 }
25 
26 weak_alias(__dup3, dup3);
27