dirent.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2015 Damien P. George
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include "dirent.h"
  27. #include <errno.h>
  28. #include <Windows.h>
  29. typedef struct DIR {
  30. HANDLE findHandle;
  31. WIN32_FIND_DATA findData;
  32. struct dirent result;
  33. } DIR;
  34. DIR *opendir(const char *name) {
  35. if (!name || !*name) {
  36. errno = ENOENT;
  37. return NULL;
  38. }
  39. DIR *dir = malloc(sizeof(DIR));
  40. if (!dir) {
  41. errno = ENOMEM;
  42. return NULL;
  43. }
  44. dir->result.d_ino = 0;
  45. dir->result.d_name = NULL;
  46. dir->findHandle = INVALID_HANDLE_VALUE;
  47. const size_t nameLen = strlen(name);
  48. char *path = malloc(nameLen + 3); // allocate enough for adding "/*"
  49. if (!path) {
  50. free(dir);
  51. errno = ENOMEM;
  52. return NULL;
  53. }
  54. strcpy(path, name);
  55. // assure path ends with wildcard
  56. const char lastChar = path[nameLen - 1];
  57. if (lastChar != '*') {
  58. const char *appendWC = (lastChar != '/' && lastChar != '\\') ? "/*" : "*";
  59. strcat(path, appendWC);
  60. }
  61. // init
  62. dir->findHandle = FindFirstFile(path, &dir->findData);
  63. free(path);
  64. if (dir->findHandle == INVALID_HANDLE_VALUE) {
  65. free(dir);
  66. errno = ENOENT;
  67. return NULL;
  68. }
  69. return dir;
  70. }
  71. int closedir(DIR *dir) {
  72. if (dir) {
  73. FindClose(dir->findHandle);
  74. free(dir);
  75. return 0;
  76. } else {
  77. errno = EBADF;
  78. return -1;
  79. }
  80. }
  81. struct dirent *readdir(DIR *dir) {
  82. if (!dir) {
  83. errno = EBADF;
  84. return NULL;
  85. }
  86. // first pass d_name is NULL so use result from FindFirstFile in opendir, else use FindNextFile
  87. if (!dir->result.d_name || FindNextFile(dir->findHandle, &dir->findData)) {
  88. dir->result.d_name = dir->findData.cFileName;
  89. return &dir->result;
  90. }
  91. return NULL;
  92. }