win32-dirent.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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
  5. * License as published by the Free Software Foundation; either
  6. * version 2.1 of the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but 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 02110-1301 USA
  16. */
  17. #ifdef HAVE_CONFIG_H
  18. # include <config.h>
  19. #endif
  20. #include "libguile/__scm.h"
  21. #include <windows.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include "win32-dirent.h"
  25. DIR *
  26. opendir (const char * name)
  27. {
  28. DIR *dir;
  29. HANDLE hnd;
  30. char *file;
  31. WIN32_FIND_DATA find;
  32. if (!name || !*name)
  33. return NULL;
  34. file = malloc (strlen (name) + 3);
  35. strcpy (file, name);
  36. if (file[strlen (name) - 1] != '/' && file[strlen (name) - 1] != '\\')
  37. strcat (file, "/*");
  38. else
  39. strcat (file, "*");
  40. if ((hnd = FindFirstFile (file, &find)) == INVALID_HANDLE_VALUE)
  41. {
  42. free (file);
  43. return NULL;
  44. }
  45. dir = malloc (sizeof (DIR));
  46. dir->mask = file;
  47. dir->fd = (int) hnd;
  48. dir->data = malloc (sizeof (WIN32_FIND_DATA));
  49. dir->allocation = sizeof (WIN32_FIND_DATA);
  50. dir->size = dir->allocation;
  51. dir->filepos = 0;
  52. memcpy (dir->data, &find, sizeof (WIN32_FIND_DATA));
  53. return dir;
  54. }
  55. struct dirent *
  56. readdir (DIR * dir)
  57. {
  58. static struct dirent entry;
  59. WIN32_FIND_DATA *find;
  60. entry.d_ino = 0;
  61. entry.d_type = 0;
  62. find = (WIN32_FIND_DATA *) dir->data;
  63. if (dir->filepos)
  64. {
  65. if (!FindNextFile ((HANDLE) dir->fd, find))
  66. return NULL;
  67. }
  68. entry.d_off = dir->filepos;
  69. strncpy (entry.d_name, find->cFileName, sizeof (entry.d_name));
  70. entry.d_reclen = strlen (find->cFileName);
  71. dir->filepos++;
  72. return &entry;
  73. }
  74. int
  75. closedir (DIR * dir)
  76. {
  77. HANDLE hnd = (HANDLE) dir->fd;
  78. free (dir->data);
  79. free (dir->mask);
  80. free (dir);
  81. return FindClose (hnd) ? 0 : -1;
  82. }
  83. void
  84. rewinddir (DIR * dir)
  85. {
  86. HANDLE hnd = (HANDLE) dir->fd;
  87. WIN32_FIND_DATA *find = (WIN32_FIND_DATA *) dir->data;
  88. FindClose (hnd);
  89. hnd = FindFirstFile (dir->mask, find);
  90. dir->fd = (int) hnd;
  91. dir->filepos = 0;
  92. }
  93. void
  94. seekdir (DIR * dir, off_t offset)
  95. {
  96. off_t n;
  97. rewinddir (dir);
  98. for (n = 0; n < offset; n++)
  99. {
  100. if (FindNextFile ((HANDLE) dir->fd, (WIN32_FIND_DATA *) dir->data))
  101. dir->filepos++;
  102. }
  103. }
  104. off_t
  105. telldir (DIR * dir)
  106. {
  107. return dir->filepos;
  108. }
  109. int
  110. dirfd (DIR * dir)
  111. {
  112. return dir->fd;
  113. }