win32-dirent.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* Copyright (C) 2001, 2006, 2008 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public License
  5. * as published by the Free Software Foundation; either version 3 of
  6. * the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301 USA
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include "libguile/__scm.h"
  22. #include <windows.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include "win32-dirent.h"
  26. DIR *
  27. opendir (const char * name)
  28. {
  29. DIR *dir;
  30. HANDLE hnd;
  31. char *file;
  32. WIN32_FIND_DATA find;
  33. if (!name || !*name)
  34. return NULL;
  35. file = malloc (strlen (name) + 3);
  36. strcpy (file, name);
  37. if (file[strlen (name) - 1] != '/' && file[strlen (name) - 1] != '\\')
  38. strcat (file, "/*");
  39. else
  40. strcat (file, "*");
  41. if ((hnd = FindFirstFile (file, &find)) == INVALID_HANDLE_VALUE)
  42. {
  43. free (file);
  44. return NULL;
  45. }
  46. dir = malloc (sizeof (DIR));
  47. dir->mask = file;
  48. dir->fd = (int) hnd;
  49. dir->data = malloc (sizeof (WIN32_FIND_DATA));
  50. dir->allocation = sizeof (WIN32_FIND_DATA);
  51. dir->size = dir->allocation;
  52. dir->filepos = 0;
  53. memcpy (dir->data, &find, sizeof (WIN32_FIND_DATA));
  54. return dir;
  55. }
  56. struct dirent *
  57. readdir (DIR * dir)
  58. {
  59. static struct dirent entry;
  60. WIN32_FIND_DATA *find;
  61. entry.d_ino = 0;
  62. entry.d_type = 0;
  63. find = (WIN32_FIND_DATA *) dir->data;
  64. if (dir->filepos)
  65. {
  66. if (!FindNextFile ((HANDLE) dir->fd, find))
  67. return NULL;
  68. }
  69. entry.d_off = dir->filepos;
  70. strncpy (entry.d_name, find->cFileName, sizeof (entry.d_name));
  71. entry.d_reclen = strlen (find->cFileName);
  72. dir->filepos++;
  73. return &entry;
  74. }
  75. int
  76. closedir (DIR * dir)
  77. {
  78. HANDLE hnd = (HANDLE) dir->fd;
  79. free (dir->data);
  80. free (dir->mask);
  81. free (dir);
  82. return FindClose (hnd) ? 0 : -1;
  83. }
  84. void
  85. rewinddir (DIR * dir)
  86. {
  87. HANDLE hnd = (HANDLE) dir->fd;
  88. WIN32_FIND_DATA *find = (WIN32_FIND_DATA *) dir->data;
  89. FindClose (hnd);
  90. hnd = FindFirstFile (dir->mask, find);
  91. dir->fd = (int) hnd;
  92. dir->filepos = 0;
  93. }
  94. void
  95. seekdir (DIR * dir, off_t offset)
  96. {
  97. off_t n;
  98. rewinddir (dir);
  99. for (n = 0; n < offset; n++)
  100. {
  101. if (FindNextFile ((HANDLE) dir->fd, (WIN32_FIND_DATA *) dir->data))
  102. dir->filepos++;
  103. }
  104. }
  105. off_t
  106. telldir (DIR * dir)
  107. {
  108. return dir->filepos;
  109. }
  110. int
  111. dirfd (DIR * dir)
  112. {
  113. return dir->fd;
  114. }