some coverity fixes.
[minix.git] / lib / libc / sys-minix / dup2.c
blob28772ff4d9bbe1cf2872d2d045bc3216afcf3b92
1 #include <sys/cdefs.h>
2 #include "namespace.h"
3 #include <lib.h>
5 #include <fcntl.h>
6 #include <unistd.h>
8 #ifdef __weak_alias
9 __weak_alias(dup2, _dup2)
10 #endif
12 int dup2(fd, fd2)
13 int fd, fd2;
15 /* The behavior of dup2 is defined by POSIX in 6.2.1.2 as almost, but not
16 * quite the same as fcntl.
19 if (fd2 < 0 || fd2 > OPEN_MAX) {
20 errno = EBADF;
21 return(-1);
24 /* Check to see if fildes is valid. */
25 if (fcntl(fd, F_GETFL) < 0) {
26 /* 'fd' is not valid. */
27 return(-1);
28 } else {
29 /* 'fd' is valid. */
30 if (fd == fd2) return(fd2);
31 close(fd2);
32 return(fcntl(fd, F_DUPFD, fd2));