am 2aef607b: Merge "Fix dup2 in the case where the two fds are equal."

* commit '2aef607b25c463baed5ae70d14212e24ea7bcf2b':
  Fix dup2 in the case where the two fds are equal.
This commit is contained in:
Elliott Hughes
2015-02-24 05:59:52 +00:00
committed by Android Git Automerger
2 changed files with 30 additions and 0 deletions

View File

@@ -26,8 +26,19 @@
* SUCH DAMAGE.
*/
#include <fcntl.h>
#include <unistd.h>
int dup2(int old_fd, int new_fd) {
// If old_fd is equal to new_fd and a valid file descriptor, dup2 returns
// old_fd without closing it. This is not true of dup3, so we have to
// handle this case ourselves.
if (old_fd == new_fd) {
if (fcntl(old_fd, F_GETFD) == -1) {
return -1;
}
return old_fd;
}
return dup3(old_fd, new_fd, 0);
}