/* $Id: kLdrHlp.c 2828 2006-10-22 18:21:04Z bird $ */ /** @file * * kLdr - The Dynamic Loader, Helper Functions. * * Copyright (c) 2006 knut st. osmundsen * * * This file is part of kLdr. * * kLdr is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * kLdr is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with kLdr; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ /******************************************************************************* * Header Files * *******************************************************************************/ #ifdef __OS2__ # define INCL_BASE # define INCL_ERRORS # include #elif defined(__WIN__) # include #else # error "port me" #endif #include #include "kLdrHlp.h" /** * Get an environment variable. * * @returns 0 on success, on failure an OS specific status code is returned. * @param pszVar The variable name. * @param pszVal Where to store the value. NULL is allowed if *pcchVal is 0. * @param pcchVal On input the size of the buffer pointed to by pszVal. * On output this contains the string length on success, while on * failure (including buffer overflow) the require buffer size. * If the variable wasn't found, it's set to 0. */ int kldrHlpGetEnv(const char *pszVar, char *pszVal, size_t *pcchVal) { #ifdef __OS2__ PSZ pszValue = NULL; int rc = DosScanEnv((PCSZ)pszVar, &pszValue); if (!rc) { size_t cch = kLdrHlpStrLen(pszValue); if (pszVal) { if (*pcchVal > cch) { kLdrHlpMemCopy(pszVal, pszValue, cch + 1); *pcchVal = cch; } else if (*pcchVal) { kLdrHlpMemCopy(pszVal, pszValue, *pcchVal); pszVal[*pcchVal - 1] = '\0'; rc = ERROR_BUFFER_OVERFLOW; *pcchVal = cch + 1; } } else { rc = ERROR_BUFFER_OVERFLOW; *pcchVal = cch + 1; } } else { if (pszVal) *pszVal = '\0'; *pcchVal = 0; } return rc; #elif defined(__WIN__) DWORD cch; SetLastError(0); cch = GetEnvironmentVariable(pszVar, pszVal, *pcchVal); if (cch) { *pcchVal = cch; return 0; } if (!GetLastError() == ERROR_ENVVAR_NOT_FOUND) { *pcchVal = 0; return ERROR_ENVVAR_NOT_FOUND; } *pcchVal = cch; return ERROR_BUFFER_OVERFLOW; #else # error "Port me" #endif } /** * Terminate the process. * * @param rc The exit status. */ void kldrHlpExit(int rc) { for (;;) { #ifdef __OS2__ DosExit(EXIT_PROCESS, rc); #elif defined(__WIN__) TerminateProcess(GetCurrentProcess(), rc); #else # error "Port me" #endif kldrHlpAssert(!"Impossible"); } } /** Internal worker for kldrHlpAssertMsg. */ static void int2dec(char *pszLine, unsigned iLine) { do { *pszLine = (iLine % 10) + '0'; iLine /= 10; } while (iLine); *pszLine++ = '\0'; } /** * Internal worker for the kLdrHlpAssert() macro. * * @param pszExpr The assert expression. * @param pszFile The filename. * @param iLine The line number. * @param pszFunction The function name. */ void kldrHlpAssertMsg(const char *pszExpr, const char *pszFile, unsigned iLine, const char *pszFunction) { #ifdef __OS2__ static const char s_szMsg1[] = "\r\n!!!kLdr Assertion Failed!!!\n\rExpression: "; static const char s_szMsg2[] = "\r\nAt: "; static const char s_szMsg3[] = "\r\n"; ULONG cbWritten; char szLine[16]; DosWrite((HFILE)2, s_szMsg1, sizeof(s_szMsg1) - 1, &cbWritten); DosWrite((HFILE)2, pszExpr, kLdrHlpStrLen(pszExpr), &cbWritten); DosWrite((HFILE)2, s_szMsg2, sizeof(s_szMsg2) - 1, &cbWritten); DosWrite((HFILE)2, pszFile, kLdrHlpStrLen(pszFile), &cbWritten); DosWrite((HFILE)2, "(", sizeof("(") - 1, &cbWritten); int2dec(szLine, iLine); DosWrite((HFILE)2, szLine, kLdrHlpStrLen(szLine), &cbWritten); DosWrite((HFILE)2, ") ", sizeof(") ") - 1, &cbWritten); DosWrite((HFILE)2, pszFunction, kLdrHlpStrLen(pszFunction), &cbWritten); DosWrite((HFILE)2, s_szMsg3, sizeof(s_szMsg3) - 1, &cbWritten); #elif defined(__WIN__) static const char s_szMsg1[] = "\r\n!!!kLdr Assertion Failed!!!\n\rExpression: "; static const char s_szMsg2[] = "\r\nAt: "; static const char s_szMsg3[] = "\r\n"; DWORD cbWritten; char szLine[16]; const HANDLE hStdErr = (HANDLE)STD_ERROR_HANDLE; WriteFile(hStdErr, s_szMsg1, sizeof(s_szMsg1) - 1, &cbWritten, NULL); WriteFile(hStdErr, pszExpr, kLdrHlpStrLen(pszExpr), &cbWritten, NULL); WriteFile(hStdErr, s_szMsg2, sizeof(s_szMsg2) - 1, &cbWritten, NULL); WriteFile(hStdErr, pszFile, kLdrHlpStrLen(pszFile), &cbWritten, NULL); WriteFile(hStdErr, "(", sizeof("(") - 1, &cbWritten, NULL); int2dec(szLine, iLine); WriteFile(hStdErr, szLine, kLdrHlpStrLen(szLine), &cbWritten, NULL); WriteFile(hStdErr, ") ", sizeof(") ") - 1, &cbWritten, NULL); WriteFile(hStdErr, pszFunction, kLdrHlpStrLen(pszFunction), &cbWritten, NULL); WriteFile(hStdErr, s_szMsg3, sizeof(s_szMsg3) - 1, &cbWritten, NULL); #else # error "Port me" #endif }