1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
#include <malloc.h>
#include <string.h>
#include <errno.h>
#include "php.h"
#include "readdir.h"
#include "win32/ioutil.h"
/**********************************************************************
* Implement dirent-style opendir/readdir/rewinddir/closedir on Win32
*
* Functions defined are opendir(), readdir(), rewinddir() and
* closedir() with the same prototypes as the normal dirent.h
* implementation.
*
* Does not implement telldir(), seekdir(), or scandir(). The dirent
* struct is compatible with Unix, except that d_ino is always 1 and
* d_off is made up as we go along.
*
* The DIR typedef is not compatible with Unix.
**********************************************************************/
#ifdef __cplusplus
extern "C" {
#endif
DIR *opendir(const char *dir)
{
DIR *dp;
wchar_t *filespecw, *resolvedw;
HANDLE handle;
int index;
char resolved_path_buff[MAXPATHLEN];
size_t resolvedw_len, filespecw_len;
if (!VCWD_REALPATH(dir, resolved_path_buff)) {
return NULL;
}
dp = (DIR *) malloc(sizeof(DIR));
if (dp == NULL) {
return NULL;
}
resolvedw = php_win32_ioutil_conv_any_to_w(resolved_path_buff, PHP_WIN32_CP_IGNORE_LEN, &resolvedw_len);
if (!resolvedw) {
free(dp);
return NULL;
}
filespecw_len = resolvedw_len + 2;
filespecw = (wchar_t *)malloc((filespecw_len + 1)*sizeof(wchar_t));
if (filespecw == NULL) {
free(dp);
free(resolvedw);
return NULL;
}
wcscpy(filespecw, resolvedw);
index = (int)filespecw_len - 1;
if (index >= 0 && filespecw[index] == L'/' || index == 0 && filespecw[index] == L'\\')
filespecw[index] = L'\0';
wcscat(filespecw, L"\\*");
if ((handle = FindFirstFileW(filespecw, &(dp->fileinfo))) == INVALID_HANDLE_VALUE) {
DWORD err = GetLastError();
if (err == ERROR_NO_MORE_FILES || err == ERROR_FILE_NOT_FOUND) {
dp->finished = 1;
} else {
free(dp);
free(filespecw);
free(resolvedw);
return NULL;
}
}
dp->dirw = _wcsdup(resolvedw);
dp->handle = handle;
dp->offset = 0;
dp->finished = 0;
free(filespecw);
free(resolvedw);
return dp;
}
struct dirent *readdir(DIR *dp)
{
char *_tmp;
if (!dp || dp->finished)
return NULL;
if (dp->offset != 0) {
if (FindNextFileW(dp->handle, &(dp->fileinfo)) == 0) {
dp->finished = 1;
return NULL;
}
}
_tmp = php_win32_ioutil_w_to_any(dp->fileinfo.cFileName);
if (!_tmp) {
/* wide to utf8 failed, should never happen. */
return NULL;
}
strlcpy(dp->dent.d_name, _tmp, _MAX_FNAME+1);
dp->dent.d_reclen = (unsigned short)strlen(dp->dent.d_name);
free(_tmp);
dp->offset++;
dp->dent.d_ino = 1;
dp->dent.d_off = dp->offset;
return &(dp->dent);
}
int readdir_r(DIR *dp, struct dirent *entry, struct dirent **result)
{
char *_tmp;
if (!dp || dp->finished) {
*result = NULL;
return 0;
}
if (dp->offset != 0) {
if (FindNextFileW(dp->handle, &(dp->fileinfo)) == 0) {
dp->finished = 1;
*result = NULL;
return 0;
}
}
_tmp = php_win32_ioutil_w_to_any(dp->fileinfo.cFileName);
if (!_tmp) {
/* wide to utf8 failed, should never happen. */
result = NULL;
return 0;
}
strlcpy(dp->dent.d_name, _tmp, _MAX_FNAME+1);
dp->dent.d_reclen = (unsigned short)strlen(dp->dent.d_name);
free(_tmp);
dp->offset++;
dp->dent.d_ino = 1;
dp->dent.d_off = dp->offset;
memcpy(entry, &dp->dent, sizeof(*entry));
*result = &dp->dent;
return 0;
}
int closedir(DIR *dp)
{
if (!dp)
return 0;
/* It is valid to scan an empty directory but we have an invalid
handle in this case (no first file found). */
if (dp->handle != INVALID_HANDLE_VALUE) {
FindClose(dp->handle);
}
if (dp->dirw)
free(dp->dirw);
if (dp)
free(dp);
return 0;
}
int rewinddir(DIR *dp)
{
/* Re-set to the beginning */
wchar_t *filespecw;
HANDLE handle;
int index;
FindClose(dp->handle);
dp->offset = 0;
dp->finished = 0;
filespecw = (wchar_t *)malloc((wcslen((wchar_t *)dp->dirw) + 2 + 1)*sizeof(wchar_t));
if (filespecw == NULL) {
return -1;
}
wcscpy(filespecw, (wchar_t *)dp->dirw);
index = (int)wcslen(filespecw) - 1;
if (index >= 0 && (filespecw[index] == L'/' ||
(filespecw[index] == L'\\' && index == 0)))
filespecw[index] = L'\0';
wcscat(filespecw, L"/*");
if ((handle = FindFirstFileW(filespecw, &(dp->fileinfo))) == INVALID_HANDLE_VALUE) {
dp->finished = 1;
}
free(filespecw);
dp->handle = handle;
return 0;
}
#ifdef __cplusplus
}
#endif
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: sw=4 ts=4 fdm=marker
* vim<600: sw=4 ts=4
*/
|