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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
/* $Id: os2fake-win.c 2243 2009-01-10 02:24:02Z bird $
*
* OS/2 Fake library for Win32.
*
* Copyright (c) 2001 knut st. osmundsen (knut.stange.osmundsen@mynd.no)
*
* GPL
*
*/
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
#define VALIDPTR(pv) (((PVOID)pv) >= (PVOID)0x10000 && ((PVOID)pv) <= (PVOID)0xc0000000)
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <windows.h>
#include <string.h>
#include <stdio.h>
#include "os2fake.h"
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
static ULONG ConvertAttributes(DWORD dwFileAttributes);
static ULONG ConvertFileTime(PFILETIME pFileTime);
/**
* Converts Win32 file attribute to OS/2 file attributes.
* @returns OS/2 fileattributes.
* @param dwFileAttributes Win32 fileattributes.
*/
ULONG ConvertAttributes(DWORD dwFileAttributes)
{
static struct _ConvAttr
{
ULONG ulWin;
ULONG ulOs2;
} aConvAttr[] =
{
{FILE_ATTRIBUTE_READONLY, FILE_READONLY},
{FILE_ATTRIBUTE_HIDDEN, FILE_HIDDEN},
{FILE_ATTRIBUTE_SYSTEM, FILE_SYSTEM},
{FILE_ATTRIBUTE_DIRECTORY, FILE_DIRECTORY},
{FILE_ATTRIBUTE_ARCHIVE, FILE_ARCHIVED}
};
ULONG ulOS2Attr = 0;
int i;
for (i = 0; i < sizeof(aConvAttr) / sizeof(aConvAttr[0]); i++)
if (dwFileAttributes & aConvAttr[i].ulWin)
ulOS2Attr |= aConvAttr[i].ulOs2;
return ulOS2Attr;
}
/**
* Converts Win32 filetime to OS/2 filetime.
* @returns OS/2 filetime.
* @param pFileTime Pointer to Win32 filetime.
*/
ULONG ConvertFileTime(PFILETIME pFileTime)
{
ULONG ulOS2FileTime;
SYSTEMTIME SystemTime;
if ( FileTimeToSystemTime(pFileTime, &SystemTime)
&& SystemTime.wYear >= 1980 && SystemTime.wYear < (1980 + 0x7F))
{
ulOS2FileTime = SystemTime.wDay
| (SystemTime.wMonth << 5)
| (((SystemTime.wYear - 1980) & 0x7F) << (5+4))
| ((SystemTime.wSecond / 2) << (16))
| (SystemTime.wMinute << (16+5))
| (SystemTime.wHour << (16+5+6));
}
else
ulOS2FileTime = 0;
return ulOS2FileTime;
}
APIRET OS2ENTRY DosQueryPathInfo(
PCSZ pszPathName,
ULONG ulInfoLevel,
PVOID pInfoBuf,
ULONG cbInfoBuf)
{
APIRET rc; /* Return code. */
if (!VALIDPTR(pszPathName))
{
fprintf(stderr, "DosQueryPathInfo: pszPathName is an invalid pointer - %p\n", pszPathName);
return ERROR_INVALID_PARAMETER;
}
if (!VALIDPTR(pInfoBuf))
{
fprintf(stderr, "DosQueryPathInfo: pInfoBuf is an invalid pointer - %p\n", pInfoBuf);
return ERROR_INVALID_PARAMETER;
}
rc = ERROR_INVALID_PARAMETER;
switch (ulInfoLevel)
{
case FIL_QUERYFULLNAME:
{
LPTSTR lpDummy;
if (GetFullPathName(pszPathName, cbInfoBuf, pInfoBuf, &lpDummy) > 0)
rc = NO_ERROR;
else
rc = GetLastError();
break;
}
case FIL_STANDARD:
if (cbInfoBuf == sizeof(FILESTATUS3))
{
WIN32_FILE_ATTRIBUTE_DATA fad;
if (GetFileAttributesEx(pszPathName, GetFileExInfoStandard, &fad)) //W98, NT4 and above.
{
PFILESTATUS3 pfst3 = (PFILESTATUS3)(pInfoBuf);
if (fad.nFileSizeHigh > 0)
rc = ERROR_BAD_LENGTH;
pfst3->cbFile = pfst3->cbFileAlloc = fad.nFileSizeLow;
pfst3->attrFile = ConvertAttributes(fad.dwFileAttributes);
*(PULONG)(&pfst3->fdateCreation) = ConvertFileTime(&fad.ftCreationTime);
*(PULONG)(&pfst3->fdateLastAccess) = ConvertFileTime(&fad.ftLastAccessTime);
*(PULONG)(&pfst3->fdateLastWrite) = ConvertFileTime(&fad.ftLastWriteTime);
rc = NO_ERROR;
}
else
rc = GetLastError();
}
else
fprintf(stderr, "DosQueryPathInfo: FIL_STANDARD - invalid structure size (cbInfoBuf=%d)\n", cbInfoBuf);
break;
default:
fprintf(stderr, "DosQueryPathInfo: ulInfoLevel=%d not supported\n", ulInfoLevel);
}
return rc;
}
APIRET OS2ENTRY DosFindFirst(
PCSZ pszFileSpec,
PHDIR phdir,
ULONG flAttribute,
PVOID pFindBuf,
ULONG cbFindBuf,
PULONG pcFileNames,
ULONG ulInfoLevel)
{
WIN32_FIND_DATA FindData; /* Win32 Find data (returned by FindFirstFile) */
APIRET rc;
if (!VALIDPTR(pszFileSpec))
{
fprintf(stderr, "DosFindFirst: pszFileSpec - %p\n", pszFileSpec);
return ERROR_INVALID_PARAMETER;
}
if (!VALIDPTR(phdir))
{
fprintf(stderr, "DosFindFirst: phdir - %p\n", phdir);
return ERROR_INVALID_PARAMETER;
}
if (!VALIDPTR(pFindBuf))
{
fprintf(stderr, "DosFindFirst: pfindbuf - %p\n", pFindBuf);
return ERROR_INVALID_PARAMETER;
}
if (!VALIDPTR(pcFileNames))
{
fprintf(stderr, "DosFindFirst: pcFileNames - %p\n", pcFileNames);
return ERROR_INVALID_PARAMETER;
}
if (*phdir != HDIR_CREATE)
{
fprintf(stderr, "DosFindFirst: *phdir != HDIR_CREATE - 0x%08x\n", *phdir);
return ERROR_INVALID_PARAMETER;
}
switch (ulInfoLevel)
{
case FIL_STANDARD:
if (cbFindBuf < sizeof(FILEFINDBUF3))
{
fprintf(stderr, "DosFindFirst: unsupported buffer size - %d\n", cbFindBuf);
return ERROR_INVALID_PARAMETER;
}
break;
default:
fprintf(stderr, "DosFindFirst: invalid infolevel %d\n", ulInfoLevel);
return ERROR_INVALID_PARAMETER;
}
*phdir = (HDIR)FindFirstFile(pszFileSpec, &FindData);
if (*phdir != (HDIR)INVALID_HANDLE_VALUE)
{
PFILEFINDBUF3 pfindbuf = (PFILEFINDBUF3)pFindBuf;
memcpy(pfindbuf->achName, FindData.cFileName, pfindbuf->cchName = strlen(FindData.cFileName) + 1);
pfindbuf->cbFile = pfindbuf->cbFileAlloc = FindData.nFileSizeHigh > 0 ? 0xffffffff : FindData.nFileSizeLow;
pfindbuf->attrFile = ConvertAttributes(FindData.dwFileAttributes);
*(PULONG)(&pfindbuf->fdateCreation) = ConvertFileTime(&FindData.ftCreationTime);
*(PULONG)(&pfindbuf->fdateLastAccess) = ConvertFileTime(&FindData.ftLastAccessTime);
*(PULONG)(&pfindbuf->fdateLastWrite) = ConvertFileTime(&FindData.ftLastWriteTime);
pfindbuf->oNextEntryOffset = 0;
*pcFileNames = 1;
rc = NO_ERROR;
}
else
rc = GetLastError();
return rc;
}
APIRET OS2ENTRY DosFindNext(
HDIR hDir,
PVOID pFindBuf,
ULONG cbFindBuf,
PULONG pcFileNames)
{
WIN32_FIND_DATA FindData; /* Win32 Find data (returned by FindFirstFile) */
APIRET rc;
if (!VALIDPTR(pFindBuf))
{
fprintf(stderr, "DosFindNext: pfindbuf - %p\n", pFindBuf);
return ERROR_INVALID_PARAMETER;
}
if (cbFindBuf < sizeof(FILEFINDBUF3))
{
fprintf(stderr, "DosFindNext: unsupported buffer size - %d\n", cbFindBuf);
return ERROR_INVALID_PARAMETER;
}
if (!VALIDPTR(pcFileNames))
{
fprintf(stderr, "DosFindNext: pcFileNames - %p\n", pcFileNames);
return ERROR_INVALID_PARAMETER;
}
if (FindNextFile((HANDLE)hDir, &FindData))
{
PFILEFINDBUF3 pfindbuf = (PFILEFINDBUF3)pFindBuf;
memcpy(pfindbuf->achName, FindData.cFileName, pfindbuf->cchName = strlen(FindData.cFileName) + 1);
pfindbuf->cbFile = pfindbuf->cbFileAlloc = FindData.nFileSizeHigh > 0 ? 0xffffffff : FindData.nFileSizeLow;
pfindbuf->attrFile = ConvertAttributes(FindData.dwFileAttributes);
*(PULONG)(&pfindbuf->fdateCreation) = ConvertFileTime(&FindData.ftCreationTime);
*(PULONG)(&pfindbuf->fdateLastAccess) = ConvertFileTime(&FindData.ftLastAccessTime);
*(PULONG)(&pfindbuf->fdateLastWrite) = ConvertFileTime(&FindData.ftLastWriteTime);
pfindbuf->oNextEntryOffset = 0;
*pcFileNames = 1;
rc = NO_ERROR;
}
else
rc = GetLastError();
return rc;
}
APIRET OS2ENTRY DosFindClose(
HDIR hDir)
{
if (FindClose((HANDLE)hDir))
return NO_ERROR;
return ERROR_INVALID_HANDLE;
}
|