| 1 | /* $Id: tstDllMain.c 2965 2007-02-13 21:04:40Z bird $ */
|
|---|
| 2 | /** @file
|
|---|
| 3 | *
|
|---|
| 4 | * kLdr testcase.
|
|---|
| 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 | #include "tst.h"
|
|---|
| 32 |
|
|---|
| 33 | #ifdef __OS2__
|
|---|
| 34 | # define INCL_BASE
|
|---|
| 35 | # include <os2.h>
|
|---|
| 36 | # include <string.h>
|
|---|
| 37 |
|
|---|
| 38 | #elif defined(__WIN__)
|
|---|
| 39 | # include <windows.h>
|
|---|
| 40 | # include <string.h>
|
|---|
| 41 |
|
|---|
| 42 | #elif defined(__DARWIN__)
|
|---|
| 43 | # include <unistd.h>
|
|---|
| 44 | # include <string.h>
|
|---|
| 45 |
|
|---|
| 46 | #else
|
|---|
| 47 | # error "port me"
|
|---|
| 48 | #endif
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 | /*******************************************************************************
|
|---|
| 52 | * Internal Functions *
|
|---|
| 53 | *******************************************************************************/
|
|---|
| 54 | void tstWrite(const char *psz);
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 | #ifdef __OS2__
|
|---|
| 59 | /**
|
|---|
| 60 | * OS/2 DLL 'main'
|
|---|
| 61 | */
|
|---|
| 62 | ULONG _System _DLL_InitTerm(HMODULE hmod, ULONG fFlags)
|
|---|
| 63 | {
|
|---|
| 64 | switch (fFlags)
|
|---|
| 65 | {
|
|---|
| 66 | case 0:
|
|---|
| 67 | tstWrite("init: ");
|
|---|
| 68 | tstWrite(g_pszName);
|
|---|
| 69 | tstWrite("\n");
|
|---|
| 70 | return TRUE;
|
|---|
| 71 |
|
|---|
| 72 | case 1:
|
|---|
| 73 | tstWrite("term: ");
|
|---|
| 74 | tstWrite(g_pszName);
|
|---|
| 75 | tstWrite("\n");
|
|---|
| 76 | return TRUE;
|
|---|
| 77 |
|
|---|
| 78 | default:
|
|---|
| 79 | tstWrite("!invalid!: ");
|
|---|
| 80 | tstWrite(g_pszName);
|
|---|
| 81 | tstWrite("\n");
|
|---|
| 82 | return FALSE;
|
|---|
| 83 | }
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | #elif defined(__WIN__)
|
|---|
| 87 |
|
|---|
| 88 | /**
|
|---|
| 89 | * OS/2 DLL 'main'
|
|---|
| 90 | */
|
|---|
| 91 | BOOL __stdcall DllMain(HANDLE hDllHandle, DWORD dwReason, LPVOID lpReserved)
|
|---|
| 92 | {
|
|---|
| 93 | switch (dwReason)
|
|---|
| 94 | {
|
|---|
| 95 | case DLL_PROCESS_ATTACH:
|
|---|
| 96 | tstWrite("init: ");
|
|---|
| 97 | tstWrite(g_pszName);
|
|---|
| 98 | tstWrite("\n");
|
|---|
| 99 | return TRUE;
|
|---|
| 100 |
|
|---|
| 101 | case DLL_PROCESS_DETACH:
|
|---|
| 102 | tstWrite("term: ");
|
|---|
| 103 | tstWrite(g_pszName);
|
|---|
| 104 | tstWrite("\n");
|
|---|
| 105 | return TRUE;
|
|---|
| 106 |
|
|---|
| 107 | case DLL_THREAD_ATTACH:
|
|---|
| 108 | tstWrite("thread init: ");
|
|---|
| 109 | tstWrite(g_pszName);
|
|---|
| 110 | tstWrite("\n");
|
|---|
| 111 | return TRUE;
|
|---|
| 112 |
|
|---|
| 113 | case DLL_THREAD_DETACH:
|
|---|
| 114 | tstWrite("thread term: ");
|
|---|
| 115 | tstWrite(g_pszName);
|
|---|
| 116 | tstWrite("\n");
|
|---|
| 117 | return TRUE;
|
|---|
| 118 |
|
|---|
| 119 | default:
|
|---|
| 120 | tstWrite("!invalid!: ");
|
|---|
| 121 | tstWrite(g_pszName);
|
|---|
| 122 | tstWrite("\n");
|
|---|
| 123 | return FALSE;
|
|---|
| 124 | }
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | #elif defined(__DARWIN__)
|
|---|
| 128 | /* later */
|
|---|
| 129 |
|
|---|
| 130 | #else
|
|---|
| 131 | # error "port me"
|
|---|
| 132 | #endif
|
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 | /**
|
|---|
| 136 | * Writes a string with unix lineendings.
|
|---|
| 137 | *
|
|---|
| 138 | * @param pszMsg The string.
|
|---|
| 139 | */
|
|---|
| 140 | void tstWrite(const char *pszMsg)
|
|---|
| 141 | {
|
|---|
| 142 | #if defined(__OS2__) || defined(__WIN__)
|
|---|
| 143 | /*
|
|---|
| 144 | * Line by line.
|
|---|
| 145 | */
|
|---|
| 146 | ULONG cbWritten;
|
|---|
| 147 | const char *pszNl = strchr(pszMsg, '\n');
|
|---|
| 148 |
|
|---|
| 149 | while (pszNl)
|
|---|
| 150 | {
|
|---|
| 151 | cbWritten = pszNl - pszMsg;
|
|---|
| 152 |
|
|---|
| 153 | #ifdef __OS2__
|
|---|
| 154 | if (cbWritten)
|
|---|
| 155 | DosWrite((HFILE)2, pszMsg, cbWritten, &cbWritten);
|
|---|
| 156 | DosWrite((HFILE)2, "\r\n", 2, &cbWritten);
|
|---|
| 157 | #else /* __WIN32__ */
|
|---|
| 158 | if (cbWritten)
|
|---|
| 159 | WriteFile((HANDLE)STD_ERROR_HANDLE, pszMsg, cbWritten, &cbWritten, NULL);
|
|---|
| 160 | WriteFile((HANDLE)STD_ERROR_HANDLE, "\r\n", 2, &cbWritten, NULL);
|
|---|
| 161 | #endif
|
|---|
| 162 |
|
|---|
| 163 | /* next */
|
|---|
| 164 | pszMsg = pszNl + 1;
|
|---|
| 165 | pszNl = strchr(pszMsg, '\n');
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | /*
|
|---|
| 169 | * Remaining incomplete line.
|
|---|
| 170 | */
|
|---|
| 171 | if (*pszMsg)
|
|---|
| 172 | {
|
|---|
| 173 | cbWritten = strlen(pszMsg);
|
|---|
| 174 | #ifdef __OS2__
|
|---|
| 175 | DosWrite((HFILE)2, pszMsg, cbWritten, &cbWritten);
|
|---|
| 176 | #else /* __WIN32__ */
|
|---|
| 177 | WriteFile((HANDLE)STD_ERROR_HANDLE, pszMsg, cbWritten, &cbWritten, NULL);
|
|---|
| 178 | #endif
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | #elif defined(__DARWIN__)
|
|---|
| 182 | write(STDERR_FILENO, pszMsg, strlen(pszMsg));
|
|---|
| 183 |
|
|---|
| 184 | #else
|
|---|
| 185 | # error "port me"
|
|---|
| 186 | #endif
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 |
|
|---|