1 | /* $Id: oslibdos.cpp,v 1.7 1999-11-10 14:16:01 sandervl Exp $ */
|
---|
2 |
|
---|
3 | /*
|
---|
4 | * Wrappers for OS/2 Dos* API
|
---|
5 | *
|
---|
6 | * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl)
|
---|
7 | *
|
---|
8 | *
|
---|
9 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
10 | *
|
---|
11 | */
|
---|
12 | #define INCL_BASE
|
---|
13 | #define INCL_DOSEXCEPTIONS
|
---|
14 | #define INCL_DOSMEMMGR
|
---|
15 | #define INCL_DOSPROCESS
|
---|
16 | #include <os2wrap.h> //Odin32 OS/2 api wrappers
|
---|
17 | #include <stdlib.h>
|
---|
18 | #include <stdio.h>
|
---|
19 | #include <string.h>
|
---|
20 | #include <win32type.h>
|
---|
21 | #include <misc.h>
|
---|
22 | #include "initterm.h"
|
---|
23 | #include "oslibdos.h"
|
---|
24 |
|
---|
25 | APIRET APIENTRY DosAliasMem(PVOID pb, ULONG cb, PPVOID ppbAlias, ULONG fl);
|
---|
26 |
|
---|
27 | //******************************************************************************
|
---|
28 | //******************************************************************************
|
---|
29 | DWORD OSLibDosAliasMem(LPVOID pb, ULONG cb, LPVOID *ppbAlias, ULONG fl)
|
---|
30 | {
|
---|
31 | return DosAliasMem(pb, cb, ppbAlias, fl);
|
---|
32 | }
|
---|
33 | //******************************************************************************
|
---|
34 | //NT returns addresses aligned at 64k, so we do too.
|
---|
35 | //******************************************************************************
|
---|
36 | DWORD OSLibDosAllocMem(LPVOID *lplpMemAddr, DWORD size, DWORD flags)
|
---|
37 | {
|
---|
38 | LPVOID memaddr;
|
---|
39 | DWORD offset;
|
---|
40 | APIRET rc;
|
---|
41 |
|
---|
42 | rc = DosAllocMem(&memaddr, size, PAG_READ | flAllocMem);
|
---|
43 | if(rc) {
|
---|
44 | return rc;
|
---|
45 | }
|
---|
46 | DosEnterCritSec();
|
---|
47 | DosFreeMem(memaddr);
|
---|
48 | offset = (DWORD)memaddr & 0xFFFF;
|
---|
49 | if(offset) {
|
---|
50 | DosAllocMem(&memaddr, 64*1024 - offset, PAG_READ | flAllocMem);
|
---|
51 | }
|
---|
52 | rc = DosAllocMem(lplpMemAddr, size, flags | flAllocMem);
|
---|
53 | DosExitCritSec();
|
---|
54 | if((DWORD)*lplpMemAddr & 0xFFFF) {//still not at 64k boundary?
|
---|
55 | DosFreeMem(*lplpMemAddr);
|
---|
56 | rc = OSLibDosAllocMem(lplpMemAddr, size, flags);
|
---|
57 | }
|
---|
58 | if(offset) {
|
---|
59 | DosFreeMem(memaddr);
|
---|
60 | }
|
---|
61 |
|
---|
62 | return rc;
|
---|
63 | }
|
---|
64 | //******************************************************************************
|
---|
65 | //******************************************************************************
|
---|
66 | DWORD OSLibDosFreeMem(LPVOID lpMemAddr)
|
---|
67 | {
|
---|
68 | return DosFreeMem(lpMemAddr);
|
---|
69 | }
|
---|
70 | //******************************************************************************
|
---|
71 | //NOTE: If name == NULL, allocated gettable unnamed shared memory
|
---|
72 | //******************************************************************************
|
---|
73 | DWORD OSLibDosAllocSharedMem(LPVOID *lplpMemAddr, DWORD size, DWORD flags, LPSTR name)
|
---|
74 | {
|
---|
75 | APIRET rc;
|
---|
76 | char *sharedmemname = NULL;
|
---|
77 |
|
---|
78 | if(name) {
|
---|
79 | sharedmemname = (char *)malloc(strlen(name) + 16);
|
---|
80 | strcpy(sharedmemname, "\\SHAREMEM\\");
|
---|
81 | strcat(sharedmemname, name);
|
---|
82 | }
|
---|
83 | else flags |= OBJ_GETTABLE;
|
---|
84 |
|
---|
85 | rc = DosAllocSharedMem(lplpMemAddr, sharedmemname, size, flags);
|
---|
86 | if(name) {
|
---|
87 | free(sharedmemname);
|
---|
88 | }
|
---|
89 | return rc;
|
---|
90 | }
|
---|
91 | //******************************************************************************
|
---|
92 | //NOTE: If name == NULL, assume gettable unnamed shared memory
|
---|
93 | //******************************************************************************
|
---|
94 | DWORD OSLibDosGetNamedSharedMem(LPVOID *lplpMemAddr, LPSTR name)
|
---|
95 | {
|
---|
96 | APIRET rc;
|
---|
97 | char *sharedmemname = NULL;
|
---|
98 |
|
---|
99 | if(name) {
|
---|
100 | sharedmemname = (char *)malloc(strlen(name) + 16);
|
---|
101 | strcpy(sharedmemname, "\\SHAREMEM\\");
|
---|
102 | strcat(sharedmemname, name);
|
---|
103 | rc = DosGetNamedSharedMem(lplpMemAddr, sharedmemname, PAG_READ|PAG_WRITE);
|
---|
104 | if(name) {
|
---|
105 | free(sharedmemname);
|
---|
106 | }
|
---|
107 | }
|
---|
108 | else rc = DosGetSharedMem((LPVOID)*(DWORD *)lplpMemAddr, PAG_READ|PAG_WRITE);
|
---|
109 |
|
---|
110 | return rc;
|
---|
111 | }
|
---|
112 | //******************************************************************************
|
---|
113 | //******************************************************************************
|
---|
114 | DWORD OSLibDosQueryMem(LPVOID lpMemAddr, DWORD *lpRangeSize, DWORD *lpAttr)
|
---|
115 | {
|
---|
116 | return DosQueryMem(lpMemAddr, lpRangeSize, lpAttr);
|
---|
117 | }
|
---|
118 | //******************************************************************************
|
---|
119 | //******************************************************************************
|
---|
120 | DWORD OSLibDosSetMem(LPVOID lpMemAddr, DWORD size, DWORD flags)
|
---|
121 | {
|
---|
122 | APIRET rc;
|
---|
123 |
|
---|
124 | rc = DosSetMem(lpMemAddr, size, flags);
|
---|
125 | switch(rc) {
|
---|
126 | case ERROR_INVALID_ADDRESS:
|
---|
127 | return OSLIB_ERROR_INVALID_ADDRESS;
|
---|
128 | case ERROR_ACCESS_DENIED:
|
---|
129 | return OSLIB_ERROR_ACCESS_DENIED;
|
---|
130 | default:
|
---|
131 | return rc;
|
---|
132 | }
|
---|
133 | }
|
---|
134 | //******************************************************************************
|
---|
135 | //******************************************************************************
|
---|
136 | DWORD OSLibDosOpen(char *lpszFileName, DWORD flags)
|
---|
137 | {
|
---|
138 | APIRET rc;
|
---|
139 | HFILE hFile;
|
---|
140 | ULONG ulAction;
|
---|
141 | DWORD os2flags = OPEN_FLAGS_NOINHERIT;
|
---|
142 |
|
---|
143 |
|
---|
144 | if(flags & OSLIB_ACCESS_READONLY)
|
---|
145 | os2flags |= OPEN_ACCESS_READONLY;
|
---|
146 | else
|
---|
147 | if(flags & OSLIB_ACCESS_READWRITE)
|
---|
148 | os2flags |= OPEN_ACCESS_READWRITE;
|
---|
149 |
|
---|
150 | if(flags & OSLIB_ACCESS_SHAREDENYNONE)
|
---|
151 | os2flags |= OPEN_SHARE_DENYNONE;
|
---|
152 | else
|
---|
153 | if(flags & OSLIB_ACCESS_SHAREDENYREAD)
|
---|
154 | os2flags |= OPEN_SHARE_DENYREAD;
|
---|
155 | else
|
---|
156 | if(flags & OSLIB_ACCESS_SHAREDENYWRITE)
|
---|
157 | os2flags |= OPEN_SHARE_DENYWRITE;
|
---|
158 |
|
---|
159 | rc = DosOpen(lpszFileName, /* File path name */
|
---|
160 | &hFile, /* File handle */
|
---|
161 | &ulAction, /* Action taken */
|
---|
162 | 0L, /* File primary allocation */
|
---|
163 | 0L, /* File attribute */
|
---|
164 | OPEN_ACTION_FAIL_IF_NEW |
|
---|
165 | OPEN_ACTION_OPEN_IF_EXISTS, /* Open function type */
|
---|
166 | os2flags,
|
---|
167 | 0L); /* No extended attribute */
|
---|
168 |
|
---|
169 | if(rc) {
|
---|
170 | return 0;
|
---|
171 | }
|
---|
172 | else return hFile;
|
---|
173 | }
|
---|
174 | //******************************************************************************
|
---|
175 | //******************************************************************************
|
---|
176 | DWORD OSLibDosClose(DWORD hFile)
|
---|
177 | {
|
---|
178 | return DosClose(hFile);
|
---|
179 | }
|
---|
180 | //******************************************************************************
|
---|
181 | //******************************************************************************
|
---|
182 | DWORD OSLibDosGetFileSize(DWORD hFile)
|
---|
183 | {
|
---|
184 | ULONG ulLocal, filesize = 0;
|
---|
185 |
|
---|
186 | DosSetFilePtr(hFile, 0L, FILE_BEGIN, &ulLocal);
|
---|
187 | DosSetFilePtr(hFile, 0L, FILE_END, &filesize);
|
---|
188 | DosSetFilePtr(hFile, 0L, FILE_BEGIN, &ulLocal);
|
---|
189 | return filesize;
|
---|
190 | }
|
---|
191 | //******************************************************************************
|
---|
192 | //******************************************************************************
|
---|
193 | DWORD OSLibDosRead(DWORD hFile, LPVOID lpBuffer, DWORD size, DWORD *nrBytesRead)
|
---|
194 | {
|
---|
195 | return DosRead(hFile, lpBuffer, size, nrBytesRead);
|
---|
196 | }
|
---|
197 | //******************************************************************************
|
---|
198 | //******************************************************************************
|
---|
199 | DWORD OSLibDosWrite(DWORD hFile, LPVOID lpBuffer, DWORD size, DWORD *nrBytesWritten)
|
---|
200 | {
|
---|
201 | return DosWrite(hFile, lpBuffer, size, nrBytesWritten);
|
---|
202 | }
|
---|
203 | //******************************************************************************
|
---|
204 | //******************************************************************************
|
---|
205 | DWORD OSLibDosSetFilePtr(DWORD hFile, DWORD offset, DWORD method)
|
---|
206 | {
|
---|
207 | DWORD os2method;
|
---|
208 | DWORD newoffset;
|
---|
209 | APIRET rc;
|
---|
210 |
|
---|
211 | switch(method) {
|
---|
212 | case OSLIB_SETPTR_FILE_CURRENT:
|
---|
213 | os2method = FILE_CURRENT;
|
---|
214 | break;
|
---|
215 | case OSLIB_SETPTR_FILE_BEGIN:
|
---|
216 | os2method = FILE_BEGIN ;
|
---|
217 | break;
|
---|
218 | case OSLIB_SETPTR_FILE_END:
|
---|
219 | os2method = FILE_END;
|
---|
220 | break;
|
---|
221 | default:
|
---|
222 | return OSLIB_ERROR_INVALID_PARAMETER;
|
---|
223 | }
|
---|
224 | rc = DosSetFilePtr(hFile, offset, os2method, &newoffset);
|
---|
225 | if(rc) {
|
---|
226 | return -1;
|
---|
227 | }
|
---|
228 | else return newoffset;
|
---|
229 | }
|
---|
230 | //******************************************************************************
|
---|
231 | //******************************************************************************
|
---|
232 | //@@@PH Note: this routine is nothing but a QUICK'N'DIRTY HACK!
|
---|
233 | //@@@PH this function should be implemented accordingly to NTDLL's
|
---|
234 | // RtlSecondsSince1980ToTime
|
---|
235 | // RtlTimeToSecondsSince1980
|
---|
236 | static void iFDATEFTIME2FILETIME(FDATE fdOS2, FTIME ftOS2, LPFILETIME pftWin32)
|
---|
237 | {
|
---|
238 | float f;
|
---|
239 | #define facSECOND 2 // as encoded in OS/2
|
---|
240 | #define facMINUTE 60
|
---|
241 | #define facHOUR 3600
|
---|
242 | #define facDAY 86400
|
---|
243 | #define facMONTH facDAY * 30 // cough, cough :)
|
---|
244 | #define facYEAR facDAY * 365
|
---|
245 |
|
---|
246 | /* pftWin32 is 100ns based from 01.01.1601 00:00:00 */
|
---|
247 | f = (fdOS2.year + 379) * facYEAR // 1980 - 1601
|
---|
248 | + (fdOS2.month - 0 ) * facMONTH
|
---|
249 | + (fdOS2.day - 1 ) * facDAY
|
---|
250 | + (ftOS2.hours ) * facHOUR
|
---|
251 | + (ftOS2.minutes ) * facMINUTE
|
---|
252 | + (ftOS2.twosecs ) * facSECOND;
|
---|
253 |
|
---|
254 | f *= 10000; // convert to 100ns base
|
---|
255 | pftWin32->dwHighDateTime = (f / (float)(0xffffffff) );
|
---|
256 | pftWin32->dwLowDateTime = (f - (float)((float)pftWin32->dwHighDateTime *
|
---|
257 | (float)0xffffffff) );
|
---|
258 | }
|
---|
259 |
|
---|
260 | BOOL OSLibDosGetFileAttributesEx(PSZ pszName,
|
---|
261 | ULONG ulDummy,
|
---|
262 | PVOID pBuffer)
|
---|
263 | {
|
---|
264 | APIRET rc; /* API return code */
|
---|
265 | FILESTATUS3 fs3; /* file information structure */
|
---|
266 | LPWIN32_FILE_ATTRIBUTE_DATA lpFad = (LPWIN32_FILE_ATTRIBUTE_DATA) pBuffer;
|
---|
267 |
|
---|
268 | // Note: we only handle standard "GetFileExInfoStandard" requests
|
---|
269 | rc = DosQueryPathInfo(pszName, /* query the file information */
|
---|
270 | FIL_STANDARD,
|
---|
271 | &fs3,
|
---|
272 | sizeof(fs3));
|
---|
273 | if (rc != NO_ERROR) /* check for errors */
|
---|
274 | return FALSE; /* raise error condition */
|
---|
275 |
|
---|
276 | // convert structure
|
---|
277 | lpFad->dwFileAttributes = fs3.attrFile; // directly interchangeable
|
---|
278 | iFDATEFTIME2FILETIME(fs3.fdateCreation, fs3.ftimeCreation, &lpFad->ftCreationTime);
|
---|
279 | iFDATEFTIME2FILETIME(fs3.fdateLastAccess, fs3.ftimeLastAccess, &lpFad->ftLastAccessTime);
|
---|
280 | iFDATEFTIME2FILETIME(fs3.fdateLastWrite, fs3.ftimeLastWrite, &lpFad->ftLastWriteTime);
|
---|
281 |
|
---|
282 | /* @@@PH we might add Aurora support ...
|
---|
283 | lpFad->nFileSizeHigh = info.nFileSizeHigh;
|
---|
284 | */
|
---|
285 | lpFad->nFileSizeHigh = 0;
|
---|
286 | lpFad->nFileSizeLow = fs3.cbFile;
|
---|
287 |
|
---|
288 | return TRUE;
|
---|
289 | }
|
---|
290 | //******************************************************************************
|
---|
291 | //******************************************************************************
|
---|
292 |
|
---|
293 | #define OSLIB_SEARCHDIR 1
|
---|
294 | #define OSLIB_SEARCHCURDIR 2
|
---|
295 | #define OSLIB_SEARCHFILE 3
|
---|
296 | #define OSLIB_SEARCHENV 4
|
---|
297 |
|
---|
298 | DWORD OSLibDosSearchPath(DWORD cmd, char *path, char *name, char *full_name,
|
---|
299 | DWORD length_fullname)
|
---|
300 | {
|
---|
301 | switch(cmd) {
|
---|
302 | case OSLIB_SEARCHDIR:
|
---|
303 | if(DosSearchPath(SEARCH_IGNORENETERRS, path,
|
---|
304 | name, full_name, length_fullname) != 0) {
|
---|
305 | return 0;
|
---|
306 | }
|
---|
307 | return strlen(full_name);
|
---|
308 |
|
---|
309 |
|
---|
310 | case OSLIB_SEARCHCURDIR:
|
---|
311 | if(DosSearchPath(SEARCH_IGNORENETERRS | SEARCH_CUR_DIRECTORY, path,
|
---|
312 | name, full_name, length_fullname) != 0) {
|
---|
313 | return 0;
|
---|
314 | }
|
---|
315 | return strlen(full_name);
|
---|
316 |
|
---|
317 | case OSLIB_SEARCHFILE:
|
---|
318 | {
|
---|
319 | FILESTATUS3 fileinfo;
|
---|
320 |
|
---|
321 | if(DosQueryPathInfo(name, FIL_STANDARD, &fileinfo, sizeof(fileinfo)) != 0) {
|
---|
322 | return 0;
|
---|
323 | }
|
---|
324 | strncpy(full_name, name, length_fullname);
|
---|
325 | return strlen(full_name);
|
---|
326 | }
|
---|
327 |
|
---|
328 | case OSLIB_SEARCHENV:
|
---|
329 | {
|
---|
330 | char *env = getenv(path);
|
---|
331 | if(env == NULL)
|
---|
332 | return 0;
|
---|
333 |
|
---|
334 | while(*env != '=') env++;
|
---|
335 | env++;
|
---|
336 | while(*env == ' ') env++;
|
---|
337 | if(DosSearchPath(SEARCH_IGNORENETERRS | SEARCH_ENVIRONMENT, env,
|
---|
338 | name, full_name, length_fullname) != 0) {
|
---|
339 | return 0;
|
---|
340 | }
|
---|
341 | return strlen(full_name);
|
---|
342 | }
|
---|
343 | }
|
---|
344 | return 0;
|
---|
345 | }
|
---|
346 | //******************************************************************************
|
---|
347 | //******************************************************************************
|
---|