mshtml: Make IEventTarget_{add,remove}EventListener_hook() static.
[wine.git] / dlls / msvcrt / exit.c
blob44b29c133c69df97e16d6bb81440530be2e2e9ce
1 /*
2 * msvcrt.dll exit functions
4 * Copyright 2000 Jon Griffiths
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include <stdio.h>
21 #include "msvcrt.h"
22 #include "mtdll.h"
23 #include "winuser.h"
24 #include "wine/debug.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
28 /* MT */
29 #define LOCK_EXIT _mlock(_EXIT_LOCK1)
30 #define UNLOCK_EXIT _munlock(_EXIT_LOCK1)
32 static MSVCRT__onexit_t *MSVCRT_atexit_table = NULL;
33 static int MSVCRT_atexit_table_size = 0;
34 static int MSVCRT_atexit_registered = 0; /* Points to free slot */
35 static MSVCRT_purecall_handler purecall_handler = NULL;
37 typedef struct MSVCRT__onexit_table_t
39 MSVCRT__onexit_t *_first;
40 MSVCRT__onexit_t *_last;
41 MSVCRT__onexit_t *_end;
42 } MSVCRT__onexit_table_t;
44 typedef void (__stdcall *_tls_callback_type)(void*,ULONG,void*);
45 static _tls_callback_type tls_atexit_callback;
47 static CRITICAL_SECTION MSVCRT_onexit_cs;
48 static CRITICAL_SECTION_DEBUG MSVCRT_onexit_cs_debug =
50 0, 0, &MSVCRT_onexit_cs,
51 { &MSVCRT_onexit_cs_debug.ProcessLocksList, &MSVCRT_onexit_cs_debug.ProcessLocksList },
52 0, 0, { (DWORD_PTR)(__FILE__ ": MSVCRT_onexit_cs") }
54 static CRITICAL_SECTION MSVCRT_onexit_cs = { &MSVCRT_onexit_cs_debug, -1, 0, 0, 0, 0 };
56 extern int MSVCRT_app_type;
57 extern MSVCRT_wchar_t *MSVCRT__wpgmptr;
59 static unsigned int MSVCRT_abort_behavior = MSVCRT__WRITE_ABORT_MSG | MSVCRT__CALL_REPORTFAULT;
60 static int MSVCRT_error_mode = MSVCRT__OUT_TO_DEFAULT;
62 void (*CDECL _aexit_rtn)(int) = MSVCRT__exit;
64 /* INTERNAL: call atexit functions */
65 static void __MSVCRT__call_atexit(void)
67 /* Note: should only be called with the exit lock held */
68 TRACE("%d atext functions to call\n", MSVCRT_atexit_registered);
69 if (tls_atexit_callback) tls_atexit_callback(NULL, DLL_PROCESS_DETACH, NULL);
70 /* Last registered gets executed first */
71 while (MSVCRT_atexit_registered > 0)
73 MSVCRT_atexit_registered--;
74 TRACE("next is %p\n",MSVCRT_atexit_table[MSVCRT_atexit_registered]);
75 if (MSVCRT_atexit_table[MSVCRT_atexit_registered])
76 (*MSVCRT_atexit_table[MSVCRT_atexit_registered])();
77 TRACE("returned\n");
81 /*********************************************************************
82 * __dllonexit (MSVCRT.@)
84 MSVCRT__onexit_t CDECL __dllonexit(MSVCRT__onexit_t func, MSVCRT__onexit_t **start, MSVCRT__onexit_t **end)
86 MSVCRT__onexit_t *tmp;
87 int len;
89 TRACE("(%p,%p,%p)\n", func, start, end);
91 if (!start || !*start || !end || !*end)
93 FIXME("bad table\n");
94 return NULL;
97 len = (*end - *start);
99 TRACE("table start %p-%p, %d entries\n", *start, *end, len);
101 if (++len <= 0)
102 return NULL;
104 tmp = MSVCRT_realloc(*start, len * sizeof(*tmp));
105 if (!tmp)
106 return NULL;
107 *start = tmp;
108 *end = tmp + len;
109 tmp[len - 1] = func;
110 TRACE("new table start %p-%p, %d entries\n", *start, *end, len);
111 return func;
114 /*********************************************************************
115 * _exit (MSVCRT.@)
117 void CDECL MSVCRT__exit(int exitcode)
119 TRACE("(%d)\n", exitcode);
120 ExitProcess(exitcode);
123 /* Print out an error message with an option to debug */
124 static void DoMessageBoxW(const MSVCRT_wchar_t *lead, const MSVCRT_wchar_t *message)
126 static const MSVCRT_wchar_t message_format[] = {'%','s','\n','\n','P','r','o','g','r','a','m',':',' ','%','s','\n',
127 '%','s','\n','\n','P','r','e','s','s',' ','O','K',' ','t','o',' ','e','x','i','t',' ','t','h','e',' ',
128 'p','r','o','g','r','a','m',',',' ','o','r',' ','C','a','n','c','e','l',' ','t','o',' ','s','t','a','r','t',' ',
129 't','h','e',' ','W','i','n','e',' ','d','e','b','u','g','g','e','r','.','\n',0};
130 static const WCHAR title[] =
131 {'W','i','n','e',' ','C','+','+',' ','R','u','n','t','i','m','e',' ','L','i','b','r','a','r','y',0};
133 MSGBOXPARAMSW msgbox;
134 MSVCRT_wchar_t text[2048];
135 INT ret;
137 MSVCRT__snwprintf(text, sizeof(text)/sizeof(text[0]), message_format,
138 lead, MSVCRT__wpgmptr, message);
140 msgbox.cbSize = sizeof(msgbox);
141 msgbox.hwndOwner = GetActiveWindow();
142 msgbox.hInstance = 0;
143 msgbox.lpszText = text;
144 msgbox.lpszCaption = title;
145 msgbox.dwStyle = MB_OKCANCEL|MB_ICONERROR;
146 msgbox.lpszIcon = NULL;
147 msgbox.dwContextHelpId = 0;
148 msgbox.lpfnMsgBoxCallback = NULL;
149 msgbox.dwLanguageId = LANG_NEUTRAL;
151 ret = MessageBoxIndirectW(&msgbox);
152 if (ret == IDCANCEL)
153 DebugBreak();
156 static void DoMessageBox(const char *lead, const char *message)
158 MSVCRT_wchar_t leadW[1024], messageW[1024];
160 MSVCRT_mbstowcs(leadW, lead, 1024);
161 MSVCRT_mbstowcs(messageW, message, 1024);
163 DoMessageBoxW(leadW, messageW);
166 /*********************************************************************
167 * _amsg_exit (MSVCRT.@)
169 void CDECL _amsg_exit(int errnum)
171 TRACE("(%d)\n", errnum);
173 if ((MSVCRT_error_mode == MSVCRT__OUT_TO_MSGBOX) ||
174 ((MSVCRT_error_mode == MSVCRT__OUT_TO_DEFAULT) && (MSVCRT_app_type == 2)))
176 char text[32];
177 sprintf(text, "Error: R60%d",errnum);
178 DoMessageBox("Runtime error!", text);
180 else
181 _cprintf("\nruntime error R60%d\n",errnum);
182 _aexit_rtn(255);
185 /*********************************************************************
186 * abort (MSVCRT.@)
188 void CDECL MSVCRT_abort(void)
190 TRACE("()\n");
192 if (MSVCRT_abort_behavior & MSVCRT__WRITE_ABORT_MSG)
194 if ((MSVCRT_error_mode == MSVCRT__OUT_TO_MSGBOX) ||
195 ((MSVCRT_error_mode == MSVCRT__OUT_TO_DEFAULT) && (MSVCRT_app_type == 2)))
197 DoMessageBox("Runtime error!", "abnormal program termination");
199 else
200 _cputs("\nabnormal program termination\n");
202 MSVCRT_raise(MSVCRT_SIGABRT);
203 /* in case raise() returns */
204 MSVCRT__exit(3);
207 /*********************************************************************
208 * _set_abort_behavior (MSVCR80.@)
210 unsigned int CDECL MSVCRT__set_abort_behavior(unsigned int flags, unsigned int mask)
212 unsigned int old = MSVCRT_abort_behavior;
214 TRACE("%x, %x\n", flags, mask);
215 if (mask & MSVCRT__CALL_REPORTFAULT)
216 FIXME("_WRITE_CALL_REPORTFAULT unhandled\n");
218 MSVCRT_abort_behavior = (MSVCRT_abort_behavior & ~mask) | (flags & mask);
219 return old;
222 /*********************************************************************
223 * _wassert (MSVCRT.@)
225 void CDECL MSVCRT__wassert(const MSVCRT_wchar_t* str, const MSVCRT_wchar_t* file, unsigned int line)
227 static const MSVCRT_wchar_t assertion_failed[] = {'A','s','s','e','r','t','i','o','n',' ','f','a','i','l','e','d','!',0};
228 static const MSVCRT_wchar_t format_msgbox[] = {'F','i','l','e',':',' ','%','s','\n','L','i','n','e',':',' ','%','d',
229 '\n','\n','E','x','p','r','e','s','s','i','o','n',':',' ','\"','%','s','\"',0};
230 static const MSVCRT_wchar_t format_console[] = {'A','s','s','e','r','t','i','o','n',' ','f','a','i','l','e','d',':',' ',
231 '%','s',',',' ','f','i','l','e',' ','%','s',',',' ','l','i','n','e',' ','%','d','\n','\n',0};
233 TRACE("(%s,%s,%d)\n", debugstr_w(str), debugstr_w(file), line);
235 if ((MSVCRT_error_mode == MSVCRT__OUT_TO_MSGBOX) ||
236 ((MSVCRT_error_mode == MSVCRT__OUT_TO_DEFAULT) && (MSVCRT_app_type == 2)))
238 MSVCRT_wchar_t text[2048];
239 MSVCRT__snwprintf(text, sizeof(text), format_msgbox, file, line, str);
240 DoMessageBoxW(assertion_failed, text);
242 else
243 _cwprintf(format_console, str, file, line);
245 MSVCRT_raise(MSVCRT_SIGABRT);
246 MSVCRT__exit(3);
249 /*********************************************************************
250 * _assert (MSVCRT.@)
252 void CDECL MSVCRT__assert(const char* str, const char* file, unsigned int line)
254 MSVCRT_wchar_t strW[1024], fileW[1024];
256 MSVCRT_mbstowcs(strW, str, 1024);
257 MSVCRT_mbstowcs(fileW, file, 1024);
259 MSVCRT__wassert(strW, fileW, line);
262 /*********************************************************************
263 * _c_exit (MSVCRT.@)
265 void CDECL MSVCRT__c_exit(void)
267 TRACE("(void)\n");
268 /* All cleanup is done on DLL detach; Return to caller */
271 /*********************************************************************
272 * _cexit (MSVCRT.@)
274 void CDECL MSVCRT__cexit(void)
276 TRACE("(void)\n");
277 LOCK_EXIT;
278 __MSVCRT__call_atexit();
279 UNLOCK_EXIT;
282 /*********************************************************************
283 * _onexit (MSVCRT.@)
285 MSVCRT__onexit_t CDECL MSVCRT__onexit(MSVCRT__onexit_t func)
287 TRACE("(%p)\n",func);
289 if (!func)
290 return NULL;
292 LOCK_EXIT;
293 if (MSVCRT_atexit_registered > MSVCRT_atexit_table_size - 1)
295 MSVCRT__onexit_t *newtable;
296 TRACE("expanding table\n");
297 newtable = MSVCRT_calloc(MSVCRT_atexit_table_size + 32, sizeof(void *));
298 if (!newtable)
300 TRACE("failed!\n");
301 UNLOCK_EXIT;
302 return NULL;
304 memcpy (newtable, MSVCRT_atexit_table, MSVCRT_atexit_table_size*sizeof(void *));
305 MSVCRT_atexit_table_size += 32;
306 MSVCRT_free (MSVCRT_atexit_table);
307 MSVCRT_atexit_table = newtable;
309 MSVCRT_atexit_table[MSVCRT_atexit_registered] = func;
310 MSVCRT_atexit_registered++;
311 UNLOCK_EXIT;
312 return func;
315 /*********************************************************************
316 * exit (MSVCRT.@)
318 void CDECL MSVCRT_exit(int exitcode)
320 HMODULE hmscoree;
321 static const WCHAR mscoreeW[] = {'m','s','c','o','r','e','e',0};
322 void (WINAPI *pCorExitProcess)(int);
324 TRACE("(%d)\n",exitcode);
325 MSVCRT__cexit();
327 hmscoree = GetModuleHandleW(mscoreeW);
329 if (hmscoree)
331 pCorExitProcess = (void*)GetProcAddress(hmscoree, "CorExitProcess");
333 if (pCorExitProcess)
334 pCorExitProcess(exitcode);
337 ExitProcess(exitcode);
340 /*********************************************************************
341 * atexit (MSVCRT.@)
343 int CDECL MSVCRT_atexit(void (*func)(void))
345 TRACE("(%p)\n", func);
346 return MSVCRT__onexit((MSVCRT__onexit_t)func) == (MSVCRT__onexit_t)func ? 0 : -1;
349 /*********************************************************************
350 * _crt_atexit (UCRTBASE.@)
352 int CDECL MSVCRT__crt_atexit(void (*func)(void))
354 TRACE("(%p)\n", func);
355 return MSVCRT__onexit((MSVCRT__onexit_t)func) == (MSVCRT__onexit_t)func ? 0 : -1;
359 /*********************************************************************
360 * _initialize_onexit_table (UCRTBASE.@)
362 int CDECL MSVCRT__initialize_onexit_table(MSVCRT__onexit_table_t *table)
364 TRACE("(%p)\n", table);
366 if (!table)
367 return -1;
369 if (table->_first == table->_end)
370 table->_last = table->_end = table->_first = NULL;
371 return 0;
374 /*********************************************************************
375 * _register_onexit_function (UCRTBASE.@)
377 int CDECL MSVCRT__register_onexit_function(MSVCRT__onexit_table_t *table, MSVCRT__onexit_t func)
379 TRACE("(%p %p)\n", table, func);
381 if (!table)
382 return -1;
384 EnterCriticalSection(&MSVCRT_onexit_cs);
385 if (!table->_first)
387 table->_first = MSVCRT_calloc(32, sizeof(void *));
388 if (!table->_first)
390 WARN("failed to allocate initial table.\n");
391 LeaveCriticalSection(&MSVCRT_onexit_cs);
392 return -1;
394 table->_last = table->_first;
395 table->_end = table->_first + 32;
398 /* grow if full */
399 if (table->_last == table->_end)
401 int len = table->_end - table->_first;
402 MSVCRT__onexit_t *tmp = MSVCRT_realloc(table->_first, 2 * len * sizeof(void *));
403 if (!tmp)
405 WARN("failed to grow table.\n");
406 LeaveCriticalSection(&MSVCRT_onexit_cs);
407 return -1;
409 table->_first = tmp;
410 table->_end = table->_first + 2 * len;
411 table->_last = table->_first + len;
414 *table->_last = func;
415 table->_last++;
416 LeaveCriticalSection(&MSVCRT_onexit_cs);
417 return 0;
420 /*********************************************************************
421 * _execute_onexit_table (UCRTBASE.@)
423 int CDECL MSVCRT__execute_onexit_table(MSVCRT__onexit_table_t *table)
425 MSVCRT__onexit_t *func;
426 MSVCRT__onexit_table_t copy;
428 TRACE("(%p)\n", table);
430 if (!table)
431 return -1;
433 EnterCriticalSection(&MSVCRT_onexit_cs);
434 if (!table->_first || table->_first >= table->_last)
436 LeaveCriticalSection(&MSVCRT_onexit_cs);
437 return 0;
439 copy._first = table->_first;
440 copy._last = table->_last;
441 copy._end = table->_end;
442 memset(table, 0, sizeof(*table));
443 MSVCRT__initialize_onexit_table(table);
444 LeaveCriticalSection(&MSVCRT_onexit_cs);
446 for (func = copy._last - 1; func >= copy._first; func--)
448 if (*func)
449 (*func)();
452 MSVCRT_free(copy._first);
453 return 0;
456 /*********************************************************************
457 * _register_thread_local_exe_atexit_callback (UCRTBASE.@)
459 void CDECL _register_thread_local_exe_atexit_callback(_tls_callback_type callback)
461 TRACE("(%p)\n", callback);
462 tls_atexit_callback = callback;
465 /*********************************************************************
466 * _set_purecall_handler (MSVCR71.@)
468 MSVCRT_purecall_handler CDECL _set_purecall_handler(MSVCRT_purecall_handler function)
470 MSVCRT_purecall_handler ret = purecall_handler;
472 TRACE("(%p)\n", function);
473 purecall_handler = function;
474 return ret;
477 /*********************************************************************
478 * _get_purecall_handler
480 MSVCRT_purecall_handler CDECL _get_purecall_handler(void)
482 TRACE("\n");
483 return purecall_handler;
486 /*********************************************************************
487 * _purecall (MSVCRT.@)
489 void CDECL _purecall(void)
491 TRACE("(void)\n");
493 if(purecall_handler)
494 purecall_handler();
495 _amsg_exit( 25 );
498 /******************************************************************************
499 * _set_error_mode (MSVCRT.@)
501 * Set the error mode, which describes where the C run-time writes error messages.
503 * PARAMS
504 * mode - the new error mode
506 * RETURNS
507 * The old error mode.
510 int CDECL _set_error_mode(int mode)
513 const int old = MSVCRT_error_mode;
514 if ( MSVCRT__REPORT_ERRMODE != mode ) {
515 MSVCRT_error_mode = mode;
517 return old;