Partial port to WIN95/NT.
[idzebra-moved-to-github.git] / util / dirent.c
1
2 #ifdef WINDOWS
3 #include <assert.h>
4 #include <io.h>
5 #include <string.h>
6 #include <stdlib.h>
7
8 #include <direntz.h>
9
10 struct DIR {
11     HANDLE handle;
12     WIN32_FIND_DATA find_data;
13     struct dirent entry;
14 };
15
16 DIR *opendir (const char *name)
17 {
18     char fullName[MAX_PATH+1];
19     DIR *dd = malloc (sizeof(*dd));
20
21     if (!dd)
22         return NULL;
23     strcpy (fullName, name);
24     strcat (fullName, "\\*.*");
25     dd->handle = FindFirstFile(fullName, &dd->find_data);
26     return dd;
27 }
28                                                           
29 struct dirent *readdir (DIR *dd)
30 {
31     if (dd->handle == INVALID_HANDLE_VALUE)
32         return NULL;
33     strcpy (dd->entry.d_name, dd->find_data.cFileName);
34     if (!FindNextFile(dd->handle, &dd->find_data))
35     {
36         FindClose (dd->handle);
37         dd->handle = INVALID_HANDLE_VALUE;
38     }
39     return &dd->entry;
40 }
41
42 void closedir(DIR *dd)
43 {
44     if (dd->handle != INVALID_HANDLE_VALUE)
45         FindClose (dd->handle);
46     if (dd)
47         free (dd);
48 }
49
50 #endif