2001-01-11 00:59:25 +01:00
|
|
|
/*
|
|
|
|
* msvcrt.dll exit functions
|
|
|
|
*
|
|
|
|
* Copyright 2000 Jon Griffiths
|
2002-03-10 00:29:33 +01:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
2001-01-11 00:59:25 +01:00
|
|
|
*/
|
2003-03-15 23:25:01 +01:00
|
|
|
#include <stdio.h>
|
2001-01-11 00:59:25 +01:00
|
|
|
#include "msvcrt.h"
|
|
|
|
|
2001-04-11 01:25:25 +02:00
|
|
|
#include "msvcrt/conio.h"
|
|
|
|
#include "msvcrt/stdlib.h"
|
2002-02-21 21:22:00 +01:00
|
|
|
#include "mtdll.h"
|
2003-03-15 23:25:01 +01:00
|
|
|
#include "winuser.h"
|
2001-04-11 01:25:25 +02:00
|
|
|
|
2002-01-22 01:57:16 +01:00
|
|
|
#include "wine/debug.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
/* MT */
|
2002-02-21 21:22:00 +01:00
|
|
|
#define LOCK_EXIT _mlock(_EXIT_LOCK1)
|
|
|
|
#define UNLOCK_EXIT _munlock(_EXIT_LOCK1)
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2004-03-16 20:17:11 +01:00
|
|
|
static MSVCRT__onexit_t *MSVCRT_atexit_table = NULL;
|
2001-01-11 00:59:25 +01:00
|
|
|
static int MSVCRT_atexit_table_size = 0;
|
|
|
|
static int MSVCRT_atexit_registered = 0; /* Points to free slot */
|
|
|
|
|
2003-03-15 23:25:01 +01:00
|
|
|
static LPCSTR szMsgBoxTitle = "Wine C++ Runtime Library";
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
extern int MSVCRT_app_type;
|
2003-03-15 23:25:01 +01:00
|
|
|
extern char *MSVCRT__pgmptr;
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2004-01-13 06:45:05 +01:00
|
|
|
void (*_aexit_rtn)(int) = MSVCRT__exit;
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/* INTERNAL: call atexit functions */
|
|
|
|
void __MSVCRT__call_atexit(void)
|
|
|
|
{
|
|
|
|
/* Note: should only be called with the exit lock held */
|
|
|
|
TRACE("%d atext functions to call\n", MSVCRT_atexit_registered);
|
|
|
|
/* Last registered gets executed first */
|
|
|
|
while (MSVCRT_atexit_registered > 0)
|
|
|
|
{
|
|
|
|
MSVCRT_atexit_registered--;
|
|
|
|
TRACE("next is %p\n",MSVCRT_atexit_table[MSVCRT_atexit_registered]);
|
|
|
|
if (MSVCRT_atexit_table[MSVCRT_atexit_registered])
|
|
|
|
(*MSVCRT_atexit_table[MSVCRT_atexit_registered])();
|
|
|
|
TRACE("returned\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* __dllonexit (MSVCRT.@)
|
|
|
|
*/
|
2004-03-16 20:17:11 +01:00
|
|
|
MSVCRT__onexit_t __dllonexit(MSVCRT__onexit_t func, MSVCRT__onexit_t **start, MSVCRT__onexit_t **end)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2004-03-16 20:17:11 +01:00
|
|
|
MSVCRT__onexit_t *tmp;
|
2001-01-22 20:28:07 +01:00
|
|
|
int len;
|
|
|
|
|
|
|
|
TRACE("(%p,%p,%p)\n", func, start, end);
|
|
|
|
|
|
|
|
if (!start || !*start || !end || !*end)
|
|
|
|
{
|
|
|
|
FIXME("bad table\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = (*end - *start);
|
|
|
|
|
|
|
|
TRACE("table start %p-%p, %d entries\n", *start, *end, len);
|
|
|
|
|
|
|
|
if (++len <= 0)
|
|
|
|
return NULL;
|
|
|
|
|
2004-03-16 20:17:11 +01:00
|
|
|
tmp = (MSVCRT__onexit_t *)MSVCRT_realloc(*start, len * sizeof(tmp));
|
2001-01-22 20:28:07 +01:00
|
|
|
if (!tmp)
|
|
|
|
return NULL;
|
|
|
|
*start = tmp;
|
|
|
|
*end = tmp + len;
|
|
|
|
tmp[len - 1] = func;
|
|
|
|
TRACE("new table start %p-%p, %d entries\n", *start, *end, len);
|
|
|
|
return func;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _exit (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
void MSVCRT__exit(int exitcode)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
TRACE("(%d)\n", exitcode);
|
|
|
|
ExitProcess(exitcode);
|
|
|
|
}
|
|
|
|
|
2003-03-15 23:25:01 +01:00
|
|
|
/* Print out an error message with an option to debug */
|
|
|
|
static void DoMessageBox(LPCSTR lead, LPCSTR message)
|
|
|
|
{
|
|
|
|
MSGBOXPARAMSA msgbox;
|
|
|
|
char text[2048];
|
|
|
|
INT ret;
|
|
|
|
|
|
|
|
snprintf(text,sizeof(text),"%s\n\nProgram: %s\n%s\n\n"
|
|
|
|
"Press OK to exit the program, or Cancel to start the Wine debugger.\n ",
|
|
|
|
lead, MSVCRT__pgmptr, message);
|
|
|
|
|
|
|
|
msgbox.cbSize = sizeof(msgbox);
|
|
|
|
msgbox.hwndOwner = GetActiveWindow();
|
|
|
|
msgbox.hInstance = 0;
|
|
|
|
msgbox.lpszText = text;
|
|
|
|
msgbox.lpszCaption = szMsgBoxTitle;
|
|
|
|
msgbox.dwStyle = MB_OKCANCEL|MB_ICONERROR;
|
|
|
|
msgbox.lpszIcon = NULL;
|
|
|
|
msgbox.dwContextHelpId = 0;
|
|
|
|
msgbox.lpfnMsgBoxCallback = NULL;
|
|
|
|
msgbox.dwLanguageId = LANG_NEUTRAL;
|
|
|
|
|
|
|
|
ret = MessageBoxIndirectA(&msgbox);
|
|
|
|
if (ret == IDCANCEL)
|
|
|
|
DebugBreak();
|
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _amsg_exit (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
void MSVCRT__amsg_exit(int errnum)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
TRACE("(%d)\n", errnum);
|
|
|
|
/* FIXME: text for the error number. */
|
|
|
|
if (MSVCRT_app_type == 2)
|
|
|
|
{
|
2003-03-15 23:25:01 +01:00
|
|
|
char text[32];
|
|
|
|
sprintf(text, "Error: R60%d",errnum);
|
|
|
|
DoMessageBox("Runtime error!", text);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
2003-03-15 23:25:01 +01:00
|
|
|
else
|
|
|
|
_cprintf("\nruntime error R60%d\n",errnum);
|
2004-01-13 06:45:05 +01:00
|
|
|
_aexit_rtn(255);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* abort (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
void MSVCRT_abort(void)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2003-03-15 23:25:01 +01:00
|
|
|
TRACE("()\n");
|
2001-01-11 00:59:25 +01:00
|
|
|
if (MSVCRT_app_type == 2)
|
|
|
|
{
|
2003-03-15 23:25:01 +01:00
|
|
|
DoMessageBox("Runtime error!", "abnormal program termination");
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
2003-03-15 23:25:01 +01:00
|
|
|
else
|
|
|
|
_cputs("\nabnormal program termination\n");
|
2001-01-11 00:59:25 +01:00
|
|
|
MSVCRT__exit(3);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _assert (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
void MSVCRT__assert(const char* str, const char* file, unsigned int line)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
TRACE("(%s,%s,%d)\n",str,file,line);
|
|
|
|
if (MSVCRT_app_type == 2)
|
|
|
|
{
|
2003-03-15 23:25:01 +01:00
|
|
|
char text[2048];
|
|
|
|
snprintf(text, sizeof(text), "File: %s\nLine: %d\n\nEpression: \"%s\"", file, line, str);
|
|
|
|
DoMessageBox("Assertion failed!", text);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
2003-03-15 23:25:01 +01:00
|
|
|
else
|
|
|
|
_cprintf("Assertion failed: %s, file %s, line %d\n\n",str, file, line);
|
|
|
|
MSVCRT__exit(3);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _c_exit (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
void MSVCRT__c_exit(void)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
TRACE("(void)\n");
|
|
|
|
/* All cleanup is done on DLL detach; Return to caller */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _cexit (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
void MSVCRT__cexit(void)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
TRACE("(void)\n");
|
|
|
|
/* All cleanup is done on DLL detach; Return to caller */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _onexit (MSVCRT.@)
|
|
|
|
*/
|
2004-03-16 20:17:11 +01:00
|
|
|
MSVCRT__onexit_t MSVCRT__onexit(MSVCRT__onexit_t func)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
TRACE("(%p)\n",func);
|
|
|
|
|
|
|
|
if (!func)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
LOCK_EXIT;
|
|
|
|
if (MSVCRT_atexit_registered > MSVCRT_atexit_table_size - 1)
|
|
|
|
{
|
2004-03-16 20:17:11 +01:00
|
|
|
MSVCRT__onexit_t *newtable;
|
2001-01-11 00:59:25 +01:00
|
|
|
TRACE("expanding table\n");
|
|
|
|
newtable = MSVCRT_calloc(sizeof(void *),MSVCRT_atexit_table_size + 32);
|
|
|
|
if (!newtable)
|
|
|
|
{
|
|
|
|
TRACE("failed!\n");
|
|
|
|
UNLOCK_EXIT;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
memcpy (newtable, MSVCRT_atexit_table, MSVCRT_atexit_table_size);
|
|
|
|
MSVCRT_atexit_table_size += 32;
|
|
|
|
if (MSVCRT_atexit_table)
|
|
|
|
MSVCRT_free (MSVCRT_atexit_table);
|
|
|
|
MSVCRT_atexit_table = newtable;
|
|
|
|
}
|
|
|
|
MSVCRT_atexit_table[MSVCRT_atexit_registered] = func;
|
|
|
|
MSVCRT_atexit_registered++;
|
|
|
|
UNLOCK_EXIT;
|
|
|
|
return func;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* exit (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
void MSVCRT_exit(int exitcode)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
TRACE("(%d)\n",exitcode);
|
|
|
|
LOCK_EXIT;
|
|
|
|
__MSVCRT__call_atexit();
|
|
|
|
UNLOCK_EXIT;
|
|
|
|
ExitProcess(exitcode);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* atexit (MSVCRT.@)
|
|
|
|
*/
|
2002-02-22 22:20:39 +01:00
|
|
|
int MSVCRT_atexit(void (*func)(void))
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
TRACE("(%p)\n", func);
|
2004-03-16 20:17:11 +01:00
|
|
|
return MSVCRT__onexit((MSVCRT__onexit_t)func) == (MSVCRT__onexit_t)func ? 0 : -1;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
2002-02-22 22:20:39 +01:00
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _purecall (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
void _purecall(void)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
TRACE("(void)\n");
|
|
|
|
MSVCRT__amsg_exit( 25 );
|
|
|
|
}
|