| 1 | /* $Id: oslibmisc.cpp,v 1.3 1999-10-09 13:33:24 sandervl Exp $ */
|
|---|
| 2 |
|
|---|
| 3 | /*
|
|---|
| 4 | * Misc OS/2 util. procedures
|
|---|
| 5 | *
|
|---|
| 6 | * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl)
|
|---|
| 7 | * Copyright 1998 Peter FitzSimmons
|
|---|
| 8 | * Copyright 1998 Patrick Haller
|
|---|
| 9 | *
|
|---|
| 10 | *
|
|---|
| 11 | * Project Odin Software License can be found in LICENSE.TXT
|
|---|
| 12 | *
|
|---|
| 13 | */
|
|---|
| 14 | #define INCL_BASE
|
|---|
| 15 | #define INCL_DOSPROCESS
|
|---|
| 16 | #define INCL_DOSSEL
|
|---|
| 17 | #include <os2wrap.h> //Odin32 OS/2 api wrappers
|
|---|
| 18 | #include <string.h>
|
|---|
| 19 | #include <stdlib.h>
|
|---|
| 20 | #include <stdio.h> /*PLF Wed 98-03-18 05:15:04*/
|
|---|
| 21 | #include <malloc.h> /*PLF Wed 98-03-18 05:15:04*/
|
|---|
| 22 | #include "oslibmisc.h"
|
|---|
| 23 | #include <misc.h>
|
|---|
| 24 |
|
|---|
| 25 | /***********************************
|
|---|
| 26 | * PH: fixups for missing os2win.h *
|
|---|
| 27 | ***********************************/
|
|---|
| 28 |
|
|---|
| 29 | void _System SetLastError(ULONG ulError);
|
|---|
| 30 |
|
|---|
| 31 | //******************************************************************************
|
|---|
| 32 | //******************************************************************************
|
|---|
| 33 | void OSLibSetExitList(unsigned long handler)
|
|---|
| 34 | {
|
|---|
| 35 | APIRET rc;
|
|---|
| 36 |
|
|---|
| 37 | rc = DosExitList(EXLST_ADD | 0x00002A00, (PFNEXITLIST)handler);
|
|---|
| 38 | if(rc) {
|
|---|
| 39 | dprintf(("DosExitList returned %d\n", rc));
|
|---|
| 40 | }
|
|---|
| 41 | }
|
|---|
| 42 | //******************************************************************************
|
|---|
| 43 | //******************************************************************************
|
|---|
| 44 | void OSLibClearExitList()
|
|---|
| 45 | {
|
|---|
| 46 | DosExitList(EXLST_EXIT, NULL);
|
|---|
| 47 | }
|
|---|
| 48 | //******************************************************************************
|
|---|
| 49 | //******************************************************************************
|
|---|
| 50 | void OSLibRemoveExitList(unsigned long handler)
|
|---|
| 51 | {
|
|---|
| 52 | DosExitList(EXLST_REMOVE, (PFNEXITLIST)handler);
|
|---|
| 53 | }
|
|---|
| 54 | //******************************************************************************
|
|---|
| 55 | //TODO: not reentrant!
|
|---|
| 56 | //******************************************************************************
|
|---|
| 57 | char *OSLibGetDllName(ULONG hModule)
|
|---|
| 58 | {
|
|---|
| 59 | static char modname[CCHMAXPATH] = {0};
|
|---|
| 60 |
|
|---|
| 61 | if(DosQueryModuleName(hModule, CCHMAXPATH, modname) != 0) {
|
|---|
| 62 | return NULL;
|
|---|
| 63 | }
|
|---|
| 64 | return(modname);
|
|---|
| 65 | }
|
|---|
| 66 | //******************************************************************************
|
|---|
| 67 | /*****************************************************************************
|
|---|
| 68 | * Name : ULONG OSLibiGetModuleHandleA
|
|---|
| 69 | * Purpose : replacement for IBM Open32's GetModuleHandle
|
|---|
| 70 | * Parameters: LPCTSTR lpszModule
|
|---|
| 71 | * Variables :
|
|---|
| 72 | * Result : HMODULE hModule or NULLHANDLE in case of error
|
|---|
| 73 | * Remark :
|
|---|
| 74 | * Status : REWRITTEN UNTESTED
|
|---|
| 75 | *
|
|---|
| 76 | * Author : Patrick Haller [Sun, 1998/04/04 01:55]
|
|---|
| 77 | *****************************************************************************/
|
|---|
| 78 |
|
|---|
| 79 | ULONG OSLibiGetModuleHandleA(char * pszModule)
|
|---|
| 80 | {
|
|---|
| 81 | HMODULE hModule; /* module handle */
|
|---|
| 82 | APIRET rc; /* API returncode */
|
|---|
| 83 | static HMODULE hModuleExe; /* "cached" hModuleExe */
|
|---|
| 84 | PTIB pTIB; /* parameters for DosGetInfoBlocks */
|
|---|
| 85 | PPIB pPIB;
|
|---|
| 86 |
|
|---|
| 87 | dprintf(("KERNEL32:GetModuleHandle(%s)\n",
|
|---|
| 88 | pszModule));
|
|---|
| 89 |
|
|---|
| 90 | /* @@@PH 98/04/04
|
|---|
| 91 |
|
|---|
| 92 | this Open32 function is broken for pszModule == NULL
|
|---|
| 93 | return(GetModuleHandle(pszModule));
|
|---|
| 94 |
|
|---|
| 95 | Open32 always returns -1 here, however it should return the handle
|
|---|
| 96 | of the current process. MFC30 crashes.
|
|---|
| 97 |
|
|---|
| 98 | SvL, me thinks for PELDR support, you'll have to rewrite
|
|---|
| 99 | this code anyway :)
|
|---|
| 100 |
|
|---|
| 101 | */
|
|---|
| 102 |
|
|---|
| 103 | if (NULL == pszModule) /* obtain handle to current executable */
|
|---|
| 104 | {
|
|---|
| 105 | if (hModuleExe != NULLHANDLE) /* do we have a cached handle ? */
|
|---|
| 106 | return (hModuleExe);
|
|---|
| 107 |
|
|---|
| 108 | rc = DosGetInfoBlocks(&pTIB, /* get the info blocks */
|
|---|
| 109 | &pPIB);
|
|---|
| 110 | if (rc != NO_ERROR) /* check for errors */
|
|---|
| 111 | {
|
|---|
| 112 | SetLastError(rc); /* set error code */
|
|---|
| 113 | return (NULLHANDLE); /* signal failure */
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | hModuleExe = pPIB->pib_hmte; /* set cached module */
|
|---|
| 117 | hModule = pPIB->pib_hmte; /* module table entry ID */
|
|---|
| 118 | }
|
|---|
| 119 | else
|
|---|
| 120 | {
|
|---|
| 121 | rc = DosQueryModuleHandle(pszModule, /* query module handle */
|
|---|
| 122 | &hModule);
|
|---|
| 123 |
|
|---|
| 124 | if (rc != NO_ERROR) /* check for errors */
|
|---|
| 125 | {
|
|---|
| 126 | SetLastError(rc); /* set error code */
|
|---|
| 127 | return (NULLHANDLE); /* signal failure */
|
|---|
| 128 | }
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | return (hModule); /* return determined handle */
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 | ULONG OSLibQueryModuleHandle(char *modname)
|
|---|
| 136 | {
|
|---|
| 137 | HMODULE hModule;
|
|---|
| 138 | APIRET rc;
|
|---|
| 139 |
|
|---|
| 140 | rc = DosQueryModuleHandle(modname, /* query module handle */
|
|---|
| 141 | &hModule);
|
|---|
| 142 | if(rc)
|
|---|
| 143 | return(-1);
|
|---|
| 144 |
|
|---|
| 145 | return(hModule);
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | //SvL: only for RT_RCDATA!
|
|---|
| 149 | ULONG OSLibGetResourceSize(HMODULE hinstance, int id)
|
|---|
| 150 | {
|
|---|
| 151 | APIRET rc;
|
|---|
| 152 | ULONG size;
|
|---|
| 153 |
|
|---|
| 154 | rc = DosQueryResourceSize(hinstance, RT_RCDATA, id, &size);
|
|---|
| 155 | if(rc) {
|
|---|
| 156 | dprintf(("DosQueryResourceSize returned %d, %X id = %d\n", rc, hinstance, id));
|
|---|
| 157 | return(0);
|
|---|
| 158 | }
|
|---|
| 159 | return(size);
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | ULONG OSLibGetResource(HMODULE hinstance, int id, char *destbuf, int bufLength)
|
|---|
| 163 | {
|
|---|
| 164 | APIRET rc;
|
|---|
| 165 | char *resdata;
|
|---|
| 166 | ULONG size;
|
|---|
| 167 |
|
|---|
| 168 | rc = DosQueryResourceSize(hinstance, RT_RCDATA, id, &size);
|
|---|
| 169 | if(rc) {
|
|---|
| 170 | dprintf(("OSLibGetResource: Can't get resource size of %d!!!\n", id));
|
|---|
| 171 | return(FALSE);
|
|---|
| 172 | }
|
|---|
| 173 | rc = DosGetResource(hinstance, RT_RCDATA, id, (PPVOID)&resdata);
|
|---|
| 174 | if(rc) {
|
|---|
| 175 | dprintf(("OSLibGetResource: Can't find resource %d!!!\n", id));
|
|---|
| 176 | return(FALSE);
|
|---|
| 177 | }
|
|---|
| 178 | dprintf(("OSLibGetResoure: bufLength %d, size %d, id %d", bufLength, size, id));
|
|---|
| 179 | size = min(size, bufLength);
|
|---|
| 180 | memcpy(destbuf, resdata, size);
|
|---|
| 181 | DosFreeResource(resdata);
|
|---|
| 182 |
|
|---|
| 183 | return(TRUE);
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | void OSLibWait(ULONG msec)
|
|---|
| 187 | {
|
|---|
| 188 | DosSleep(msec);
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | //******************************************************************************
|
|---|
| 192 | //Wrapper for Dos16AllocSeg
|
|---|
| 193 | //******************************************************************************
|
|---|
| 194 | ULONG OSLibAllocSel(ULONG size, USHORT *selector)
|
|---|
| 195 | {
|
|---|
| 196 | return (Dos16AllocSeg(size, selector, SEG_NONSHARED) == 0);
|
|---|
| 197 | }
|
|---|
| 198 | //******************************************************************************
|
|---|
| 199 | //Wrapper for Dos16FreeSeg
|
|---|
| 200 | //******************************************************************************
|
|---|
| 201 | ULONG OSLibFreeSel(USHORT selector)
|
|---|
| 202 | {
|
|---|
| 203 | return (Dos16FreeSeg(selector) == 0);
|
|---|
| 204 | }
|
|---|
| 205 | //******************************************************************************
|
|---|
| 206 | //Wrapper for Dos32SelToFlat
|
|---|
| 207 | //******************************************************************************
|
|---|
| 208 | PVOID OSLibSelToFlat(USHORT selector)
|
|---|
| 209 | {
|
|---|
| 210 | return (PVOID)DosSelToFlat(selector << 16);
|
|---|
| 211 | }
|
|---|
| 212 | //******************************************************************************
|
|---|
| 213 | //Get TIB data
|
|---|
| 214 | //******************************************************************************
|
|---|
| 215 | ULONG OSLibGetTIB(int tiboff)
|
|---|
| 216 | {
|
|---|
| 217 | PTIB ptib;
|
|---|
| 218 | PPIB ppib;
|
|---|
| 219 | APIRET rc;
|
|---|
| 220 |
|
|---|
| 221 | rc = DosGetInfoBlocks(&ptib, &ppib);
|
|---|
| 222 | if(rc) {
|
|---|
| 223 | return 0;
|
|---|
| 224 | }
|
|---|
| 225 | switch(tiboff)
|
|---|
| 226 | {
|
|---|
| 227 | case TIB_STACKTOP:
|
|---|
| 228 | return (ULONG)ptib->tib_pstacklimit;
|
|---|
| 229 | case TIB_STACKLOW:
|
|---|
| 230 | return (ULONG)ptib->tib_pstack;
|
|---|
| 231 | default:
|
|---|
| 232 | return 0;
|
|---|
| 233 | }
|
|---|
| 234 | }
|
|---|
| 235 | //******************************************************************************
|
|---|
| 236 | //Get PIB data
|
|---|
| 237 | //******************************************************************************
|
|---|
| 238 | ULONG OSLibGetPIB(int piboff)
|
|---|
| 239 | {
|
|---|
| 240 | PTIB ptib;
|
|---|
| 241 | PPIB ppib;
|
|---|
| 242 | APIRET rc;
|
|---|
| 243 |
|
|---|
| 244 | rc = DosGetInfoBlocks(&ptib, &ppib);
|
|---|
| 245 | if(rc) {
|
|---|
| 246 | return 0;
|
|---|
| 247 | }
|
|---|
| 248 | switch(piboff)
|
|---|
| 249 | {
|
|---|
| 250 | case PIB_TASKHNDL:
|
|---|
| 251 | return ppib->pib_hmte;
|
|---|
| 252 | case PIB_TASKTYPE:
|
|---|
| 253 | if(ppib->pib_ultype == 3) {
|
|---|
| 254 | return TASKTYPE_PM;
|
|---|
| 255 | }
|
|---|
| 256 | else return TASKTYPE_VIO;
|
|---|
| 257 | default:
|
|---|
| 258 | return 0;
|
|---|
| 259 | }
|
|---|
| 260 | }
|
|---|
| 261 | //******************************************************************************
|
|---|
| 262 | //Allocate local thread memory
|
|---|
| 263 | //******************************************************************************
|
|---|
| 264 | ULONG OSLibAllocThreadLocalMemory(int nrdwords)
|
|---|
| 265 | {
|
|---|
| 266 | APIRET rc;
|
|---|
| 267 | PULONG thrdaddr;
|
|---|
| 268 |
|
|---|
| 269 | rc = DosAllocThreadLocalMemory(nrdwords, &thrdaddr);
|
|---|
| 270 | if(rc) {
|
|---|
| 271 | dprintf(("DosAllocThreadLocalMemory failed %d", rc));
|
|---|
| 272 | return 0;
|
|---|
| 273 | }
|
|---|
| 274 | return (ULONG)thrdaddr;
|
|---|
| 275 | }
|
|---|
| 276 | //******************************************************************************
|
|---|
| 277 | //******************************************************************************
|
|---|
| 278 | char *OSLibStripPath(char *path)
|
|---|
| 279 | {
|
|---|
| 280 | /* @@@PH what does this function do ? Strip the path from a FQFN name ? */
|
|---|
| 281 | char *pszFilename;
|
|---|
| 282 |
|
|---|
| 283 | pszFilename = strrchr(path, '\\'); /* find rightmost slash */
|
|---|
| 284 | if (pszFilename != NULL)
|
|---|
| 285 | return (pszFilename++); /* return pointer to next character */
|
|---|
| 286 |
|
|---|
| 287 | pszFilename = strrchr(path, '/'); /* find rightmost slash */
|
|---|
| 288 | if (pszFilename != NULL)
|
|---|
| 289 | return (pszFilename++); /* return pointer to next character */
|
|---|
| 290 |
|
|---|
| 291 | return (path); /* default return value */
|
|---|
| 292 | }
|
|---|
| 293 | //******************************************************************************
|
|---|
| 294 | //******************************************************************************
|
|---|