fs_dupfd2.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /****************************************************************************
  2. * fs/vfs/fs_dupfd2.c
  3. *
  4. * Copyright (C) 2007-2009, 2011-2014, 2017 Gregory Nutt. All rights
  5. * reserved.
  6. * Author: Gregory Nutt <gnutt@nuttx.org>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. * 3. Neither the name NuttX nor the names of its contributors may be
  19. * used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  29. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  30. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  32. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. ****************************************************************************/
  36. /****************************************************************************
  37. * Included Files
  38. ****************************************************************************/
  39. #include <nuttx/config.h>
  40. #include <unistd.h>
  41. #include <sched.h>
  42. #include <errno.h>
  43. #include "inode/inode.h"
  44. /****************************************************************************
  45. * Pre-processor Definitions
  46. ****************************************************************************/
  47. #define DUP_ISOPEN(filep) (filep->f_inode != NULL)
  48. /****************************************************************************
  49. * Private Functions
  50. ****************************************************************************/
  51. /****************************************************************************
  52. * Public Functions
  53. ****************************************************************************/
  54. /****************************************************************************
  55. * Name: fs_dupfd2 OR dup2
  56. *
  57. * Description:
  58. * Clone a file descriptor to a specific descriptor number. If socket
  59. * descriptors are implemented, then this is called by dup2() for the
  60. * case of file descriptors. If socket descriptors are not implemented,
  61. * then this function IS dup2().
  62. *
  63. * Returned Value:
  64. * fs_dupfd is sometimes an OS internal function and sometimes is a direct
  65. * substitute for dup2(). So it must return an errno value as though it
  66. * were dup2().
  67. *
  68. ****************************************************************************/
  69. #ifdef CONFIG_NET
  70. int fs_dupfd2(int fd1, int fd2)
  71. #else
  72. int dup2(int fd1, int fd2)
  73. #endif
  74. {
  75. FAR struct file *filep1;
  76. FAR struct file *filep2 = NULL;
  77. int ret;
  78. /* Get the file structures corresponding to the file descriptors. */
  79. ret = fs_getfilep(fd1, &filep1);
  80. if (ret >= 0)
  81. {
  82. ret = fs_getfilep(fd2, &filep2);
  83. }
  84. if (ret < 0)
  85. {
  86. goto errout;
  87. }
  88. DEBUGASSERT(filep1 != NULL && filep2 != NULL);
  89. /* Verify that fd1 is a valid, open file descriptor */
  90. if (!DUP_ISOPEN(filep1))
  91. {
  92. ret = -EBADF;
  93. goto errout;
  94. }
  95. /* Handle a special case */
  96. if (fd1 == fd2)
  97. {
  98. return fd1;
  99. }
  100. /* Perform the dup2 operation */
  101. ret = file_dup2(filep1, filep2);
  102. if (ret < 0)
  103. {
  104. goto errout;
  105. }
  106. return OK;
  107. errout:
  108. set_errno(-ret);
  109. return ERROR;
  110. }