fdopen.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (C) 2001-2003 by egnite Software GmbH. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * For additional information see http://www.ethernut.de/
  31. *
  32. */
  33. /*
  34. * $Log$
  35. * Revision 1.3 2009/02/13 14:52:05 haraldkipp
  36. * Include memdebug.h for heap management debugging support.
  37. *
  38. * Revision 1.2 2008/08/11 06:59:40 haraldkipp
  39. * BSD types replaced by stdint types (feature request #1282721).
  40. *
  41. * Revision 1.1.1.1 2003/05/09 14:40:24 haraldkipp
  42. * Initial using 3.2.1
  43. *
  44. * Revision 1.1 2003/02/04 17:49:04 harald
  45. * *** empty log message ***
  46. *
  47. */
  48. #include "nut_io.h"
  49. #include <errno.h>
  50. #include <fcntl.h>
  51. #include <stdlib.h>
  52. #include <memdebug.h>
  53. #include <sys/device.h>
  54. /*!
  55. * \addtogroup xgCrtStdio
  56. */
  57. /*@{*/
  58. /*!
  59. * \brief Open a stream associated with a file, device or socket descriptor.
  60. *
  61. * \param fd Descriptor of a previously opened file, device or
  62. * connected socket.
  63. * \param mode Specifies the access mode.
  64. * \li \c "r" Read only.
  65. * \li \c "w" Write only.
  66. * \li \c "a" Write only at the end of file.
  67. * \li \c "r+" Read and write existing file.
  68. * \li \c "w+" Read and write, destroys existing file contents.
  69. * \li \c "a+" Read and write, preserves existing file contents.
  70. * \li \c "b" May be appended to any of the above strings to
  71. * specify binary access.
  72. *
  73. * \return A pointer to the open stream or a null pointer to indicate
  74. * an error.
  75. */
  76. FILE *_fdopen(int fd, const char *mode)
  77. {
  78. int mflags = _O_TEXT;
  79. uint_fast8_t i;
  80. /*
  81. * Translate file mode.
  82. */
  83. if ((mflags = _fmode(mode)) == EOF)
  84. return 0;
  85. /************************* HACK ALERT!!! *************************/
  86. /* TCP Sockets are hacked into this pseudo
  87. * file scheme. If a stream shall be connected to this socket,
  88. * _fdopen() is called with a casted pointer to the socket struct
  89. * instead of passing a real file descriptor. Therefore we have
  90. * to handle such pointers in a special way...
  91. *
  92. * I really would like to see a new socket API, where we directly
  93. * use file descriptors (integers in the range of 0 ... FOPEN_MAX-1
  94. * instead of struct pointers
  95. */
  96. /************************* HACK ALERT!!! *************************/
  97. if ((unsigned int)fd >= FOPEN_MAX) {
  98. /* We expect "normal" filedescriptors to be positive integers in
  99. * a range of 0 ... FOPEN_MAX-1. Any other value will be sanity
  100. * checked and then manually added to the __fds[] array. The
  101. * resulting integer file descriptor will then be used to
  102. * connect the stream like is is done with normal file descriptors
  103. * as well.
  104. */
  105. NUTVIRTUALDEVICE *vdv = (NUTVIRTUALDEVICE *) fd;
  106. if ((vdv->vdv_zero == NULL) && (vdv->vdv_type == IFTYP_TCPSOCK)) {
  107. /* Seems the user passed a TCPSOCK struct pointer... */
  108. /* Search the next free filedescriptor */
  109. for (i = 0; __fds[i];) {
  110. if (++i >= FOPEN_MAX) {
  111. errno = EMFILE;
  112. return NULL;
  113. }
  114. }
  115. /* Reserve the entry */
  116. __fds[i] = (NUTFILE*) fd;
  117. fd = i;
  118. }
  119. /************************ HACK ALERT END *************************/
  120. }
  121. /*
  122. * Find an empty slot.
  123. */
  124. for (i = 0; __iob[i];) {
  125. if (++i >= FOPEN_MAX) {
  126. errno = ENFILE;
  127. return NULL;
  128. }
  129. }
  130. if ((__iob[i] = malloc(sizeof(FILE))) != 0) {
  131. __iob[i]->iob_fd = fd;
  132. __iob[i]->iob_mode = mflags;
  133. __iob[i]->iob_flags = 0;
  134. __iob[i]->iob_unget = 0;
  135. } else
  136. errno = ENOMEM;
  137. return __iob[i];
  138. }
  139. /*@}*/