| 1 | /* $Id: kLdrHlp.c 2828 2006-10-22 18:21:04Z bird $ */
|
|---|
| 2 | /** @file
|
|---|
| 3 | *
|
|---|
| 4 | * kLdr - The Dynamic Loader, Helper Functions.
|
|---|
| 5 | *
|
|---|
| 6 | * Copyright (c) 2006 knut st. osmundsen <[email protected]>
|
|---|
| 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 |
|
|---|
| 45 | /**
|
|---|
| 46 | * Get an environment variable.
|
|---|
| 47 | *
|
|---|
| 48 | * @returns 0 on success, on failure an OS specific status code is returned.
|
|---|
| 49 | * @param pszVar The variable name.
|
|---|
| 50 | * @param pszVal Where to store the value. NULL is allowed if *pcchVal is 0.
|
|---|
| 51 | * @param pcchVal On input the size of the buffer pointed to by pszVal.
|
|---|
| 52 | * On output this contains the string length on success, while on
|
|---|
| 53 | * failure (including buffer overflow) the require buffer size.
|
|---|
| 54 | * If the variable wasn't found, it's set to 0.
|
|---|
| 55 | */
|
|---|
| 56 | int kldrHlpGetEnv(const char *pszVar, char *pszVal, size_t *pcchVal)
|
|---|
| 57 | {
|
|---|
| 58 | #ifdef __OS2__
|
|---|
| 59 | PSZ pszValue = NULL;
|
|---|
| 60 | int rc = DosScanEnv((PCSZ)pszVar, &pszValue);
|
|---|
| 61 | if (!rc)
|
|---|
| 62 | {
|
|---|
| 63 | size_t cch = kLdrHlpStrLen(pszValue);
|
|---|
| 64 | if (pszVal)
|
|---|
| 65 | {
|
|---|
| 66 | if (*pcchVal > cch)
|
|---|
| 67 | {
|
|---|
| 68 | kLdrHlpMemCopy(pszVal, pszValue, cch + 1);
|
|---|
| 69 | *pcchVal = cch;
|
|---|
| 70 | }
|
|---|
| 71 | else if (*pcchVal)
|
|---|
| 72 | {
|
|---|
| 73 | kLdrHlpMemCopy(pszVal, pszValue, *pcchVal);
|
|---|
| 74 | pszVal[*pcchVal - 1] = '\0';
|
|---|
| 75 | rc = ERROR_BUFFER_OVERFLOW;
|
|---|
| 76 | *pcchVal = cch + 1;
|
|---|
| 77 | }
|
|---|
| 78 | }
|
|---|
| 79 | else
|
|---|
| 80 | {
|
|---|
| 81 | rc = ERROR_BUFFER_OVERFLOW;
|
|---|
| 82 | *pcchVal = cch + 1;
|
|---|
| 83 | }
|
|---|
| 84 | }
|
|---|
| 85 | else
|
|---|
| 86 | {
|
|---|
| 87 | if (pszVal)
|
|---|
| 88 | *pszVal = '\0';
|
|---|
| 89 | *pcchVal = 0;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | return rc;
|
|---|
| 93 |
|
|---|
| 94 | #elif defined(__WIN__)
|
|---|
| 95 | DWORD cch;
|
|---|
| 96 | SetLastError(0);
|
|---|
| 97 | cch = GetEnvironmentVariable(pszVar, pszVal, *pcchVal);
|
|---|
| 98 | if (cch)
|
|---|
| 99 | {
|
|---|
| 100 | *pcchVal = cch;
|
|---|
| 101 | return 0;
|
|---|
| 102 | }
|
|---|
| 103 | if (!GetLastError() == ERROR_ENVVAR_NOT_FOUND)
|
|---|
| 104 | {
|
|---|
| 105 | *pcchVal = 0;
|
|---|
| 106 | return ERROR_ENVVAR_NOT_FOUND;
|
|---|
| 107 | }
|
|---|
| 108 | *pcchVal = cch;
|
|---|
| 109 | return ERROR_BUFFER_OVERFLOW;
|
|---|
| 110 |
|
|---|
| 111 | #else
|
|---|
| 112 | # error "Port me"
|
|---|
| 113 | #endif
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 | /**
|
|---|
| 118 | * Terminate the process.
|
|---|
| 119 | *
|
|---|
| 120 | * @param rc The exit status.
|
|---|
| 121 | */
|
|---|
| 122 | void kldrHlpExit(int rc)
|
|---|
| 123 | {
|
|---|
| 124 | for (;;)
|
|---|
| 125 | {
|
|---|
| 126 | #ifdef __OS2__
|
|---|
| 127 | DosExit(EXIT_PROCESS, rc);
|
|---|
| 128 |
|
|---|
| 129 | #elif defined(__WIN__)
|
|---|
| 130 | TerminateProcess(GetCurrentProcess(), rc);
|
|---|
| 131 |
|
|---|
| 132 | #else
|
|---|
| 133 | # error "Port me"
|
|---|
| 134 | #endif
|
|---|
| 135 | kldrHlpAssert(!"Impossible");
|
|---|
| 136 | }
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | /** Internal worker for kldrHlpAssertMsg. */
|
|---|
| 140 | static void int2dec(char *pszLine, unsigned iLine)
|
|---|
| 141 | {
|
|---|
| 142 | do
|
|---|
| 143 | {
|
|---|
| 144 | *pszLine = (iLine % 10) + '0';
|
|---|
| 145 | iLine /= 10;
|
|---|
| 146 | } while (iLine);
|
|---|
| 147 | *pszLine++ = '\0';
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 | /**
|
|---|
| 152 | * Internal worker for the kLdrHlpAssert() macro.
|
|---|
| 153 | *
|
|---|
| 154 | * @param pszExpr The assert expression.
|
|---|
| 155 | * @param pszFile The filename.
|
|---|
| 156 | * @param iLine The line number.
|
|---|
| 157 | * @param pszFunction The function name.
|
|---|
| 158 | */
|
|---|
| 159 | void kldrHlpAssertMsg(const char *pszExpr, const char *pszFile, unsigned iLine, const char *pszFunction)
|
|---|
| 160 | {
|
|---|
| 161 | #ifdef __OS2__
|
|---|
| 162 | static const char s_szMsg1[] = "\r\n!!!kLdr Assertion Failed!!!\n\rExpression: ";
|
|---|
| 163 | static const char s_szMsg2[] = "\r\nAt: ";
|
|---|
| 164 | static const char s_szMsg3[] = "\r\n";
|
|---|
| 165 | ULONG cbWritten;
|
|---|
| 166 | char szLine[16];
|
|---|
| 167 |
|
|---|
| 168 | DosWrite((HFILE)2, s_szMsg1, sizeof(s_szMsg1) - 1, &cbWritten);
|
|---|
| 169 | DosWrite((HFILE)2, pszExpr, kLdrHlpStrLen(pszExpr), &cbWritten);
|
|---|
| 170 | DosWrite((HFILE)2, s_szMsg2, sizeof(s_szMsg2) - 1, &cbWritten);
|
|---|
| 171 | DosWrite((HFILE)2, pszFile, kLdrHlpStrLen(pszFile), &cbWritten);
|
|---|
| 172 | DosWrite((HFILE)2, "(", sizeof("(") - 1, &cbWritten);
|
|---|
| 173 | int2dec(szLine, iLine);
|
|---|
| 174 | DosWrite((HFILE)2, szLine, kLdrHlpStrLen(szLine), &cbWritten);
|
|---|
| 175 | DosWrite((HFILE)2, ") ", sizeof(") ") - 1, &cbWritten);
|
|---|
| 176 | DosWrite((HFILE)2, pszFunction, kLdrHlpStrLen(pszFunction), &cbWritten);
|
|---|
| 177 | DosWrite((HFILE)2, s_szMsg3, sizeof(s_szMsg3) - 1, &cbWritten);
|
|---|
| 178 |
|
|---|
| 179 | #elif defined(__WIN__)
|
|---|
| 180 | static const char s_szMsg1[] = "\r\n!!!kLdr Assertion Failed!!!\n\rExpression: ";
|
|---|
| 181 | static const char s_szMsg2[] = "\r\nAt: ";
|
|---|
| 182 | static const char s_szMsg3[] = "\r\n";
|
|---|
| 183 | DWORD cbWritten;
|
|---|
| 184 | char szLine[16];
|
|---|
| 185 | const HANDLE hStdErr = (HANDLE)STD_ERROR_HANDLE;
|
|---|
| 186 |
|
|---|
| 187 | WriteFile(hStdErr, s_szMsg1, sizeof(s_szMsg1) - 1, &cbWritten, NULL);
|
|---|
| 188 | WriteFile(hStdErr, pszExpr, kLdrHlpStrLen(pszExpr), &cbWritten, NULL);
|
|---|
| 189 | WriteFile(hStdErr, s_szMsg2, sizeof(s_szMsg2) - 1, &cbWritten, NULL);
|
|---|
| 190 | WriteFile(hStdErr, pszFile, kLdrHlpStrLen(pszFile), &cbWritten, NULL);
|
|---|
| 191 | WriteFile(hStdErr, "(", sizeof("(") - 1, &cbWritten, NULL);
|
|---|
| 192 | int2dec(szLine, iLine);
|
|---|
| 193 | WriteFile(hStdErr, szLine, kLdrHlpStrLen(szLine), &cbWritten, NULL);
|
|---|
| 194 | WriteFile(hStdErr, ") ", sizeof(") ") - 1, &cbWritten, NULL);
|
|---|
| 195 | WriteFile(hStdErr, pszFunction, kLdrHlpStrLen(pszFunction), &cbWritten, NULL);
|
|---|
| 196 | WriteFile(hStdErr, s_szMsg3, sizeof(s_szMsg3) - 1, &cbWritten, NULL);
|
|---|
| 197 |
|
|---|
| 198 | #else
|
|---|
| 199 | # error "Port me"
|
|---|
| 200 | #endif
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 |
|
|---|