dup2.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* Duplicate an open file descriptor to a specified file descriptor.
  2. Copyright (C) 1999, 2004-2007, 2009-2013 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 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 General Public License for more details.
  11. You should have received a copy of the GNU 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 Haiku, dup2 (fd, fd) mistakenly clears FD_CLOEXEC. */
  76. if (desired_fd < 0)
  77. fd = desired_fd;
  78. if (fd == desired_fd)
  79. return fcntl (fd, F_GETFL) == -1 ? -1 : fd;
  80. # endif
  81. result = dup2 (fd, desired_fd);
  82. /* Correct an errno value on FreeBSD 6.1 and Cygwin 1.5.x. */
  83. if (result == -1 && errno == EMFILE)
  84. errno = EBADF;
  85. # if REPLACE_FCHDIR
  86. if (fd != desired_fd && result != -1)
  87. result = _gl_register_dup (fd, result);
  88. # endif
  89. return result;
  90. }
  91. #else /* !HAVE_DUP2 */
  92. /* On older platforms, dup2 did not exist. */
  93. # ifndef F_DUPFD
  94. static int
  95. dupfd (int fd, int desired_fd)
  96. {
  97. int duplicated_fd = dup (fd);
  98. if (duplicated_fd < 0 || duplicated_fd == desired_fd)
  99. return duplicated_fd;
  100. else
  101. {
  102. int r = dupfd (fd, desired_fd);
  103. int e = errno;
  104. close (duplicated_fd);
  105. errno = e;
  106. return r;
  107. }
  108. }
  109. # endif
  110. int
  111. dup2 (int fd, int desired_fd)
  112. {
  113. int result = fcntl (fd, F_GETFL) < 0 ? -1 : fd;
  114. if (result == -1 || fd == desired_fd)
  115. return result;
  116. close (desired_fd);
  117. # ifdef F_DUPFD
  118. result = fcntl (fd, F_DUPFD, desired_fd);
  119. # if REPLACE_FCHDIR
  120. if (0 <= result)
  121. result = _gl_register_dup (fd, result);
  122. # endif
  123. # else
  124. result = dupfd (fd, desired_fd);
  125. # endif
  126. if (result == -1 && (errno == EMFILE || errno == EINVAL))
  127. errno = EBADF;
  128. return result;
  129. }
  130. #endif /* !HAVE_DUP2 */