move msghdr and cmsghdr out of bits/socket.h
[musl.git] / src / unistd / dup3.c
blobf919f79125a624c4d47c1cb50a103f82e0ba576f
1 #define _GNU_SOURCE
2 #include <unistd.h>
3 #include <errno.h>
4 #include <fcntl.h>
5 #include "syscall.h"
7 int __dup3(int old, int new, int flags)
9 int r;
10 #ifdef SYS_dup2
11 if (old==new) return __syscall_ret(-EINVAL);
12 if (flags & O_CLOEXEC) {
13 while ((r=__syscall(SYS_dup3, old, new, flags))==-EBUSY);
14 if (r!=-ENOSYS) return __syscall_ret(r);
16 while ((r=__syscall(SYS_dup2, old, new))==-EBUSY);
17 if (flags & O_CLOEXEC) __syscall(SYS_fcntl, new, F_SETFD, FD_CLOEXEC);
18 #else
19 while ((r=__syscall(SYS_dup3, old, new, flags))==-EBUSY);
20 #endif
21 return __syscall_ret(r);
24 weak_alias(__dup3, dup3);