*: Updated copyright to 2009 and normalized name & email.
[kbuild-mirror.git] / src / fastdep / os2fake-win.c
blobdc504d54a01d317cadeef02bed8843edef4240a4
1 /* $Id$
3 * OS/2 Fake library for Win32.
5 * Copyright (c) 2001 knut st. osmundsen (knut.stange.osmundsen@mynd.no)
7 * GPL
9 */
12 /*******************************************************************************
13 * Defined Constants And Macros *
14 *******************************************************************************/
15 #define VALIDPTR(pv) (((PVOID)pv) >= (PVOID)0x10000 && ((PVOID)pv) <= (PVOID)0xc0000000)
19 /*******************************************************************************
20 * Header Files *
21 *******************************************************************************/
22 #include <windows.h>
23 #include <string.h>
24 #include <stdio.h>
26 #include "os2fake.h"
29 /*******************************************************************************
30 * Internal Functions *
31 *******************************************************************************/
32 static ULONG ConvertAttributes(DWORD dwFileAttributes);
33 static ULONG ConvertFileTime(PFILETIME pFileTime);
35 /**
36 * Converts Win32 file attribute to OS/2 file attributes.
37 * @returns OS/2 fileattributes.
38 * @param dwFileAttributes Win32 fileattributes.
40 ULONG ConvertAttributes(DWORD dwFileAttributes)
42 static struct _ConvAttr
44 ULONG ulWin;
45 ULONG ulOs2;
46 } aConvAttr[] =
48 {FILE_ATTRIBUTE_READONLY, FILE_READONLY},
49 {FILE_ATTRIBUTE_HIDDEN, FILE_HIDDEN},
50 {FILE_ATTRIBUTE_SYSTEM, FILE_SYSTEM},
51 {FILE_ATTRIBUTE_DIRECTORY, FILE_DIRECTORY},
52 {FILE_ATTRIBUTE_ARCHIVE, FILE_ARCHIVED}
54 ULONG ulOS2Attr = 0;
55 int i;
57 for (i = 0; i < sizeof(aConvAttr) / sizeof(aConvAttr[0]); i++)
58 if (dwFileAttributes & aConvAttr[i].ulWin)
59 ulOS2Attr |= aConvAttr[i].ulOs2;
61 return ulOS2Attr;
65 /**
66 * Converts Win32 filetime to OS/2 filetime.
67 * @returns OS/2 filetime.
68 * @param pFileTime Pointer to Win32 filetime.
70 ULONG ConvertFileTime(PFILETIME pFileTime)
72 ULONG ulOS2FileTime;
73 SYSTEMTIME SystemTime;
75 if ( FileTimeToSystemTime(pFileTime, &SystemTime)
76 && SystemTime.wYear >= 1980 && SystemTime.wYear < (1980 + 0x7F))
78 ulOS2FileTime = SystemTime.wDay
79 | (SystemTime.wMonth << 5)
80 | (((SystemTime.wYear - 1980) & 0x7F) << (5+4))
81 | ((SystemTime.wSecond / 2) << (16))
82 | (SystemTime.wMinute << (16+5))
83 | (SystemTime.wHour << (16+5+6));
85 else
86 ulOS2FileTime = 0;
88 return ulOS2FileTime;
93 APIRET OS2ENTRY DosQueryPathInfo(
94 PCSZ pszPathName,
95 ULONG ulInfoLevel,
96 PVOID pInfoBuf,
97 ULONG cbInfoBuf)
99 APIRET rc; /* Return code. */
101 if (!VALIDPTR(pszPathName))
103 fprintf(stderr, "DosQueryPathInfo: pszPathName is an invalid pointer - %p\n", pszPathName);
104 return ERROR_INVALID_PARAMETER;
107 if (!VALIDPTR(pInfoBuf))
109 fprintf(stderr, "DosQueryPathInfo: pInfoBuf is an invalid pointer - %p\n", pInfoBuf);
110 return ERROR_INVALID_PARAMETER;
114 rc = ERROR_INVALID_PARAMETER;
115 switch (ulInfoLevel)
117 case FIL_QUERYFULLNAME:
119 LPTSTR lpDummy;
120 if (GetFullPathName(pszPathName, cbInfoBuf, pInfoBuf, &lpDummy) > 0)
121 rc = NO_ERROR;
122 else
123 rc = GetLastError();
124 break;
127 case FIL_STANDARD:
128 if (cbInfoBuf == sizeof(FILESTATUS3))
130 WIN32_FILE_ATTRIBUTE_DATA fad;
132 if (GetFileAttributesEx(pszPathName, GetFileExInfoStandard, &fad)) //W98, NT4 and above.
134 PFILESTATUS3 pfst3 = (PFILESTATUS3)(pInfoBuf);
136 if (fad.nFileSizeHigh > 0)
137 rc = ERROR_BAD_LENGTH;
138 pfst3->cbFile = pfst3->cbFileAlloc = fad.nFileSizeLow;
139 pfst3->attrFile = ConvertAttributes(fad.dwFileAttributes);
140 *(PULONG)(&pfst3->fdateCreation) = ConvertFileTime(&fad.ftCreationTime);
141 *(PULONG)(&pfst3->fdateLastAccess) = ConvertFileTime(&fad.ftLastAccessTime);
142 *(PULONG)(&pfst3->fdateLastWrite) = ConvertFileTime(&fad.ftLastWriteTime);
143 rc = NO_ERROR;
145 else
146 rc = GetLastError();
148 else
149 fprintf(stderr, "DosQueryPathInfo: FIL_STANDARD - invalid structure size (cbInfoBuf=%d)\n", cbInfoBuf);
150 break;
152 default:
153 fprintf(stderr, "DosQueryPathInfo: ulInfoLevel=%d not supported\n", ulInfoLevel);
156 return rc;
160 APIRET OS2ENTRY DosFindFirst(
161 PCSZ pszFileSpec,
162 PHDIR phdir,
163 ULONG flAttribute,
164 PVOID pFindBuf,
165 ULONG cbFindBuf,
166 PULONG pcFileNames,
167 ULONG ulInfoLevel)
169 WIN32_FIND_DATA FindData; /* Win32 Find data (returned by FindFirstFile) */
170 APIRET rc;
172 if (!VALIDPTR(pszFileSpec))
174 fprintf(stderr, "DosFindFirst: pszFileSpec - %p\n", pszFileSpec);
175 return ERROR_INVALID_PARAMETER;
178 if (!VALIDPTR(phdir))
180 fprintf(stderr, "DosFindFirst: phdir - %p\n", phdir);
181 return ERROR_INVALID_PARAMETER;
184 if (!VALIDPTR(pFindBuf))
186 fprintf(stderr, "DosFindFirst: pfindbuf - %p\n", pFindBuf);
187 return ERROR_INVALID_PARAMETER;
190 if (!VALIDPTR(pcFileNames))
192 fprintf(stderr, "DosFindFirst: pcFileNames - %p\n", pcFileNames);
193 return ERROR_INVALID_PARAMETER;
196 if (*phdir != HDIR_CREATE)
198 fprintf(stderr, "DosFindFirst: *phdir != HDIR_CREATE - 0x%08x\n", *phdir);
199 return ERROR_INVALID_PARAMETER;
202 switch (ulInfoLevel)
204 case FIL_STANDARD:
205 if (cbFindBuf < sizeof(FILEFINDBUF3))
207 fprintf(stderr, "DosFindFirst: unsupported buffer size - %d\n", cbFindBuf);
208 return ERROR_INVALID_PARAMETER;
210 break;
212 default:
213 fprintf(stderr, "DosFindFirst: invalid infolevel %d\n", ulInfoLevel);
214 return ERROR_INVALID_PARAMETER;
217 *phdir = (HDIR)FindFirstFile(pszFileSpec, &FindData);
218 if (*phdir != (HDIR)INVALID_HANDLE_VALUE)
220 PFILEFINDBUF3 pfindbuf = (PFILEFINDBUF3)pFindBuf;
222 memcpy(pfindbuf->achName, FindData.cFileName, pfindbuf->cchName = strlen(FindData.cFileName) + 1);
223 pfindbuf->cbFile = pfindbuf->cbFileAlloc = FindData.nFileSizeHigh > 0 ? 0xffffffff : FindData.nFileSizeLow;
224 pfindbuf->attrFile = ConvertAttributes(FindData.dwFileAttributes);
225 *(PULONG)(&pfindbuf->fdateCreation) = ConvertFileTime(&FindData.ftCreationTime);
226 *(PULONG)(&pfindbuf->fdateLastAccess) = ConvertFileTime(&FindData.ftLastAccessTime);
227 *(PULONG)(&pfindbuf->fdateLastWrite) = ConvertFileTime(&FindData.ftLastWriteTime);
228 pfindbuf->oNextEntryOffset = 0;
229 *pcFileNames = 1;
230 rc = NO_ERROR;
232 else
233 rc = GetLastError();
235 return rc;
239 APIRET OS2ENTRY DosFindNext(
240 HDIR hDir,
241 PVOID pFindBuf,
242 ULONG cbFindBuf,
243 PULONG pcFileNames)
245 WIN32_FIND_DATA FindData; /* Win32 Find data (returned by FindFirstFile) */
246 APIRET rc;
248 if (!VALIDPTR(pFindBuf))
250 fprintf(stderr, "DosFindNext: pfindbuf - %p\n", pFindBuf);
251 return ERROR_INVALID_PARAMETER;
254 if (cbFindBuf < sizeof(FILEFINDBUF3))
256 fprintf(stderr, "DosFindNext: unsupported buffer size - %d\n", cbFindBuf);
257 return ERROR_INVALID_PARAMETER;
260 if (!VALIDPTR(pcFileNames))
262 fprintf(stderr, "DosFindNext: pcFileNames - %p\n", pcFileNames);
263 return ERROR_INVALID_PARAMETER;
266 if (FindNextFile((HANDLE)hDir, &FindData))
268 PFILEFINDBUF3 pfindbuf = (PFILEFINDBUF3)pFindBuf;
270 memcpy(pfindbuf->achName, FindData.cFileName, pfindbuf->cchName = strlen(FindData.cFileName) + 1);
271 pfindbuf->cbFile = pfindbuf->cbFileAlloc = FindData.nFileSizeHigh > 0 ? 0xffffffff : FindData.nFileSizeLow;
272 pfindbuf->attrFile = ConvertAttributes(FindData.dwFileAttributes);
273 *(PULONG)(&pfindbuf->fdateCreation) = ConvertFileTime(&FindData.ftCreationTime);
274 *(PULONG)(&pfindbuf->fdateLastAccess) = ConvertFileTime(&FindData.ftLastAccessTime);
275 *(PULONG)(&pfindbuf->fdateLastWrite) = ConvertFileTime(&FindData.ftLastWriteTime);
276 pfindbuf->oNextEntryOffset = 0;
277 *pcFileNames = 1;
278 rc = NO_ERROR;
280 else
281 rc = GetLastError();
283 return rc;
288 APIRET OS2ENTRY DosFindClose(
289 HDIR hDir)
291 if (FindClose((HANDLE)hDir))
292 return NO_ERROR;
293 return ERROR_INVALID_HANDLE;