dirent_compat.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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.
  5. Rights: See end of file.
  6. */
  7. #ifdef WIN32
  8. #include "dirent_compat.h"
  9. #include <errno.h>
  10. #include <io.h> /* _findfirst and _findnext set errno iff they return -1 */
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #ifdef __cplusplus
  14. extern "C"
  15. {
  16. #endif
  17. struct DIR
  18. {
  19. long 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 = (long) _findfirst(dir->name, &dir->info)) != -1)
  37. {
  38. dir->result.d_name = 0;
  39. }
  40. else /* rollback */
  41. {
  42. free(dir->name);
  43. free(dir);
  44. dir = 0;
  45. }
  46. }
  47. else /* rollback */
  48. {
  49. free(dir);
  50. dir = 0;
  51. errno = ENOMEM;
  52. }
  53. }
  54. else
  55. {
  56. errno = EINVAL;
  57. }
  58. return dir;
  59. }
  60. int closedir(DIR *dir)
  61. {
  62. int result = -1;
  63. if(dir)
  64. {
  65. if(dir->handle != -1)
  66. {
  67. result = _findclose(dir->handle);
  68. }
  69. free(dir->name);
  70. free(dir);
  71. }
  72. if(result == -1) /* map all errors to EBADF */
  73. {
  74. errno = EBADF;
  75. }
  76. return result;
  77. }
  78. struct dirent *readdir(DIR *dir)
  79. {
  80. struct dirent *result = 0;
  81. if(dir && dir->handle != -1)
  82. {
  83. if(!dir->result.d_name || _findnext(dir->handle, &dir->info) != -1)
  84. {
  85. result = &dir->result;
  86. result->d_name = dir->info.name;
  87. }
  88. }
  89. else
  90. {
  91. errno = EBADF;
  92. }
  93. return result;
  94. }
  95. void rewinddir(DIR *dir)
  96. {
  97. if(dir && dir->handle != -1)
  98. {
  99. _findclose(dir->handle);
  100. dir->handle = (long) _findfirst(dir->name, &dir->info);
  101. dir->result.d_name = 0;
  102. }
  103. else
  104. {
  105. errno = EBADF;
  106. }
  107. }
  108. #ifdef __cplusplus
  109. }
  110. #endif
  111. #endif
  112. /*
  113. Copyright Kevlin Henney, 1997, 2003. 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. */