libc-top-half/musl/src/unistd/dup2.c
raw| 1 | #include <unistd.h> |
| 2 | #include <errno.h> |
| 3 | #include <fcntl.h> |
| 4 | #include "syscall.h" |
| 5 | |
| 6 | int dup2(int old, int new) |
| 7 | { |
| 8 | int r; |
| 9 | #ifdef SYS_dup2 |
| 10 | while ((r=__syscall(SYS_dup2, old, new))==-EBUSY); |
| 11 | #else |
| 12 | if (old==new) { |
| 13 | r = __syscall(SYS_fcntl, old, F_GETFD); |
| 14 | if (r >= 0) return old; |
| 15 | } else { |
| 16 | while ((r=__syscall(SYS_dup3, old, new, 0))==-EBUSY); |
| 17 | } |
| 18 | #endif |
| 19 | return __syscall_ret(r); |
| 20 | } |