1*433d6423SLionel Sambuc #include <sys/cdefs.h> 2*433d6423SLionel Sambuc #include "namespace.h" 3*433d6423SLionel Sambuc #include <lib.h> 4*433d6423SLionel Sambuc 5*433d6423SLionel Sambuc #include <fcntl.h> 6*433d6423SLionel Sambuc #include <unistd.h> 7*433d6423SLionel Sambuc dup2(fd,fd2)8*433d6423SLionel Sambucint dup2(fd, fd2) 9*433d6423SLionel Sambuc int fd, fd2; 10*433d6423SLionel Sambuc { 11*433d6423SLionel Sambuc /* The behavior of dup2 is defined by POSIX in 6.2.1.2 as almost, but not 12*433d6423SLionel Sambuc * quite the same as fcntl. 13*433d6423SLionel Sambuc */ 14*433d6423SLionel Sambuc 15*433d6423SLionel Sambuc if (fd2 < 0 || fd2 > OPEN_MAX) { 16*433d6423SLionel Sambuc errno = EBADF; 17*433d6423SLionel Sambuc return(-1); 18*433d6423SLionel Sambuc } 19*433d6423SLionel Sambuc 20*433d6423SLionel Sambuc /* Check to see if fildes is valid. */ 21*433d6423SLionel Sambuc if (fcntl(fd, F_GETFL) < 0) { 22*433d6423SLionel Sambuc /* 'fd' is not valid. */ 23*433d6423SLionel Sambuc return(-1); 24*433d6423SLionel Sambuc } else { 25*433d6423SLionel Sambuc /* 'fd' is valid. */ 26*433d6423SLionel Sambuc if (fd == fd2) return(fd2); 27*433d6423SLionel Sambuc close(fd2); 28*433d6423SLionel Sambuc return(fcntl(fd, F_DUPFD, fd2)); 29*433d6423SLionel Sambuc } 30*433d6423SLionel Sambuc } 31