dirent_on_windows.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. Implementation of POSIX directory browsing functions and types for Win32.
  3. Author: Kevlin Henney ([email protected], [email protected])
  4. History: Created March 1997. Updated June 2003 and July 2012.
  5. Rights: See end of file.
  6. */
  7. #include "dirent_on_windows.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. #include "vk_loader_platform.h"
  13. #include "loader.h"
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. typedef ptrdiff_t handle_type; /* C99's intptr_t not sufficiently portable */
  18. struct DIR {
  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. DIR *dir = 0;
  26. if (name && name[0]) {
  27. size_t base_length = strlen(name);
  28. const char *all = /* search pattern must end with suitable wildcard */
  29. strchr("/\\", name[base_length - 1]) ? "*" : "/*";
  30. if ((dir = (DIR *)loader_instance_tls_heap_alloc(sizeof *dir)) != 0 &&
  31. (dir->name = (char *)loader_instance_tls_heap_alloc(base_length + strlen(all) + 1)) != 0) {
  32. strcat(strcpy(dir->name, name), all);
  33. if ((dir->handle = (handle_type)_findfirst(dir->name, &dir->info)) != -1) {
  34. dir->result.d_name = 0;
  35. } else /* rollback */
  36. {
  37. loader_instance_tls_heap_free(dir->name);
  38. loader_instance_tls_heap_free(dir);
  39. dir = 0;
  40. }
  41. } else /* rollback */
  42. {
  43. loader_instance_tls_heap_free(dir);
  44. dir = 0;
  45. errno = ENOMEM;
  46. }
  47. } else {
  48. errno = EINVAL;
  49. }
  50. return dir;
  51. }
  52. int closedir(DIR *dir) {
  53. int result = -1;
  54. if (dir) {
  55. if (dir->handle != -1) {
  56. result = _findclose(dir->handle);
  57. }
  58. loader_instance_tls_heap_free(dir->name);
  59. loader_instance_tls_heap_free(dir);
  60. }
  61. if (result == -1) /* map all errors to EBADF */
  62. {
  63. errno = EBADF;
  64. }
  65. return result;
  66. }
  67. struct dirent *readdir(DIR *dir) {
  68. struct dirent *result = 0;
  69. if (dir && dir->handle != -1) {
  70. if (!dir->result.d_name || _findnext(dir->handle, &dir->info) != -1) {
  71. result = &dir->result;
  72. result->d_name = dir->info.name;
  73. }
  74. } else {
  75. errno = EBADF;
  76. }
  77. return result;
  78. }
  79. void rewinddir(DIR *dir) {
  80. if (dir && dir->handle != -1) {
  81. _findclose(dir->handle);
  82. dir->handle = (handle_type)_findfirst(dir->name, &dir->info);
  83. dir->result.d_name = 0;
  84. } else {
  85. errno = EBADF;
  86. }
  87. }
  88. #ifdef __cplusplus
  89. }
  90. #endif
  91. /*
  92. Copyright Kevlin Henney, 1997, 2003, 2012. All rights reserved.
  93. Copyright (c) 2015 The Khronos Group Inc.
  94. Copyright (c) 2015 Valve Corporation
  95. Copyright (c) 2015 LunarG, Inc.
  96. Permission to use, copy, modify, and distribute this software and its
  97. documentation for any purpose is hereby granted without fee, provided
  98. that this copyright and permissions notice appear in all copies and
  99. derivatives.
  100. This software is supplied "as is" without express or implied warranty.
  101. But that said, if there are any problems please get in touch.
  102. */