[2826] | 1 | /* $Id: kLdrHlp.c 2830 2006-10-23 20:53:11Z bird $ */
|
---|
[2825] | 2 | /** @file
|
---|
| 3 | *
|
---|
| 4 | * kLdr - The Dynamic Loader, Helper Functions.
|
---|
| 5 | *
|
---|
| 6 | * Copyright (c) 2006 knut st. osmundsen <bird-kbuild-src@anduin.net>
|
---|
| 7 | *
|
---|
| 8 | *
|
---|
| 9 | * This file is part of kLdr.
|
---|
| 10 | *
|
---|
| 11 | * kLdr is free software; you can redistribute it and/or modify
|
---|
| 12 | * it under the terms of the GNU General Public License as published by
|
---|
| 13 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 14 | * (at your option) any later version.
|
---|
| 15 | *
|
---|
| 16 | * kLdr is distributed in the hope that it will be useful,
|
---|
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 19 | * GNU General Public License for more details.
|
---|
| 20 | *
|
---|
| 21 | * You should have received a copy of the GNU General Public License
|
---|
| 22 | * along with kLdr; if not, write to the Free Software
|
---|
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
| 24 | *
|
---|
| 25 | */
|
---|
| 26 |
|
---|
| 27 |
|
---|
| 28 | /*******************************************************************************
|
---|
| 29 | * Header Files *
|
---|
| 30 | *******************************************************************************/
|
---|
| 31 | #ifdef __OS2__
|
---|
| 32 | # define INCL_BASE
|
---|
| 33 | # define INCL_ERRORS
|
---|
| 34 | # include <os2.h>
|
---|
| 35 | #elif defined(__WIN__)
|
---|
| 36 | # include <Windows.h>
|
---|
| 37 | #else
|
---|
| 38 | # error "port me"
|
---|
| 39 | #endif
|
---|
| 40 |
|
---|
| 41 | #include <kLdr.h>
|
---|
| 42 | #include "kLdrHlp.h"
|
---|
| 43 |
|
---|
| 44 |
|
---|
[2830] | 45 | /*******************************************************************************
|
---|
| 46 | * Global Variables *
|
---|
| 47 | *******************************************************************************/
|
---|
| 48 | #ifdef __OS2__
|
---|
| 49 | /** The loader sempahore. */
|
---|
| 50 | static HMTX g_hmtx;
|
---|
| 51 | /** The base of the stub object.
|
---|
| 52 | * The OS/2 exe stub consists of a single data object. When allocating memory
|
---|
| 53 | * for an executable, we'll have to reuse this. */
|
---|
| 54 | static void *g_pvStub = NULL;
|
---|
| 55 | /** The size of the stub object - 0 if no stub. */
|
---|
| 56 | static size_t g_cbStub = 0;
|
---|
| 57 |
|
---|
| 58 | #elif defined(__WIN__)
|
---|
| 59 | /** The loader sempahore. */
|
---|
| 60 | static CRITICAL_SECTION g_CritSect;
|
---|
| 61 | /** The system info. */
|
---|
| 62 | static SYSTEM_INFO g_SystemInfo;
|
---|
| 63 | #else
|
---|
| 64 | # error "port me"
|
---|
| 65 | #endif
|
---|
| 66 |
|
---|
| 67 |
|
---|
[2825] | 68 | /**
|
---|
[2830] | 69 | * Initializes the loader semaphore.
|
---|
| 70 | *
|
---|
| 71 | * @returns 0 on success, non-zero OS status code on failure.
|
---|
| 72 | */
|
---|
| 73 | int kldrHlpSemInit(void)
|
---|
| 74 | {
|
---|
| 75 | #ifdef __OS2__
|
---|
| 76 | APIRET rc;
|
---|
| 77 | g_hmtx = NULLHANDLE;
|
---|
| 78 | rc = DosCreateMutexSem(NULL, &g_hmtx, 0, FALSE);
|
---|
| 79 | if (rc)
|
---|
| 80 | return rc;
|
---|
| 81 |
|
---|
| 82 | #elif defined(__WIN__)
|
---|
| 83 | InitializeCriticalSection(&g_CritSect);
|
---|
| 84 |
|
---|
| 85 | #else
|
---|
| 86 | # error "port me"
|
---|
| 87 | #endif
|
---|
| 88 | return 0;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 |
|
---|
| 92 | /**
|
---|
| 93 | * Terminates the loader semaphore.
|
---|
| 94 | */
|
---|
| 95 | void kldrHlpSemTerm(void)
|
---|
| 96 | {
|
---|
| 97 | #ifdef __OS2__
|
---|
| 98 | APIRET rc;
|
---|
| 99 | HMTX hmtx = g_hmtx;
|
---|
| 100 | g_hmtx = NULLHANDLE;
|
---|
| 101 | rc = DosCloseMutexSem(hmtx);
|
---|
| 102 | if (rc)
|
---|
| 103 | return rc;
|
---|
| 104 |
|
---|
| 105 | #elif defined(__WIN__)
|
---|
| 106 | DeleteCriticalSection(&g_CritSect);
|
---|
| 107 |
|
---|
| 108 | #else
|
---|
| 109 | # error "port me"
|
---|
| 110 | #endif
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 |
|
---|
| 114 | /**
|
---|
| 115 | * Requests the loader sempahore ownership.
|
---|
| 116 | * This can be done recursivly.
|
---|
| 117 | *
|
---|
| 118 | * @returns 0 on success, non-zero OS status code on failure.
|
---|
| 119 | */
|
---|
| 120 | int kldrHlpSemRequest(void)
|
---|
| 121 | {
|
---|
| 122 | #ifdef __OS2__
|
---|
| 123 | APIRET rc = DosRequestMutexSem(g_hmtx, 5000);
|
---|
| 124 | if (rc == ERROR_TIMEOUT || rc == ERROR_SEM_TIMEOUT || rc == ERROR_INTERRUPT)
|
---|
| 125 | {
|
---|
| 126 | unsigned i = 0;
|
---|
| 127 | do
|
---|
| 128 | {
|
---|
| 129 | /** @todo check for deadlocks etc. */
|
---|
| 130 | rc = DosRequestMutexSem(g_hmtx, 1000);
|
---|
| 131 | } while ( ( rc == ERROR_TIMEOUT
|
---|
| 132 | || rc == ERROR_SEM_TIMEOUT
|
---|
| 133 | || rc == ERROR_INTERRUPT)
|
---|
| 134 | && i++ < 120);
|
---|
| 135 | }
|
---|
| 136 | return rc;
|
---|
| 137 |
|
---|
| 138 | #elif defined(__WIN__)
|
---|
| 139 | EnterCriticalSection(&g_CritSect);
|
---|
| 140 | return 0;
|
---|
| 141 |
|
---|
| 142 | #else
|
---|
| 143 | # error "port me"
|
---|
| 144 | #endif
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 |
|
---|
| 148 | /**
|
---|
| 149 | * Releases the loader semaphore ownership.
|
---|
| 150 | * The caller is responsible for making sure it's the semaphore owner!
|
---|
| 151 | */
|
---|
| 152 | void kldrHlpSemRelease(void)
|
---|
| 153 | {
|
---|
| 154 | #ifdef __OS2__
|
---|
| 155 | APIRET rc = DosReleaseMutexSem(g_hmtx);
|
---|
| 156 | kldrHlpAssert(!rc); (void)rc;
|
---|
| 157 |
|
---|
| 158 | #elif defined(__WIN__)
|
---|
| 159 | LeaveCriticalSection(&g_CritSect);
|
---|
| 160 |
|
---|
| 161 | #else
|
---|
| 162 | # error "port me"
|
---|
| 163 | #endif
|
---|
| 164 |
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | #ifdef __OS2__
|
---|
| 168 | static ULONG kldrHlpPageProtToNative(KLDRPROT enmProt)
|
---|
| 169 | {
|
---|
| 170 | switch (enmProt)
|
---|
| 171 | {
|
---|
| 172 | case KLDRPROT_NOACCESS: return PAG_EXECUTE | PAG_READ | PAG_WRITE;
|
---|
| 173 | case KLDRPROT_READONLY: return PAG_COMMIT | PAG_READ;
|
---|
| 174 | case KLDRPROT_READWRITE: return PAG_COMMIT | PAG_READ | PAG_WRITE;
|
---|
| 175 | case KLDRPROT_EXECUTE: return PAG_COMMIT | PAG_EXECUTE;
|
---|
| 176 | case KLDRPROT_EXECUTE_READ: return PAG_COMMIT | PAG_EXECUTE | PAG_READ;
|
---|
| 177 | case KLDRPROT_EXECUTE_READWRITE: return PAG_COMMIT | PAG_EXECUTE | PAG_READ | PAG_WRITE;
|
---|
| 178 | default:
|
---|
| 179 | kldrHlpAssert(0);
|
---|
| 180 | return ~0U;
|
---|
| 181 | }
|
---|
| 182 | }
|
---|
| 183 | #elif defined(__WIN__)
|
---|
| 184 | static DWORD kldrHlpPageProtToNative(KLDRPROT enmProt)
|
---|
| 185 | {
|
---|
| 186 | switch (enmProt)
|
---|
| 187 | {
|
---|
| 188 | case KLDRPROT_NOACCESS: return PAGE_NOACCESS;
|
---|
| 189 | case KLDRPROT_READONLY: return PAGE_READONLY;
|
---|
| 190 | case KLDRPROT_READWRITE: return PAGE_READWRITE;
|
---|
| 191 | case KLDRPROT_EXECUTE: return PAGE_EXECUTE;
|
---|
| 192 | case KLDRPROT_EXECUTE_READ: return PAGE_EXECUTE_READ;
|
---|
| 193 | case KLDRPROT_EXECUTE_READWRITE: return PAGE_EXECUTE_READWRITE;
|
---|
| 194 | default:
|
---|
| 195 | kldrHlpAssert(0);
|
---|
| 196 | return ~0U;
|
---|
| 197 | }
|
---|
| 198 | }
|
---|
| 199 | #endif
|
---|
| 200 |
|
---|
| 201 |
|
---|
| 202 | /**
|
---|
| 203 | * Allocate a chunk of memory with page granularity.
|
---|
| 204 | *
|
---|
| 205 | * @returns 0 on success, non-zero OS status code on failure.
|
---|
| 206 | * @param ppv Where to store the address of the allocated memory.
|
---|
| 207 | * If fFixed is set, *ppv will on entry contain the desired address (page aligned).
|
---|
| 208 | * @param cb Number of bytes. Page aligned.
|
---|
| 209 | * @param enmProt The new protection. Copy-on-write is invalid.
|
---|
| 210 | */
|
---|
| 211 | int kldrHlpPageAlloc(void **ppv, size_t cb, KLDRPROT enmProt, unsigned fFixed)
|
---|
| 212 | {
|
---|
| 213 | #ifdef __OS2__
|
---|
| 214 | APIRET rc;
|
---|
| 215 | ULONG fFlags = kldrHlpPageProtToNative(enmProt);;
|
---|
| 216 |
|
---|
| 217 | if (!fFixed)
|
---|
| 218 | {
|
---|
| 219 | /* simple */
|
---|
| 220 | rc = DosAllocMem(ppv, cb, fFlags | OBJ_ANY);
|
---|
| 221 | if (rc == ERROR_INVALID_PARAMETER)
|
---|
| 222 | rc = DosAllocMem(ppv, cb, fFlags);
|
---|
| 223 | }
|
---|
| 224 | else
|
---|
| 225 | {
|
---|
| 226 | /* not so simple. */
|
---|
| 227 | /** @todo I've got code for this in libc somewhere. */
|
---|
| 228 | }
|
---|
| 229 | if (!rc)
|
---|
| 230 | return 0;
|
---|
| 231 | kldrHlpAssert(0);
|
---|
| 232 | return rc;
|
---|
| 233 |
|
---|
| 234 | #elif defined(__WIN__)
|
---|
| 235 | int rc;
|
---|
| 236 | DWORD fProt = kldrHlpPageProtToNative(enmProt);
|
---|
| 237 |
|
---|
| 238 | if (!g_SystemInfo.dwPageSize)
|
---|
| 239 | GetSystemInfo(&g_SystemInfo);
|
---|
| 240 |
|
---|
| 241 | *ppv = VirtualAlloc(fFixed ? *ppv : NULL, cb, MEM_COMMIT, fProt);
|
---|
| 242 | if (*ppv == NULL)
|
---|
| 243 | {
|
---|
| 244 | rc = GetLastError();
|
---|
| 245 | kldrHlpAssert(0);
|
---|
| 246 | }
|
---|
| 247 | return rc;
|
---|
| 248 |
|
---|
| 249 | #else
|
---|
| 250 | # error "port me"
|
---|
| 251 | #endif
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 |
|
---|
| 255 | /**
|
---|
| 256 | * Change the protection of one or more pages in an allocation.
|
---|
| 257 | *
|
---|
| 258 | * (This will of course only work correctly on memory allocated by kldrHlpPageAlloc().)
|
---|
| 259 | *
|
---|
| 260 | * @returns 0 on success, non-zero OS status code on failure.
|
---|
| 261 | * @param pv First page. Page aligned.
|
---|
| 262 | * @param cb Number of bytes. Page aligned.
|
---|
| 263 | * @param enmProt The new protection. Copy-on-write is invalid.
|
---|
| 264 | */
|
---|
| 265 | int kldrHlpPageProtect(void *pv, size_t cb, KLDRPROT enmProt)
|
---|
| 266 | {
|
---|
| 267 | #ifdef __OS2__
|
---|
| 268 | APIRET rc;
|
---|
| 269 | uintptr_t offStub;
|
---|
| 270 | ULONG fFlags = kldrHlpPageProtToNative(enmProt);;
|
---|
| 271 |
|
---|
| 272 | /*
|
---|
| 273 | * The non-stub pages.
|
---|
| 274 | */
|
---|
| 275 | rc = DosSetMem(pv, cb, fFlags);
|
---|
| 276 | if (rc && fFlags != PAG_DECOMMIT)
|
---|
| 277 | rc = DosSetMem(pv, cb, fFlags | PAG_COMMIT);
|
---|
| 278 | if (rc)
|
---|
| 279 | {
|
---|
| 280 | /* Try page by page. */
|
---|
| 281 | while (cb > 0)
|
---|
| 282 | {
|
---|
| 283 | rc = DosSetMem(pv, 0x1000, fFlags);
|
---|
| 284 | if (rc && fFlags != PAG_DECOMMIT)
|
---|
| 285 | rc = DosSetMem(pv, 0x1000, fFlags | PAG_COMMIT);
|
---|
| 286 | if (rc)
|
---|
| 287 | return rc;
|
---|
| 288 | pv = (void *)((uintptr_t)pv + 0x1000);
|
---|
| 289 | cb -= 0x1000;
|
---|
| 290 | }
|
---|
| 291 | }
|
---|
| 292 | kldrHlpAssert(!rc);
|
---|
| 293 | return rc;
|
---|
| 294 |
|
---|
| 295 | #elif defined(__WIN__)
|
---|
| 296 | DWORD fOldProt = 0;
|
---|
| 297 | DWORD fProt = kldrHlpPageProtToNative(enmProt);
|
---|
| 298 | int rc = 0;
|
---|
| 299 |
|
---|
| 300 | if (!VirtualProtect(pv, cb, fProt, &fOldProt))
|
---|
| 301 | {
|
---|
| 302 | rc = GetLastError();
|
---|
| 303 | kldrHlpAssert(0);
|
---|
| 304 | }
|
---|
| 305 | return rc;
|
---|
| 306 | #else
|
---|
| 307 | # error "port me"
|
---|
| 308 | #endif
|
---|
| 309 | }
|
---|
| 310 |
|
---|
| 311 |
|
---|
| 312 | /**
|
---|
| 313 | * Free memory allocated by kldrHlpPageAlloc().
|
---|
| 314 | *
|
---|
| 315 | * @returns 0 on success, non-zero OS status code on failure.
|
---|
| 316 | * @param pv The address returned by kldrHlpPageAlloc().
|
---|
| 317 | * @param cb The byte count requested from kldrHlpPageAlloc().
|
---|
| 318 | */
|
---|
| 319 | int kldrHlpPageFree(void *pv, size_t cb)
|
---|
| 320 | {
|
---|
| 321 | #ifdef __OS2__
|
---|
| 322 | APIRET rc;
|
---|
| 323 |
|
---|
| 324 | /*
|
---|
| 325 | * Deal with any portion overlapping with the stub.
|
---|
| 326 | */
|
---|
| 327 | uintptr_t offStub = (uintptr_t)pv - (uintptr_t)g_pvStub;
|
---|
| 328 | if (offStub < g_cbStub)
|
---|
| 329 | {
|
---|
| 330 | /* decommit the pages in the stub. */
|
---|
| 331 | size_t cbSub = KLDR_MIN(g_cbStub - offStub, cb);
|
---|
| 332 | rc = DosSetMem(pv, cbStub, PAG_DECOMMIT);
|
---|
| 333 | if (rc)
|
---|
| 334 | {
|
---|
| 335 | /* Page by page, ignoring errors after the first success. */
|
---|
| 336 | while (cbSub > 0)
|
---|
| 337 | {
|
---|
| 338 | if (!DosSetMem(pv, 0x1000, PAG_DECOMMIT))
|
---|
| 339 | rc = 0;
|
---|
| 340 | pv = (void *)((uintptr_t)pv + 0x1000);
|
---|
| 341 | cbSub -= 0x1000;
|
---|
| 342 | cb -= 0x1000;
|
---|
| 343 | }
|
---|
| 344 | if (rc)
|
---|
| 345 | {
|
---|
| 346 | kldrHlpAssert(!rc);
|
---|
| 347 | return rc;
|
---|
| 348 | }
|
---|
| 349 | }
|
---|
| 350 | else
|
---|
| 351 | {
|
---|
| 352 | cb -= cbSub;
|
---|
| 353 | if (!cb)
|
---|
| 354 | return 0;
|
---|
| 355 | pv = (void *)((uintptr_t)pv + cbSub);
|
---|
| 356 | }
|
---|
| 357 | }
|
---|
| 358 |
|
---|
| 359 | /*
|
---|
| 360 | * Free the object.
|
---|
| 361 | */
|
---|
| 362 | rc = DosFreeMem(pv);
|
---|
| 363 | kldrHlpAssert(!rc);
|
---|
| 364 | return rc;
|
---|
| 365 |
|
---|
| 366 | #elif defined(__WIN__)
|
---|
| 367 | /*
|
---|
| 368 | * Free the object.
|
---|
| 369 | */
|
---|
| 370 | int rc = 0;
|
---|
| 371 | if (!VirtualFree(pv, 0 /*cb*/, MEM_RELEASE))
|
---|
| 372 | {
|
---|
| 373 | rc = GetLastError();
|
---|
| 374 | kldrHlpAssert(0);
|
---|
| 375 | }
|
---|
| 376 | return rc;
|
---|
| 377 |
|
---|
| 378 | #else
|
---|
| 379 | # error "port me"
|
---|
| 380 | #endif
|
---|
| 381 | }
|
---|
| 382 |
|
---|
| 383 |
|
---|
| 384 | /**
|
---|
[2825] | 385 | * Get an environment variable.
|
---|
| 386 | *
|
---|
| 387 | * @returns 0 on success, on failure an OS specific status code is returned.
|
---|
| 388 | * @param pszVar The variable name.
|
---|
| 389 | * @param pszVal Where to store the value. NULL is allowed if *pcchVal is 0.
|
---|
| 390 | * @param pcchVal On input the size of the buffer pointed to by pszVal.
|
---|
| 391 | * On output this contains the string length on success, while on
|
---|
| 392 | * failure (including buffer overflow) the require buffer size.
|
---|
| 393 | * If the variable wasn't found, it's set to 0.
|
---|
| 394 | */
|
---|
| 395 | int kldrHlpGetEnv(const char *pszVar, char *pszVal, size_t *pcchVal)
|
---|
| 396 | {
|
---|
| 397 | #ifdef __OS2__
|
---|
| 398 | PSZ pszValue = NULL;
|
---|
| 399 | int rc = DosScanEnv((PCSZ)pszVar, &pszValue);
|
---|
| 400 | if (!rc)
|
---|
| 401 | {
|
---|
[2828] | 402 | size_t cch = kLdrHlpStrLen(pszValue);
|
---|
[2825] | 403 | if (pszVal)
|
---|
| 404 | {
|
---|
| 405 | if (*pcchVal > cch)
|
---|
| 406 | {
|
---|
[2828] | 407 | kLdrHlpMemCopy(pszVal, pszValue, cch + 1);
|
---|
[2825] | 408 | *pcchVal = cch;
|
---|
| 409 | }
|
---|
| 410 | else if (*pcchVal)
|
---|
| 411 | {
|
---|
[2828] | 412 | kLdrHlpMemCopy(pszVal, pszValue, *pcchVal);
|
---|
[2825] | 413 | pszVal[*pcchVal - 1] = '\0';
|
---|
| 414 | rc = ERROR_BUFFER_OVERFLOW;
|
---|
| 415 | *pcchVal = cch + 1;
|
---|
| 416 | }
|
---|
| 417 | }
|
---|
| 418 | else
|
---|
| 419 | {
|
---|
| 420 | rc = ERROR_BUFFER_OVERFLOW;
|
---|
| 421 | *pcchVal = cch + 1;
|
---|
| 422 | }
|
---|
| 423 | }
|
---|
| 424 | else
|
---|
| 425 | {
|
---|
| 426 | if (pszVal)
|
---|
| 427 | *pszVal = '\0';
|
---|
| 428 | *pcchVal = 0;
|
---|
| 429 | }
|
---|
| 430 |
|
---|
| 431 | return rc;
|
---|
| 432 |
|
---|
| 433 | #elif defined(__WIN__)
|
---|
| 434 | DWORD cch;
|
---|
| 435 | SetLastError(0);
|
---|
| 436 | cch = GetEnvironmentVariable(pszVar, pszVal, *pcchVal);
|
---|
| 437 | if (cch)
|
---|
| 438 | {
|
---|
| 439 | *pcchVal = cch;
|
---|
| 440 | return 0;
|
---|
| 441 | }
|
---|
| 442 | if (!GetLastError() == ERROR_ENVVAR_NOT_FOUND)
|
---|
| 443 | {
|
---|
| 444 | *pcchVal = 0;
|
---|
| 445 | return ERROR_ENVVAR_NOT_FOUND;
|
---|
| 446 | }
|
---|
| 447 | *pcchVal = cch;
|
---|
| 448 | return ERROR_BUFFER_OVERFLOW;
|
---|
| 449 |
|
---|
| 450 | #else
|
---|
| 451 | # error "Port me"
|
---|
| 452 | #endif
|
---|
| 453 | }
|
---|
| 454 |
|
---|
| 455 |
|
---|
| 456 | /**
|
---|
| 457 | * Terminate the process.
|
---|
| 458 | *
|
---|
| 459 | * @param rc The exit status.
|
---|
| 460 | */
|
---|
| 461 | void kldrHlpExit(int rc)
|
---|
| 462 | {
|
---|
| 463 | for (;;)
|
---|
| 464 | {
|
---|
| 465 | #ifdef __OS2__
|
---|
| 466 | DosExit(EXIT_PROCESS, rc);
|
---|
| 467 |
|
---|
| 468 | #elif defined(__WIN__)
|
---|
| 469 | TerminateProcess(GetCurrentProcess(), rc);
|
---|
| 470 |
|
---|
| 471 | #else
|
---|
| 472 | # error "Port me"
|
---|
| 473 | #endif
|
---|
| 474 | kldrHlpAssert(!"Impossible");
|
---|
| 475 | }
|
---|
| 476 | }
|
---|
| 477 |
|
---|
[2830] | 478 |
|
---|
| 479 | /**
|
---|
| 480 | * Sleep for a number of milliseconds.
|
---|
| 481 | * @param cMillies Number of milliseconds to sleep.
|
---|
| 482 | */
|
---|
| 483 | void kldrHlpSleep(unsigned cMillies)
|
---|
| 484 | {
|
---|
| 485 | #ifdef __OS2__
|
---|
| 486 | DosSleep(cMillies);
|
---|
| 487 | #elif defined(__WIN__)
|
---|
| 488 | Sleep(cMillies);
|
---|
| 489 | #else
|
---|
| 490 | usleep(cMillies * 1000);
|
---|
| 491 | #endif
|
---|
| 492 | }
|
---|
| 493 |
|
---|
| 494 |
|
---|
[2825] | 495 | /** Internal worker for kldrHlpAssertMsg. */
|
---|
| 496 | static void int2dec(char *pszLine, unsigned iLine)
|
---|
| 497 | {
|
---|
| 498 | do
|
---|
| 499 | {
|
---|
| 500 | *pszLine = (iLine % 10) + '0';
|
---|
| 501 | iLine /= 10;
|
---|
| 502 | } while (iLine);
|
---|
| 503 | *pszLine++ = '\0';
|
---|
| 504 | }
|
---|
| 505 |
|
---|
| 506 |
|
---|
| 507 | /**
|
---|
| 508 | * Internal worker for the kLdrHlpAssert() macro.
|
---|
| 509 | *
|
---|
| 510 | * @param pszExpr The assert expression.
|
---|
| 511 | * @param pszFile The filename.
|
---|
| 512 | * @param iLine The line number.
|
---|
| 513 | * @param pszFunction The function name.
|
---|
| 514 | */
|
---|
| 515 | void kldrHlpAssertMsg(const char *pszExpr, const char *pszFile, unsigned iLine, const char *pszFunction)
|
---|
| 516 | {
|
---|
| 517 | #ifdef __OS2__
|
---|
| 518 | static const char s_szMsg1[] = "\r\n!!!kLdr Assertion Failed!!!\n\rExpression: ";
|
---|
| 519 | static const char s_szMsg2[] = "\r\nAt: ";
|
---|
| 520 | static const char s_szMsg3[] = "\r\n";
|
---|
| 521 | ULONG cbWritten;
|
---|
| 522 | char szLine[16];
|
---|
| 523 |
|
---|
| 524 | DosWrite((HFILE)2, s_szMsg1, sizeof(s_szMsg1) - 1, &cbWritten);
|
---|
[2828] | 525 | DosWrite((HFILE)2, pszExpr, kLdrHlpStrLen(pszExpr), &cbWritten);
|
---|
[2825] | 526 | DosWrite((HFILE)2, s_szMsg2, sizeof(s_szMsg2) - 1, &cbWritten);
|
---|
[2828] | 527 | DosWrite((HFILE)2, pszFile, kLdrHlpStrLen(pszFile), &cbWritten);
|
---|
[2825] | 528 | DosWrite((HFILE)2, "(", sizeof("(") - 1, &cbWritten);
|
---|
| 529 | int2dec(szLine, iLine);
|
---|
[2828] | 530 | DosWrite((HFILE)2, szLine, kLdrHlpStrLen(szLine), &cbWritten);
|
---|
[2825] | 531 | DosWrite((HFILE)2, ") ", sizeof(") ") - 1, &cbWritten);
|
---|
[2828] | 532 | DosWrite((HFILE)2, pszFunction, kLdrHlpStrLen(pszFunction), &cbWritten);
|
---|
[2825] | 533 | DosWrite((HFILE)2, s_szMsg3, sizeof(s_szMsg3) - 1, &cbWritten);
|
---|
| 534 |
|
---|
| 535 | #elif defined(__WIN__)
|
---|
| 536 | static const char s_szMsg1[] = "\r\n!!!kLdr Assertion Failed!!!\n\rExpression: ";
|
---|
| 537 | static const char s_szMsg2[] = "\r\nAt: ";
|
---|
| 538 | static const char s_szMsg3[] = "\r\n";
|
---|
| 539 | DWORD cbWritten;
|
---|
| 540 | char szLine[16];
|
---|
| 541 | const HANDLE hStdErr = (HANDLE)STD_ERROR_HANDLE;
|
---|
| 542 |
|
---|
| 543 | WriteFile(hStdErr, s_szMsg1, sizeof(s_szMsg1) - 1, &cbWritten, NULL);
|
---|
[2828] | 544 | WriteFile(hStdErr, pszExpr, kLdrHlpStrLen(pszExpr), &cbWritten, NULL);
|
---|
[2825] | 545 | WriteFile(hStdErr, s_szMsg2, sizeof(s_szMsg2) - 1, &cbWritten, NULL);
|
---|
[2828] | 546 | WriteFile(hStdErr, pszFile, kLdrHlpStrLen(pszFile), &cbWritten, NULL);
|
---|
[2825] | 547 | WriteFile(hStdErr, "(", sizeof("(") - 1, &cbWritten, NULL);
|
---|
| 548 | int2dec(szLine, iLine);
|
---|
[2828] | 549 | WriteFile(hStdErr, szLine, kLdrHlpStrLen(szLine), &cbWritten, NULL);
|
---|
[2825] | 550 | WriteFile(hStdErr, ") ", sizeof(") ") - 1, &cbWritten, NULL);
|
---|
[2828] | 551 | WriteFile(hStdErr, pszFunction, kLdrHlpStrLen(pszFunction), &cbWritten, NULL);
|
---|
[2825] | 552 | WriteFile(hStdErr, s_szMsg3, sizeof(s_szMsg3) - 1, &cbWritten, NULL);
|
---|
| 553 |
|
---|
| 554 | #else
|
---|
| 555 | # error "Port me"
|
---|
| 556 | #endif
|
---|
| 557 | }
|
---|
| 558 |
|
---|
| 559 |
|
---|