1 | #include <string.h>
|
---|
2 | #include <windows.h>
|
---|
3 |
|
---|
4 | #include "dirent.h"
|
---|
5 |
|
---|
6 | void _getFileName(DIRSTR *dirlist, WIN32_FIND_DATA *winFileData)
|
---|
7 | { // get leaf if directory name is given
|
---|
8 | char *leafname = strrchr(winFileData->cFileName, '\\');
|
---|
9 |
|
---|
10 | if (leafname)
|
---|
11 | { leafname ++;
|
---|
12 | }
|
---|
13 | else
|
---|
14 | { leafname = winFileData->cFileName;
|
---|
15 | }
|
---|
16 | strcpy(dirlist->fileData.d_name, leafname);
|
---|
17 | }
|
---|
18 |
|
---|
19 | DIRSTR *opendir(const char *name)
|
---|
20 | { char buffer[MAX_PATH];
|
---|
21 | WIN32_FIND_DATA winFileData;
|
---|
22 |
|
---|
23 | DIRSTR *newlist = (DIRSTR *) malloc(sizeof(DIRSTR));
|
---|
24 |
|
---|
25 | strcpy(buffer, name);
|
---|
26 | if (buffer[strlen(buffer)-1] != '\\')
|
---|
27 | { strcat(buffer, "\\*");
|
---|
28 | }
|
---|
29 | else
|
---|
30 | { strcat(buffer, "*");
|
---|
31 | }
|
---|
32 |
|
---|
33 | newlist->searchHandle = FindFirstFile(buffer, &winFileData);
|
---|
34 | if (newlist->searchHandle == INVALID_HANDLE_VALUE)
|
---|
35 | { DWORD error = GetLastError();
|
---|
36 | if (error == ERROR_NO_MORE_FILES)
|
---|
37 | { newlist->first = 2;
|
---|
38 | }
|
---|
39 | else
|
---|
40 | { free(newlist);
|
---|
41 | newlist = NULL;
|
---|
42 | }
|
---|
43 | }
|
---|
44 | else
|
---|
45 | { _getFileName(newlist, &winFileData);
|
---|
46 | newlist->first = 1;
|
---|
47 | }
|
---|
48 | return newlist;
|
---|
49 | }
|
---|
50 |
|
---|
51 | dirent *readdir(DIRSTR *dirlist)
|
---|
52 | { if (dirlist->first == 2)
|
---|
53 | { // empty directory
|
---|
54 | return NULL;
|
---|
55 | }
|
---|
56 | else if (dirlist->first != 1)
|
---|
57 | { WIN32_FIND_DATA winFileData;
|
---|
58 |
|
---|
59 | if (FindNextFile(dirlist->searchHandle, &winFileData) == 0)
|
---|
60 | { DWORD error = GetLastError();
|
---|
61 |
|
---|
62 | return NULL;
|
---|
63 | }
|
---|
64 | else
|
---|
65 | { _getFileName(dirlist, &winFileData);
|
---|
66 | }
|
---|
67 | }
|
---|
68 | else
|
---|
69 | { dirlist->first = 0;
|
---|
70 | }
|
---|
71 |
|
---|
72 | return &dirlist->fileData;
|
---|
73 | }
|
---|
74 |
|
---|
75 | void closedir(DIRSTR *dirlist)
|
---|
76 | { free(dirlist);
|
---|
77 | }
|
---|
78 |
|
---|
79 |
|
---|
80 |
|
---|