source: trunk/src/kernel32/oslibmisc.cpp@ 9678

Last change on this file since 9678 was 9678, checked in by sandervl, 23 years ago

fixed selector to flat translation

File size: 9.6 KB
Line 
1/* $Id: oslibmisc.cpp,v 1.16 2003-01-16 00:44:31 sandervl Exp $ */
2/*
3 * Misc OS/2 util. procedures
4 *
5 * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl)
6 * Copyright 1998 Peter FitzSimmons
7 * Copyright 1998 Patrick Haller
8 *
9 *
10 * Project Odin Software License can be found in LICENSE.TXT
11 *
12 */
13#define INCL_WIN
14#define INCL_BASE
15#define INCL_DOSPROCESS
16#define INCL_DOSSEL
17#define INCL_DOSNLS /* National Language Support values */
18#include <os2wrap.h> //Odin32 OS/2 api wrappers
19#include <string.h>
20#include <stdlib.h>
21#include <stdio.h> /*PLF Wed 98-03-18 05:15:04*/
22#include <malloc.h> /*PLF Wed 98-03-18 05:15:04*/
23#include "oslibmisc.h"
24#include <misc.h>
25
26#define DBG_LOCALLOG DBG_oslibmisc
27#include "dbglocal.h"
28
29//******************************************************************************
30//TODO: not reentrant!
31//******************************************************************************
32char *OSLibGetDllName(ULONG hModule)
33{
34 static char modname[CCHMAXPATH] = {0};
35
36 if(DosQueryModuleName(hModule, CCHMAXPATH, modname) != 0) {
37 return NULL;
38 }
39 return(modname);
40}
41//******************************************************************************
42//******************************************************************************
43BOOL OSLibGetDllName(ULONG hModule, char *name, int length)
44{
45 return DosQueryModuleName(hModule, length, name) == 0;
46}
47//******************************************************************************
48/******************************************************************************
49 * Name : ULONG OSLibiGetModuleHandleA
50 * Purpose : replacement for IBM Open32's GetModuleHandle
51 * Parameters: LPCTSTR lpszModule
52 * Variables :
53 * Result : HMODULE hModule or NULLHANDLE in case of error
54 * Remark :
55 * Status : REWRITTEN UNTESTED
56 *
57 * Author : Patrick Haller [Sun, 1998/04/04 01:55]
58 *****************************************************************************/
59
60ULONG OSLibiGetModuleHandleA(char * pszModule)
61{
62 HMODULE hModule; /* module handle */
63 APIRET rc; /* API returncode */
64 static HMODULE hModuleExe = 0; /* "cached" hModuleExe */
65 PTIB pTIB; /* parameters for DosGetInfoBlocks */
66 PPIB pPIB;
67
68 dprintf(("KERNEL32:GetModuleHandle(%x)\n",
69 pszModule));
70
71 /* @@@PH 98/04/04
72
73 this Open32 function is broken for pszModule == NULL
74 return(GetModuleHandle(pszModule));
75
76 Open32 always returns -1 here, however it should return the handle
77 of the current process. MFC30 crashes.
78
79 SvL, me thinks for PELDR support, you'll have to rewrite
80 this code anyway :)
81
82 */
83
84 if (NULL == pszModule) /* obtain handle to current executable */
85 {
86 if (hModuleExe != NULLHANDLE) /* do we have a cached handle ? */
87 return (hModuleExe);
88
89 rc = DosGetInfoBlocks(&pTIB, /* get the info blocks */
90 &pPIB);
91 if (rc != NO_ERROR) /* check for errors */
92 {
93 return (NULLHANDLE); /* signal failure */
94 }
95
96 hModuleExe = pPIB->pib_hmte; /* set cached module */
97 hModule = pPIB->pib_hmte; /* module table entry ID */
98 }
99 else
100 {
101 rc = DosQueryModuleHandle(pszModule, /* query module handle */
102 &hModule);
103
104 if (rc != NO_ERROR) /* check for errors */
105 {
106 return (NULLHANDLE); /* signal failure */
107 }
108 }
109
110 return (hModule); /* return determined handle */
111}
112
113
114ULONG OSLibQueryModuleHandle(char *modname)
115{
116 HMODULE hModule;
117 APIRET rc;
118
119 rc = DosQueryModuleHandle(modname, /* query module handle */
120 &hModule);
121 if(rc)
122 return(-1);
123
124 return(hModule);
125}
126
127void OSLibWait(ULONG msec)
128{
129 DosSleep(msec);
130}
131
132//******************************************************************************
133//Wrapper for Dos16AllocSeg
134//******************************************************************************
135ULONG OSLibAllocSel(ULONG size, USHORT *selector)
136{
137#if 1
138 PVOID pSelMem;
139 ULONG sel;
140 APIRET rc;
141
142 rc = DosAllocMem(&pSelMem, size, PAG_COMMIT|PAG_READ|PAG_WRITE|OBJ_TILE);
143 if(rc != NO_ERROR) {
144 dprintf(("OSLibAllocSel: DosAllocMem failed with %d", rc));
145 DebugInt3();
146 return FALSE;
147 }
148 *selector = (DosFlatToSel((ULONG)pSelMem) >> 16);
149 return *selector != 0;
150#else
151 return (Dos16AllocSeg(size, selector, SEG_NONSHARED) == 0);
152#endif
153}
154//******************************************************************************
155//Wrapper for Dos16FreeSeg
156//******************************************************************************
157ULONG OSLibFreeSel(USHORT selector)
158{
159#if 1
160 PVOID pSelMem;
161 APIRET rc;
162
163 pSelMem = (PVOID)DosSelToFlat(selector << 16);
164 rc = DosFreeMem(pSelMem);
165 return rc == NO_ERROR;
166#else
167 return (Dos16FreeSeg(selector) == 0);
168#endif
169}
170//******************************************************************************
171//Wrapper for Dos32SelToFlat
172//******************************************************************************
173PVOID OSLibSelToFlat(USHORT selector)
174{
175 return (PVOID)DosSelToFlat(selector << 16);
176}
177//******************************************************************************
178//Get TIB data
179//******************************************************************************
180ULONG OSLibGetTIB(int tiboff)
181{
182 PTIB ptib;
183 PPIB ppib;
184 APIRET rc;
185
186 rc = DosGetInfoBlocks(&ptib, &ppib);
187 if(rc) {
188 return 0;
189 }
190 switch(tiboff)
191 {
192 case TIB_STACKTOP:
193 return (ULONG)ptib->tib_pstacklimit;
194 case TIB_STACKLOW:
195 return (ULONG)ptib->tib_pstack;
196 default:
197 return 0;
198 }
199}
200
201/**
202 * Gets a PIB data.
203 * @returns Requested PIB data.
204 * 0 may indicate error or that the PIB data you requested actually is 0.
205 * @param iPIB PIB data index. (one of the PIB_* defines in oslibmisc.h)
206 * @author
207 * @remark Spooky error handling.
208 */
209ULONG OSLibGetPIB(int iPIB)
210{
211 PTIB ptib;
212 PPIB ppib;
213 APIRET rc;
214
215 rc = DosGetInfoBlocks(&ptib, &ppib);
216 if (rc)
217 {
218 dprintf(("KERNEL32: OSLibGetPIB(%d): DosGetInfoBlocks failed with rc=%d\n", iPIB, rc));
219 return 0;
220 }
221
222 switch(iPIB)
223 {
224 case PIB_TASKHNDL:
225 return ppib->pib_hmte;
226
227 case PIB_TASKTYPE:
228 return (ppib->pib_ultype == 3) ? TASKTYPE_PM : TASKTYPE_VIO;
229
230 case PIB_PCHCMD:
231 return (ULONG)ppib->pib_pchcmd;
232
233 default:
234 dprintf(("KERNEL32: OSLibGetPIB(%d): Invalid PIB data index\n.", iPIB));
235 DebugInt3();
236 return 0;
237 }
238}
239//******************************************************************************
240//Allocate local thread memory
241//******************************************************************************
242ULONG OSLibAllocThreadLocalMemory(int nrdwords)
243{
244 APIRET rc;
245 PULONG thrdaddr;
246
247 rc = DosAllocThreadLocalMemory(nrdwords, &thrdaddr);
248 if(rc) {
249 dprintf(("DosAllocThreadLocalMemory failed %d", rc));
250 return 0;
251 }
252 return (ULONG)thrdaddr;
253}
254//******************************************************************************
255//******************************************************************************
256char *OSLibStripPath(char *path)
257{
258 /* @@@PH what does this function do ? Strip the path from a FQFN name ? */
259 char *pszFilename;
260 char *pszFilename1;
261
262 pszFilename = strrchr(path, '\\'); /* find rightmost backslash */
263 pszFilename1 = strrchr(path, '/'); /* find rightmost slash */
264 if(pszFilename > pszFilename1 && pszFilename != NULL)
265 return (++pszFilename); /* return pointer to next character */
266
267 if (pszFilename1 != NULL)
268 return (++pszFilename1); /* return pointer to next character */
269
270 return (path); /* default return value */
271}
272//******************************************************************************
273//******************************************************************************
274ULONG OSLibWinInitialize()
275{
276 return (ULONG)WinInitialize(0);
277}
278//******************************************************************************
279//******************************************************************************
280ULONG OSLibWinQueryMsgQueue(ULONG hab)
281{
282 ULONG hmq;
283
284 hmq = (ULONG)WinCreateMsgQueue((HAB)hab, 0);
285 if(!hmq) {
286 PTIB ptib;
287 PPIB ppib;
288
289 DosGetInfoBlocks(&ptib, &ppib);
290
291 hmq = WinQueueFromID(hab, ppib->pib_ulpid, ptib->tib_ptib2->tib2_ultid);
292 }
293 return hmq;
294}
295//******************************************************************************
296//******************************************************************************
297ULONG OSLibWinSetCp(ULONG hmq, ULONG codepage)
298{
299 return WinSetCp(hmq, codepage);
300}
301//******************************************************************************
302//******************************************************************************
303ULONG OSLibQueryCountry()
304{
305 COUNTRYCODE Country = {0}; /* Country code info (0 = current country) */
306 COUNTRYINFO CtryInfo = {0}; /* Buffer for country-specific information */
307 ULONG ulInfoLen = 0;
308 APIRET rc = NO_ERROR; /* Return code */
309
310 rc = DosQueryCtryInfo(sizeof(CtryInfo), &Country,
311 &CtryInfo, &ulInfoLen);
312
313 if (rc != NO_ERROR) {
314 return -1;
315 }
316 return CtryInfo.country;
317}
318//******************************************************************************
319//******************************************************************************
Note: See TracBrowser for help on using the repository browser.