dirent.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright (C) 2004-2005 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. * Parts are
  34. *
  35. * Copyright (c) 1989, 1993
  36. * The Regents of the University of California. All rights reserved.
  37. *
  38. * Redistribution and use in source and binary forms, with or without
  39. * modification, are permitted provided that the following conditions
  40. * are met:
  41. * 1. Redistributions of source code must retain the above copyright
  42. * notice, this list of conditions and the following disclaimer.
  43. * 2. Redistributions in binary form must reproduce the above copyright
  44. * notice, this list of conditions and the following disclaimer in the
  45. * documentation and/or other materials provided with the distribution.
  46. * 3. Neither the name of the University nor the names of its contributors
  47. * may be used to endorse or promote products derived from this software
  48. * without specific prior written permission.
  49. *
  50. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  51. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  52. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  53. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  54. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  55. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  56. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  57. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  58. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  59. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  60. * SUCH DAMAGE.
  61. *
  62. */
  63. /*!
  64. * \file fs/dirent.c
  65. * \brief Directory functions.
  66. *
  67. * \verbatim
  68. *
  69. * $Log$
  70. * Revision 1.8 2009/02/13 14:52:05 haraldkipp
  71. * Include memdebug.h for heap management debugging support.
  72. *
  73. * Revision 1.7 2008/08/11 06:59:42 haraldkipp
  74. * BSD types replaced by stdint types (feature request #1282721).
  75. *
  76. * Revision 1.6 2006/08/01 07:44:22 haraldkipp
  77. * Already disabled NUTFILE structure allocation finally removed from
  78. * opendir().
  79. *
  80. * Revision 1.5 2006/04/07 12:51:04 haraldkipp
  81. * Memory hole fixed.
  82. *
  83. * Revision 1.4 2006/03/16 15:25:24 haraldkipp
  84. * Changed human readable strings from u_char to char to stop GCC 4 from
  85. * nagging about signedness.
  86. *
  87. * Revision 1.3 2006/01/05 16:45:20 haraldkipp
  88. * Dynamic NUTFILE allocation for detached block device.
  89. *
  90. * Revision 1.2 2005/02/07 18:57:47 haraldkipp
  91. * ICCAVR compile errors fixed
  92. *
  93. * Revision 1.1 2005/02/05 20:35:21 haraldkipp
  94. * Peanut added
  95. *
  96. *
  97. * \endverbatim
  98. */
  99. #include <sys/device.h>
  100. #include <stdlib.h>
  101. #include <string.h>
  102. #include <errno.h>
  103. #include <memdebug.h>
  104. #include <fs/fs.h>
  105. #include <dirent.h>
  106. /*!
  107. * \addtogroup xgFSDir
  108. */
  109. /*@{*/
  110. /*!
  111. * \brief Open a directory stream.
  112. *
  113. * Opens a directory and associates a directory stream with it.
  114. *
  115. * \param name Pathname of the directory. Must be the full pathname
  116. * including the device, because Nut/OS doesn't support
  117. * relative paths.
  118. *
  119. * \return A pointer used to identify the directory stream in subsequent
  120. * operations. NULL is returned if the directory cannot be accessed,
  121. * or if it cannot allocate enough memory.
  122. */
  123. DIR *opendir(const char *name)
  124. {
  125. DIR *dir = 0;
  126. NUTDEVICE *dev;
  127. char dev_name[9];
  128. uint8_t nidx;
  129. const char *nptr = name;
  130. /* Extract the device name. */
  131. for (nidx = 0; *nptr && *nptr != ':' && nidx < 8; nidx++) {
  132. dev_name[nidx] = *nptr++;
  133. }
  134. dev_name[nidx] = 0;
  135. /* Get device structure of registered device. */
  136. if ((dev = NutDeviceLookup(dev_name)) == 0) {
  137. errno = ENODEV;
  138. return 0;
  139. }
  140. /* Allocate and initialize the stream. */
  141. if ((dir = malloc(sizeof(DIR))) == 0) {
  142. errno = ENOMEM;
  143. return 0;
  144. }
  145. memset(dir, 0, sizeof(DIR));
  146. /* Allocate and initialize the data buffer. */
  147. if ((dir->dd_len = strlen(name + 1)) < sizeof(struct dirent)) {
  148. dir->dd_len = sizeof(struct dirent);
  149. }
  150. if ((dir->dd_buf = malloc(dir->dd_len)) == 0) {
  151. free(dir);
  152. errno = ENOMEM;
  153. return 0;
  154. }
  155. if (*nptr == ':') {
  156. nptr++;
  157. }
  158. strcpy(dir->dd_buf, nptr);
  159. /* Call the proper IOCTL. */
  160. if ((*dev->dev_ioctl) (dev, FS_DIR_OPEN, dir)) {
  161. free(dir->dd_buf);
  162. free(dir);
  163. dir = 0;
  164. }
  165. return dir;
  166. }
  167. /*!
  168. * \brief Close a directory stream.
  169. *
  170. * Closes the given directory stream and frees any allocated memory.
  171. *
  172. * \param dir Pointer to the directory stream.
  173. *
  174. * \return 0 on success. On failure, -1 is returned and the global
  175. * variable errno is set to indicate the error.
  176. */
  177. int closedir(DIR * dir)
  178. {
  179. NUTDEVICE *dev;
  180. if (dir) {
  181. dev = dir->dd_fd->nf_dev;
  182. (*dev->dev_ioctl) (dev, FS_DIR_CLOSE, dir);
  183. if (dir->dd_buf) {
  184. free(dir->dd_buf);
  185. }
  186. free(dir);
  187. }
  188. return 0;
  189. }
  190. /*!
  191. * \brief Get the next directory entry.
  192. *
  193. * \param dir Pointer to the directory stream.
  194. *
  195. * \return A pointer to the next directory entry. It returns NULL upon
  196. * reaching the end of the directory.
  197. */
  198. struct dirent *readdir(DIR * dir)
  199. {
  200. NUTDEVICE *dev = dir->dd_fd->nf_dev;
  201. if ((*dev->dev_ioctl) (dev, FS_DIR_READ, dir)) {
  202. return 0;
  203. }
  204. return (struct dirent *) (dir->dd_buf);
  205. }
  206. /*@}*/