dup2.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /* Duplicate an open file descriptor to a specified file descriptor.
  2. Copyright (C) 1999, 2004-2007, 2009-2017 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. # elif defined __KLIBC__
  67. # include <InnoTekLIBC/backend.h>
  68. static int
  69. klibc_dup2dirfd (int fd, int desired_fd)
  70. {
  71. int tempfd;
  72. int dupfd;
  73. tempfd = open ("NUL", O_RDONLY);
  74. if (tempfd == -1)
  75. return -1;
  76. if (tempfd == desired_fd)
  77. {
  78. close (tempfd);
  79. char path[_MAX_PATH];
  80. if (__libc_Back_ioFHToPath (fd, path, sizeof (path)))
  81. return -1;
  82. return open(path, O_RDONLY);
  83. }
  84. dupfd = klibc_dup2dirfd (fd, desired_fd);
  85. close (tempfd);
  86. return dupfd;
  87. }
  88. static int
  89. klibc_dup2 (int fd, int desired_fd)
  90. {
  91. int dupfd;
  92. struct stat sbuf;
  93. dupfd = dup2 (fd, desired_fd);
  94. if (dupfd == -1 && errno == ENOTSUP \
  95. && !fstat (fd, &sbuf) && S_ISDIR (sbuf.st_mode))
  96. {
  97. close (desired_fd);
  98. return klibc_dup2dirfd (fd, desired_fd);
  99. }
  100. return dupfd;
  101. }
  102. # define dup2 klibc_dup2
  103. # endif
  104. int
  105. rpl_dup2 (int fd, int desired_fd)
  106. {
  107. int result;
  108. # ifdef F_GETFL
  109. /* On Linux kernels 2.6.26-2.6.29, dup2 (fd, fd) returns -EBADF.
  110. On Cygwin 1.5.x, dup2 (1, 1) returns 0.
  111. On Cygwin 1.7.17, dup2 (1, -1) dumps core.
  112. On Cygwin 1.7.25, dup2 (1, 256) can dump core.
  113. On Haiku, dup2 (fd, fd) mistakenly clears FD_CLOEXEC. */
  114. # if HAVE_SETDTABLESIZE
  115. setdtablesize (desired_fd + 1);
  116. # endif
  117. if (desired_fd < 0)
  118. fd = desired_fd;
  119. if (fd == desired_fd)
  120. return fcntl (fd, F_GETFL) == -1 ? -1 : fd;
  121. # endif
  122. result = dup2 (fd, desired_fd);
  123. /* Correct an errno value on FreeBSD 6.1 and Cygwin 1.5.x. */
  124. if (result == -1 && errno == EMFILE)
  125. errno = EBADF;
  126. # if REPLACE_FCHDIR
  127. if (fd != desired_fd && result != -1)
  128. result = _gl_register_dup (fd, result);
  129. # endif
  130. return result;
  131. }
  132. #else /* !HAVE_DUP2 */
  133. /* On older platforms, dup2 did not exist. */
  134. # ifndef F_DUPFD
  135. static int
  136. dupfd (int fd, int desired_fd)
  137. {
  138. int duplicated_fd = dup (fd);
  139. if (duplicated_fd < 0 || duplicated_fd == desired_fd)
  140. return duplicated_fd;
  141. else
  142. {
  143. int r = dupfd (fd, desired_fd);
  144. int e = errno;
  145. close (duplicated_fd);
  146. errno = e;
  147. return r;
  148. }
  149. }
  150. # endif
  151. int
  152. dup2 (int fd, int desired_fd)
  153. {
  154. int result = fcntl (fd, F_GETFL) < 0 ? -1 : fd;
  155. if (result == -1 || fd == desired_fd)
  156. return result;
  157. close (desired_fd);
  158. # ifdef F_DUPFD
  159. result = fcntl (fd, F_DUPFD, desired_fd);
  160. # if REPLACE_FCHDIR
  161. if (0 <= result)
  162. result = _gl_register_dup (fd, result);
  163. # endif
  164. # else
  165. result = dupfd (fd, desired_fd);
  166. # endif
  167. if (result == -1 && (errno == EMFILE || errno == EINVAL))
  168. errno = EBADF;
  169. return result;
  170. }
  171. #endif /* !HAVE_DUP2 */