dup2.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* Duplicate an open file descriptor to a specified file descriptor.
  2. Copyright (C) 1999, 2004-2007, 2009-2014 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /* written by Paul Eggert */
  14. #include <config.h>
  15. /* Specification. */
  16. #include <unistd.h>
  17. #include <errno.h>
  18. #include <fcntl.h>
  19. #if HAVE_DUP2
  20. # undef dup2
  21. # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
  22. /* Get declarations of the native Windows API functions. */
  23. # define WIN32_LEAN_AND_MEAN
  24. # include <windows.h>
  25. # include "msvc-inval.h"
  26. /* Get _get_osfhandle. */
  27. # include "msvc-nothrow.h"
  28. static int
  29. ms_windows_dup2 (int fd, int desired_fd)
  30. {
  31. int result;
  32. /* If fd is closed, mingw hangs on dup2 (fd, fd). If fd is open,
  33. dup2 (fd, fd) returns 0, but all further attempts to use fd in
  34. future dup2 calls will hang. */
  35. if (fd == desired_fd)
  36. {
  37. if ((HANDLE) _get_osfhandle (fd) == INVALID_HANDLE_VALUE)
  38. {
  39. errno = EBADF;
  40. return -1;
  41. }
  42. return fd;
  43. }
  44. /* Wine 1.0.1 return 0 when desired_fd is negative but not -1:
  45. http://bugs.winehq.org/show_bug.cgi?id=21289 */
  46. if (desired_fd < 0)
  47. {
  48. errno = EBADF;
  49. return -1;
  50. }
  51. TRY_MSVC_INVAL
  52. {
  53. result = dup2 (fd, desired_fd);
  54. }
  55. CATCH_MSVC_INVAL
  56. {
  57. errno = EBADF;
  58. result = -1;
  59. }
  60. DONE_MSVC_INVAL;
  61. if (result == 0)
  62. result = desired_fd;
  63. return result;
  64. }
  65. # define dup2 ms_windows_dup2
  66. # endif
  67. int
  68. rpl_dup2 (int fd, int desired_fd)
  69. {
  70. int result;
  71. # ifdef F_GETFL
  72. /* On Linux kernels 2.6.26-2.6.29, dup2 (fd, fd) returns -EBADF.
  73. On Cygwin 1.5.x, dup2 (1, 1) returns 0.
  74. On Cygwin 1.7.17, dup2 (1, -1) dumps core.
  75. On Cygwin 1.7.25, dup2 (1, 256) can dump core.
  76. On Haiku, dup2 (fd, fd) mistakenly clears FD_CLOEXEC. */
  77. # if HAVE_SETDTABLESIZE
  78. setdtablesize (desired_fd + 1);
  79. # endif
  80. if (desired_fd < 0)
  81. fd = desired_fd;
  82. if (fd == desired_fd)
  83. return fcntl (fd, F_GETFL) == -1 ? -1 : fd;
  84. # endif
  85. result = dup2 (fd, desired_fd);
  86. /* Correct an errno value on FreeBSD 6.1 and Cygwin 1.5.x. */
  87. if (result == -1 && errno == EMFILE)
  88. errno = EBADF;
  89. # if REPLACE_FCHDIR
  90. if (fd != desired_fd && result != -1)
  91. result = _gl_register_dup (fd, result);
  92. # endif
  93. return result;
  94. }
  95. #else /* !HAVE_DUP2 */
  96. /* On older platforms, dup2 did not exist. */
  97. # ifndef F_DUPFD
  98. static int
  99. dupfd (int fd, int desired_fd)
  100. {
  101. int duplicated_fd = dup (fd);
  102. if (duplicated_fd < 0 || duplicated_fd == desired_fd)
  103. return duplicated_fd;
  104. else
  105. {
  106. int r = dupfd (fd, desired_fd);
  107. int e = errno;
  108. close (duplicated_fd);
  109. errno = e;
  110. return r;
  111. }
  112. }
  113. # endif
  114. int
  115. dup2 (int fd, int desired_fd)
  116. {
  117. int result = fcntl (fd, F_GETFL) < 0 ? -1 : fd;
  118. if (result == -1 || fd == desired_fd)
  119. return result;
  120. close (desired_fd);
  121. # ifdef F_DUPFD
  122. result = fcntl (fd, F_DUPFD, desired_fd);
  123. # if REPLACE_FCHDIR
  124. if (0 <= result)
  125. result = _gl_register_dup (fd, result);
  126. # endif
  127. # else
  128. result = dupfd (fd, desired_fd);
  129. # endif
  130. if (result == -1 && (errno == EMFILE || errno == EINVAL))
  131. errno = EBADF;
  132. return result;
  133. }
  134. #endif /* !HAVE_DUP2 */