You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
3.1KB

  1. /*
  2. Implementation of POSIX directory browsing functions and types for Win32.
  3. Author: Kevlin Henney (kevlin@acm.org, kevlin@curbralan.com)
  4. History: Created March 1997. Updated June 2003 and July 2012.
  5. Rights: See end of file.
  6. */
  7. #include "dirent.h"
  8. #include <errno.h>
  9. #include <io.h> /* _findfirst and _findnext set errno iff they return -1 */
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #ifdef __cplusplus
  13. extern "C"
  14. {
  15. #endif
  16. typedef ptrdiff_t handle_type; /* C99's intptr_t not sufficiently portable */
  17. struct DIR
  18. {
  19. handle_type handle; /* -1 for failed rewind */
  20. struct _finddata_t info;
  21. struct dirent result; /* d_name null iff first time */
  22. char *name; /* null-terminated char string */
  23. };
  24. DIR *opendir(const char *name)
  25. {
  26. DIR *dir = 0;
  27. if(name && name[0])
  28. {
  29. size_t base_length = strlen(name);
  30. const char *all = /* search pattern must end with suitable wildcard */
  31. strchr("/\\", name[base_length - 1]) ? "*" : "/*";
  32. if((dir = (DIR *) malloc(sizeof *dir)) != 0 &&
  33. (dir->name = (char *) malloc(base_length + strlen(all) + 1)) != 0)
  34. {
  35. strcat(strcpy(dir->name, name), all);
  36. if((dir->handle =
  37. (handle_type) _findfirst(dir->name, &dir->info)) != -1)
  38. {
  39. dir->result.d_name = 0;
  40. }
  41. else /* rollback */
  42. {
  43. free(dir->name);
  44. free(dir);
  45. dir = 0;
  46. }
  47. }
  48. else /* rollback */
  49. {
  50. free(dir);
  51. dir = 0;
  52. errno = ENOMEM;
  53. }
  54. }
  55. else
  56. {
  57. errno = EINVAL;
  58. }
  59. return dir;
  60. }
  61. int closedir(DIR *dir)
  62. {
  63. int result = -1;
  64. if(dir)
  65. {
  66. if(dir->handle != -1)
  67. {
  68. result = _findclose(dir->handle);
  69. }
  70. free(dir->name);
  71. free(dir);
  72. }
  73. if(result == -1) /* map all errors to EBADF */
  74. {
  75. errno = EBADF;
  76. }
  77. return result;
  78. }
  79. struct dirent *readdir(DIR *dir)
  80. {
  81. struct dirent *result = 0;
  82. if(dir && dir->handle != -1)
  83. {
  84. if(!dir->result.d_name || _findnext(dir->handle, &dir->info) != -1)
  85. {
  86. result = &dir->result;
  87. result->d_name = dir->info.name;
  88. }
  89. }
  90. else
  91. {
  92. errno = EBADF;
  93. }
  94. return result;
  95. }
  96. void rewinddir(DIR *dir)
  97. {
  98. if(dir && dir->handle != -1)
  99. {
  100. _findclose(dir->handle);
  101. dir->handle = (handle_type) _findfirst(dir->name, &dir->info);
  102. dir->result.d_name = 0;
  103. }
  104. else
  105. {
  106. errno = EBADF;
  107. }
  108. }
  109. #ifdef __cplusplus
  110. }
  111. #endif
  112. /*
  113. Copyright Kevlin Henney, 1997, 2003, 2012. All rights reserved.
  114. Permission to use, copy, modify, and distribute this software and its
  115. documentation for any purpose is hereby granted without fee, provided
  116. that this copyright and permissions notice appear in all copies and
  117. derivatives.
  118. This software is supplied "as is" without express or implied warranty.
  119. But that said, if there are any problems please get in touch.
  120. */