| 1 | /* $Id: oslibmisc.cpp,v 1.1 1999-09-15 23:32:59 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 | DosQueryModuleName(hModule, CCHMAXPATH, modname); | 
|---|
| 62 | return(modname); | 
|---|
| 63 | } | 
|---|
| 64 | //****************************************************************************** | 
|---|
| 65 | /***************************************************************************** | 
|---|
| 66 | * Name      : ULONG OSLibiGetModuleHandleA | 
|---|
| 67 | * Purpose   : replacement for IBM Open32's GetModuleHandle | 
|---|
| 68 | * Parameters: LPCTSTR lpszModule | 
|---|
| 69 | * Variables : | 
|---|
| 70 | * Result    : HMODULE hModule or NULLHANDLE in case of error | 
|---|
| 71 | * Remark    : | 
|---|
| 72 | * Status    : REWRITTEN UNTESTED | 
|---|
| 73 | * | 
|---|
| 74 | * Author    : Patrick Haller [Sun, 1998/04/04 01:55] | 
|---|
| 75 | *****************************************************************************/ | 
|---|
| 76 |  | 
|---|
| 77 | ULONG OSLibiGetModuleHandleA(char * pszModule) | 
|---|
| 78 | { | 
|---|
| 79 | HMODULE hModule;                                          /* module handle */ | 
|---|
| 80 | APIRET  rc;                                              /* API returncode */ | 
|---|
| 81 | static HMODULE hModuleExe;                          /* "cached" hModuleExe */ | 
|---|
| 82 | PTIB pTIB;                              /* parameters for DosGetInfoBlocks */ | 
|---|
| 83 | PPIB pPIB; | 
|---|
| 84 |  | 
|---|
| 85 | dprintf(("KERNEL32:GetModuleHandle(%s)\n", | 
|---|
| 86 | pszModule)); | 
|---|
| 87 |  | 
|---|
| 88 | /* @@@PH 98/04/04 | 
|---|
| 89 |  | 
|---|
| 90 | this Open32 function is broken for pszModule == NULL | 
|---|
| 91 | return(GetModuleHandle(pszModule)); | 
|---|
| 92 |  | 
|---|
| 93 | Open32 always returns -1 here, however it should return the handle | 
|---|
| 94 | of the current process. MFC30 crashes. | 
|---|
| 95 |  | 
|---|
| 96 | SvL, me thinks for PELDR support, you'll have to rewrite | 
|---|
| 97 | this code anyway :) | 
|---|
| 98 |  | 
|---|
| 99 | */ | 
|---|
| 100 |  | 
|---|
| 101 | if (NULL == pszModule)              /* obtain handle to current executable */ | 
|---|
| 102 | { | 
|---|
| 103 | if (hModuleExe != NULLHANDLE)            /* do we have a cached handle ? */ | 
|---|
| 104 | return (hModuleExe); | 
|---|
| 105 |  | 
|---|
| 106 | rc = DosGetInfoBlocks(&pTIB,                      /* get the info blocks */ | 
|---|
| 107 | &pPIB); | 
|---|
| 108 | if (rc != NO_ERROR)                                  /* check for errors */ | 
|---|
| 109 | { | 
|---|
| 110 | SetLastError(rc);                                    /* set error code */ | 
|---|
| 111 | return (NULLHANDLE);                                 /* signal failure */ | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | hModuleExe = pPIB->pib_hmte;                        /* set cached module */ | 
|---|
| 115 | hModule = pPIB->pib_hmte;                       /* module table entry ID */ | 
|---|
| 116 | } | 
|---|
| 117 | else | 
|---|
| 118 | { | 
|---|
| 119 | rc = DosQueryModuleHandle(pszModule,              /* query module handle */ | 
|---|
| 120 | &hModule); | 
|---|
| 121 |  | 
|---|
| 122 | if (rc != NO_ERROR)                                  /* check for errors */ | 
|---|
| 123 | { | 
|---|
| 124 | SetLastError(rc);                                    /* set error code */ | 
|---|
| 125 | return (NULLHANDLE);                                 /* signal failure */ | 
|---|
| 126 | } | 
|---|
| 127 | } | 
|---|
| 128 |  | 
|---|
| 129 | return (hModule);                              /* return determined handle */ | 
|---|
| 130 | } | 
|---|
| 131 |  | 
|---|
| 132 |  | 
|---|
| 133 | ULONG OSLibQueryModuleHandle(char *modname) | 
|---|
| 134 | { | 
|---|
| 135 | HMODULE hModule; | 
|---|
| 136 | APIRET  rc; | 
|---|
| 137 |  | 
|---|
| 138 | rc = DosQueryModuleHandle(modname,              /* query module handle */ | 
|---|
| 139 | &hModule); | 
|---|
| 140 | if(rc) | 
|---|
| 141 | return(-1); | 
|---|
| 142 |  | 
|---|
| 143 | return(hModule); | 
|---|
| 144 | } | 
|---|
| 145 |  | 
|---|
| 146 | //SvL: only for RT_RCDATA! | 
|---|
| 147 | ULONG OSLibGetResourceSize(HMODULE hinstance, int id) | 
|---|
| 148 | { | 
|---|
| 149 | APIRET rc; | 
|---|
| 150 | ULONG  size; | 
|---|
| 151 |  | 
|---|
| 152 | rc = DosQueryResourceSize(hinstance, RT_RCDATA, id, &size); | 
|---|
| 153 | if(rc) { | 
|---|
| 154 | dprintf(("DosQueryResourceSize returned %d, %X id = %d\n", rc, hinstance, id)); | 
|---|
| 155 | return(0); | 
|---|
| 156 | } | 
|---|
| 157 | return(size); | 
|---|
| 158 | } | 
|---|
| 159 |  | 
|---|
| 160 | ULONG OSLibGetResource(HMODULE hinstance, int id, char *destbuf, int bufLength) | 
|---|
| 161 | { | 
|---|
| 162 | APIRET rc; | 
|---|
| 163 | char  *resdata; | 
|---|
| 164 | ULONG  size; | 
|---|
| 165 |  | 
|---|
| 166 | rc = DosQueryResourceSize(hinstance, RT_RCDATA, id, &size); | 
|---|
| 167 | if(rc) { | 
|---|
| 168 | dprintf(("OSLibGetResource: Can't get resource size of %d!!!\n", id)); | 
|---|
| 169 | return(FALSE); | 
|---|
| 170 | } | 
|---|
| 171 | rc = DosGetResource(hinstance, RT_RCDATA, id, (PPVOID)&resdata); | 
|---|
| 172 | if(rc) { | 
|---|
| 173 | dprintf(("OSLibGetResource: Can't find resource %d!!!\n", id)); | 
|---|
| 174 | return(FALSE); | 
|---|
| 175 | } | 
|---|
| 176 | dprintf(("OSLibGetResoure: bufLength %d, size %d, id %d", bufLength, size, id)); | 
|---|
| 177 | size = min(size, bufLength); | 
|---|
| 178 | memcpy(destbuf, resdata, size); | 
|---|
| 179 | DosFreeResource(resdata); | 
|---|
| 180 |  | 
|---|
| 181 | return(TRUE); | 
|---|
| 182 | } | 
|---|
| 183 |  | 
|---|
| 184 | void  OSLibWait(ULONG msec) | 
|---|
| 185 | { | 
|---|
| 186 | DosSleep(msec); | 
|---|
| 187 | } | 
|---|
| 188 |  | 
|---|
| 189 | //****************************************************************************** | 
|---|
| 190 | //Wrapper for Dos16AllocSeg | 
|---|
| 191 | //****************************************************************************** | 
|---|
| 192 | ULONG OSLibAllocSel(ULONG size, USHORT *selector) | 
|---|
| 193 | { | 
|---|
| 194 | return (Dos16AllocSeg(size, selector, SEG_NONSHARED) == 0); | 
|---|
| 195 | } | 
|---|
| 196 | //****************************************************************************** | 
|---|
| 197 | //Wrapper for Dos16FreeSeg | 
|---|
| 198 | //****************************************************************************** | 
|---|
| 199 | ULONG OSLibFreeSel(USHORT selector) | 
|---|
| 200 | { | 
|---|
| 201 | return (Dos16FreeSeg(selector) == 0); | 
|---|
| 202 | } | 
|---|
| 203 | //****************************************************************************** | 
|---|
| 204 | //Wrapper for Dos32SelToFlat | 
|---|
| 205 | //****************************************************************************** | 
|---|
| 206 | PVOID OSLibSelToFlat(USHORT selector) | 
|---|
| 207 | { | 
|---|
| 208 | return (PVOID)DosSelToFlat(selector << 16); | 
|---|
| 209 | } | 
|---|
| 210 | //****************************************************************************** | 
|---|
| 211 | //Get TIB data | 
|---|
| 212 | //****************************************************************************** | 
|---|
| 213 | ULONG OSLibGetTIB(int tiboff) | 
|---|
| 214 | { | 
|---|
| 215 | PTIB   ptib; | 
|---|
| 216 | PPIB   ppib; | 
|---|
| 217 | APIRET rc; | 
|---|
| 218 |  | 
|---|
| 219 | rc = DosGetInfoBlocks(&ptib, &ppib); | 
|---|
| 220 | if(rc) { | 
|---|
| 221 | return 0; | 
|---|
| 222 | } | 
|---|
| 223 | switch(tiboff) | 
|---|
| 224 | { | 
|---|
| 225 | case TIB_STACKTOP: | 
|---|
| 226 | return (ULONG)ptib->tib_pstack; | 
|---|
| 227 | case TIB_STACKLOW: | 
|---|
| 228 | return (ULONG)ptib->tib_pstacklimit; | 
|---|
| 229 | default: | 
|---|
| 230 | return 0; | 
|---|
| 231 | } | 
|---|
| 232 | } | 
|---|
| 233 | //****************************************************************************** | 
|---|
| 234 | //Get PIB data | 
|---|
| 235 | //****************************************************************************** | 
|---|
| 236 | ULONG OSLibGetPIB(int piboff) | 
|---|
| 237 | { | 
|---|
| 238 | PTIB   ptib; | 
|---|
| 239 | PPIB   ppib; | 
|---|
| 240 | APIRET rc; | 
|---|
| 241 |  | 
|---|
| 242 | rc = DosGetInfoBlocks(&ptib, &ppib); | 
|---|
| 243 | if(rc) { | 
|---|
| 244 | return 0; | 
|---|
| 245 | } | 
|---|
| 246 | switch(piboff) | 
|---|
| 247 | { | 
|---|
| 248 | case PIB_TASKHNDL: | 
|---|
| 249 | return ppib->pib_hmte; | 
|---|
| 250 | case PIB_TASKTYPE: | 
|---|
| 251 | if(ppib->pib_ultype == 3) { | 
|---|
| 252 | return TASKTYPE_PM; | 
|---|
| 253 | } | 
|---|
| 254 | else    return TASKTYPE_VIO; | 
|---|
| 255 | default: | 
|---|
| 256 | return 0; | 
|---|
| 257 | } | 
|---|
| 258 | } | 
|---|
| 259 | //****************************************************************************** | 
|---|
| 260 | //Allocate local thread memory | 
|---|
| 261 | //****************************************************************************** | 
|---|
| 262 | ULONG OSLibAllocThreadLocalMemory(int nrdwords) | 
|---|
| 263 | { | 
|---|
| 264 | APIRET rc; | 
|---|
| 265 | PULONG thrdaddr; | 
|---|
| 266 |  | 
|---|
| 267 | rc = DosAllocThreadLocalMemory(nrdwords, &thrdaddr); | 
|---|
| 268 | if(rc) { | 
|---|
| 269 | dprintf(("DosAllocThreadLocalMemory failed %d", rc)); | 
|---|
| 270 | return 0; | 
|---|
| 271 | } | 
|---|
| 272 | return (ULONG)thrdaddr; | 
|---|
| 273 | } | 
|---|
| 274 | //****************************************************************************** | 
|---|
| 275 | //****************************************************************************** | 
|---|
| 276 | char *OSLibStripPath(char *path) | 
|---|
| 277 | { | 
|---|
| 278 | /* @@@PH what does this function do ? Strip the path from a FQFN name ? */ | 
|---|
| 279 | char *pszFilename; | 
|---|
| 280 |  | 
|---|
| 281 | pszFilename = strrchr(path, '\\');                 /* find rightmost slash */ | 
|---|
| 282 | if (pszFilename != NULL) | 
|---|
| 283 | return (pszFilename++);              /* return pointer to next character */ | 
|---|
| 284 |  | 
|---|
| 285 | pszFilename = strrchr(path, '/');                  /* find rightmost slash */ | 
|---|
| 286 | if (pszFilename != NULL) | 
|---|
| 287 | return (pszFilename++);              /* return pointer to next character */ | 
|---|
| 288 |  | 
|---|
| 289 | return (path);                                     /* default return value */ | 
|---|
| 290 | } | 
|---|
| 291 | //****************************************************************************** | 
|---|
| 292 | //****************************************************************************** | 
|---|