| 1 | /* $Id: oslibmisc.cpp,v 1.13 2001-08-09 08:45:06 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 | //****************************************************************************** | 
|---|
| 32 | char *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 | //****************************************************************************** | 
|---|
| 43 | BOOL 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 |  | 
|---|
| 60 | ULONG 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 |  | 
|---|
| 114 | ULONG 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 |  | 
|---|
| 127 | void OSLibWait(ULONG msec) | 
|---|
| 128 | { | 
|---|
| 129 | DosSleep(msec); | 
|---|
| 130 | } | 
|---|
| 131 |  | 
|---|
| 132 | //****************************************************************************** | 
|---|
| 133 | //Wrapper for Dos16AllocSeg | 
|---|
| 134 | //****************************************************************************** | 
|---|
| 135 | ULONG OSLibAllocSel(ULONG size, USHORT *selector) | 
|---|
| 136 | { | 
|---|
| 137 | return (Dos16AllocSeg(size, selector, SEG_NONSHARED) == 0); | 
|---|
| 138 | } | 
|---|
| 139 | //****************************************************************************** | 
|---|
| 140 | //Wrapper for Dos16FreeSeg | 
|---|
| 141 | //****************************************************************************** | 
|---|
| 142 | ULONG OSLibFreeSel(USHORT selector) | 
|---|
| 143 | { | 
|---|
| 144 | return (Dos16FreeSeg(selector) == 0); | 
|---|
| 145 | } | 
|---|
| 146 | //****************************************************************************** | 
|---|
| 147 | //Wrapper for Dos32SelToFlat | 
|---|
| 148 | //****************************************************************************** | 
|---|
| 149 | PVOID OSLibSelToFlat(USHORT selector) | 
|---|
| 150 | { | 
|---|
| 151 | return (PVOID)DosSelToFlat(selector << 16); | 
|---|
| 152 | } | 
|---|
| 153 | //****************************************************************************** | 
|---|
| 154 | //Get TIB data | 
|---|
| 155 | //****************************************************************************** | 
|---|
| 156 | ULONG OSLibGetTIB(int tiboff) | 
|---|
| 157 | { | 
|---|
| 158 | PTIB   ptib; | 
|---|
| 159 | PPIB   ppib; | 
|---|
| 160 | APIRET rc; | 
|---|
| 161 |  | 
|---|
| 162 | rc = DosGetInfoBlocks(&ptib, &ppib); | 
|---|
| 163 | if(rc) { | 
|---|
| 164 | return 0; | 
|---|
| 165 | } | 
|---|
| 166 | switch(tiboff) | 
|---|
| 167 | { | 
|---|
| 168 | case TIB_STACKTOP: | 
|---|
| 169 | return (ULONG)ptib->tib_pstacklimit; | 
|---|
| 170 | case TIB_STACKLOW: | 
|---|
| 171 | return (ULONG)ptib->tib_pstack; | 
|---|
| 172 | default: | 
|---|
| 173 | return 0; | 
|---|
| 174 | } | 
|---|
| 175 | } | 
|---|
| 176 |  | 
|---|
| 177 | /** | 
|---|
| 178 | * Gets a PIB data. | 
|---|
| 179 | * @returns     Requested PIB data. | 
|---|
| 180 | *              0 may indicate error or that the PIB data you requested actually is 0. | 
|---|
| 181 | * @param       iPIB    PIB data index. (one of the PIB_* defines in oslibmisc.h) | 
|---|
| 182 | * @author | 
|---|
| 183 | * @remark      Spooky error handling. | 
|---|
| 184 | */ | 
|---|
| 185 | ULONG OSLibGetPIB(int iPIB) | 
|---|
| 186 | { | 
|---|
| 187 | PTIB   ptib; | 
|---|
| 188 | PPIB   ppib; | 
|---|
| 189 | APIRET rc; | 
|---|
| 190 |  | 
|---|
| 191 | rc = DosGetInfoBlocks(&ptib, &ppib); | 
|---|
| 192 | if (rc) | 
|---|
| 193 | { | 
|---|
| 194 | dprintf(("KERNEL32: OSLibGetPIB(%d): DosGetInfoBlocks failed with rc=%d\n", iPIB, rc)); | 
|---|
| 195 | return 0; | 
|---|
| 196 | } | 
|---|
| 197 |  | 
|---|
| 198 | switch(iPIB) | 
|---|
| 199 | { | 
|---|
| 200 | case PIB_TASKHNDL: | 
|---|
| 201 | return ppib->pib_hmte; | 
|---|
| 202 |  | 
|---|
| 203 | case PIB_TASKTYPE: | 
|---|
| 204 | return (ppib->pib_ultype == 3) ? TASKTYPE_PM : TASKTYPE_VIO; | 
|---|
| 205 |  | 
|---|
| 206 | case PIB_PCHCMD: | 
|---|
| 207 | return (ULONG)ppib->pib_pchcmd; | 
|---|
| 208 |  | 
|---|
| 209 | default: | 
|---|
| 210 | dprintf(("KERNEL32: OSLibGetPIB(%d): Invalid PIB data index\n.", iPIB)); | 
|---|
| 211 | DebugInt3(); | 
|---|
| 212 | return 0; | 
|---|
| 213 | } | 
|---|
| 214 | } | 
|---|
| 215 | //****************************************************************************** | 
|---|
| 216 | //Allocate local thread memory | 
|---|
| 217 | //****************************************************************************** | 
|---|
| 218 | ULONG OSLibAllocThreadLocalMemory(int nrdwords) | 
|---|
| 219 | { | 
|---|
| 220 | APIRET rc; | 
|---|
| 221 | PULONG thrdaddr; | 
|---|
| 222 |  | 
|---|
| 223 | rc = DosAllocThreadLocalMemory(nrdwords, &thrdaddr); | 
|---|
| 224 | if(rc) { | 
|---|
| 225 | dprintf(("DosAllocThreadLocalMemory failed %d", rc)); | 
|---|
| 226 | return 0; | 
|---|
| 227 | } | 
|---|
| 228 | return (ULONG)thrdaddr; | 
|---|
| 229 | } | 
|---|
| 230 | //****************************************************************************** | 
|---|
| 231 | //****************************************************************************** | 
|---|
| 232 | char *OSLibStripPath(char *path) | 
|---|
| 233 | { | 
|---|
| 234 | /* @@@PH what does this function do ? Strip the path from a FQFN name ? */ | 
|---|
| 235 | char *pszFilename; | 
|---|
| 236 | char *pszFilename1; | 
|---|
| 237 |  | 
|---|
| 238 | pszFilename  = strrchr(path, '\\');                 /* find rightmost backslash */ | 
|---|
| 239 | pszFilename1 = strrchr(path, '/');                  /* find rightmost slash */ | 
|---|
| 240 | if(pszFilename > pszFilename1 && pszFilename != NULL) | 
|---|
| 241 | return (++pszFilename);              /* return pointer to next character */ | 
|---|
| 242 |  | 
|---|
| 243 | if (pszFilename1 != NULL) | 
|---|
| 244 | return (++pszFilename1);              /* return pointer to next character */ | 
|---|
| 245 |  | 
|---|
| 246 | return (path);                                     /* default return value */ | 
|---|
| 247 | } | 
|---|
| 248 | //****************************************************************************** | 
|---|
| 249 | //****************************************************************************** | 
|---|
| 250 | ULONG OSLibWinInitialize() | 
|---|
| 251 | { | 
|---|
| 252 | return (ULONG)WinInitialize(0); | 
|---|
| 253 | } | 
|---|
| 254 | //****************************************************************************** | 
|---|
| 255 | //****************************************************************************** | 
|---|
| 256 | ULONG OSLibWinQueryMsgQueue(ULONG hab) | 
|---|
| 257 | { | 
|---|
| 258 | ULONG hmq; | 
|---|
| 259 |  | 
|---|
| 260 | hmq = (ULONG)WinCreateMsgQueue((HAB)hab, 0); | 
|---|
| 261 | if(!hmq) { | 
|---|
| 262 | PTIB ptib; | 
|---|
| 263 | PPIB ppib; | 
|---|
| 264 |  | 
|---|
| 265 | DosGetInfoBlocks(&ptib, &ppib); | 
|---|
| 266 |  | 
|---|
| 267 | hmq = WinQueueFromID(hab, ppib->pib_ulpid, ptib->tib_ptib2->tib2_ultid); | 
|---|
| 268 | } | 
|---|
| 269 | return hmq; | 
|---|
| 270 | } | 
|---|
| 271 | //****************************************************************************** | 
|---|
| 272 | //****************************************************************************** | 
|---|
| 273 | ULONG OSLibQueryCountry() | 
|---|
| 274 | { | 
|---|
| 275 | COUNTRYCODE  Country    = {0};   /* Country code info (0 = current country) */ | 
|---|
| 276 | COUNTRYINFO  CtryInfo   = {0};   /* Buffer for country-specific information */ | 
|---|
| 277 | ULONG        ulInfoLen  = 0; | 
|---|
| 278 | APIRET       rc         = NO_ERROR;  /* Return code                         */ | 
|---|
| 279 |  | 
|---|
| 280 | rc = DosQueryCtryInfo(sizeof(CtryInfo), &Country, | 
|---|
| 281 | &CtryInfo, &ulInfoLen); | 
|---|
| 282 |  | 
|---|
| 283 | if (rc != NO_ERROR) { | 
|---|
| 284 | return -1; | 
|---|
| 285 | } | 
|---|
| 286 | return CtryInfo.country; | 
|---|
| 287 | } | 
|---|
| 288 | //****************************************************************************** | 
|---|
| 289 | //****************************************************************************** | 
|---|