open.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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.7 2009/01/17 15:37:52 haraldkipp
  36. * Added some NUTASSERT macros to check function parameters.
  37. *
  38. * Revision 1.6 2009/01/17 11:26:38 haraldkipp
  39. * Getting rid of two remaining BSD types in favor of stdint.
  40. * Replaced 'u_int' by 'unsinged int' and 'uptr_t' by 'uintptr_t'.
  41. *
  42. * Revision 1.5 2008/08/11 06:59:40 haraldkipp
  43. * BSD types replaced by stdint types (feature request #1282721).
  44. *
  45. * Revision 1.4 2006/03/16 15:25:20 haraldkipp
  46. * Changed human readable strings from u_char to char to stop GCC 4 from
  47. * nagging about signedness.
  48. *
  49. * Revision 1.3 2004/03/16 16:48:27 haraldkipp
  50. * Added Jan Dubiec's H8/300 port.
  51. *
  52. * Revision 1.2 2003/08/05 20:01:55 haraldkipp
  53. * Typing errors corrected
  54. *
  55. * Revision 1.1.1.1 2003/05/09 14:40:30 haraldkipp
  56. * Initial using 3.2.1
  57. *
  58. * Revision 1.1 2003/02/04 17:49:08 harald
  59. * *** empty log message ***
  60. *
  61. */
  62. #include "nut_io.h"
  63. #include <errno.h>
  64. #include <sys/device.h>
  65. #include <sys/nutdebug.h>
  66. /*!
  67. * \addtogroup xgCrtLowio
  68. */
  69. /*@{*/
  70. NUTFILE *__fds[FOPEN_MAX] = { NULL, };
  71. /*!
  72. * \brief Open a file.
  73. *
  74. * \param name The name of a registered device, optionally followed by a
  75. * colon and a filename.
  76. * \param mode Operation mode. May be any of the following:
  77. * - _O_APPEND Always write at the end.
  78. * - _O_BINARY Raw mode.
  79. * - _O_CREAT Create file if it does not exist.
  80. * - _O_EXCL Open only if it does not exist.
  81. * - _O_RDONLY Read only.
  82. * - _O_RDWR Read and write.
  83. * - _O_TEXT End of line translation.
  84. * - _O_TRUNC Truncate file if it exists.
  85. * - _O_WRONLY Write only.
  86. *
  87. * \return File descriptor for the opened file or -1 to indicate an error.
  88. */
  89. int _open(const char *name, int mode)
  90. {
  91. NUTFILE *file;
  92. NUTDEVICE *dev;
  93. char dev_name[9];
  94. uint_fast8_t nidx;
  95. const char *nptr = name;
  96. int fd;
  97. NUTASSERT(name != NULL);
  98. /*
  99. * Extract device name.
  100. */
  101. for (nidx = 0; *nptr && *nptr != ':' && nidx < 8; nidx++) {
  102. dev_name[nidx] = *nptr++;
  103. }
  104. dev_name[nidx] = 0;
  105. /*
  106. * Get device structure of registered device. In later releases we
  107. * try to open a file on a root device.
  108. */
  109. if ((dev = NutDeviceLookup(dev_name)) == 0) {
  110. errno = ENOENT;
  111. return -1;
  112. }
  113. /*
  114. * We should check, if the mode flags are device conformant.
  115. */
  116. /* Search the next free filedescriptor */
  117. for (fd = 0; __fds[fd];) {
  118. if (++fd >= FOPEN_MAX) {
  119. errno = EMFILE;
  120. return -1;
  121. }
  122. }
  123. /* Reserve the entry */
  124. __fds[fd] = NUTFILE_EOF;
  125. /*
  126. * If a device name was specified, open this device. Otherwise open
  127. * a file on the device.
  128. */
  129. if (*nptr++ != ':')
  130. nptr = 0;
  131. file = (*dev->dev_open) (dev, nptr, mode, 0);
  132. if (file == NUTFILE_EOF) {
  133. __fds[fd] = NULL;
  134. return -1;
  135. }
  136. /* assign the file struct to the file descriptor */
  137. __fds[fd] = file;
  138. return fd;
  139. }
  140. /*@}*/