wineconsole: Try harder to get a scalable font.
[wine.git] / dlls / msvcrt / exit.c
blob51bc6f07e1ba5d860dda6277332a2f4eb3bc580d
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_purecall_handler purecall_handler = NULL;
34 typedef struct MSVCRT__onexit_table_t
36 MSVCRT__onexit_t *_first;
37 MSVCRT__onexit_t *_last;
38 MSVCRT__onexit_t *_end;
39 } MSVCRT__onexit_table_t;
41 static MSVCRT__onexit_table_t MSVCRT_atexit_table;
43 #if _MSVCR_VER>=140
44 static MSVCRT__onexit_table_t MSVCRT_quick_exit_table;
45 #endif
47 typedef void (__stdcall *_tls_callback_type)(void*,ULONG,void*);
48 static _tls_callback_type tls_atexit_callback;
50 static CRITICAL_SECTION MSVCRT_onexit_cs;
51 static CRITICAL_SECTION_DEBUG MSVCRT_onexit_cs_debug =
53 0, 0, &MSVCRT_onexit_cs,
54 { &MSVCRT_onexit_cs_debug.ProcessLocksList, &MSVCRT_onexit_cs_debug.ProcessLocksList },
55 0, 0, { (DWORD_PTR)(__FILE__ ": MSVCRT_onexit_cs") }
57 static CRITICAL_SECTION MSVCRT_onexit_cs = { &MSVCRT_onexit_cs_debug, -1, 0, 0, 0, 0 };
59 extern int MSVCRT_app_type;
60 extern MSVCRT_wchar_t *MSVCRT__wpgmptr;
62 static unsigned int MSVCRT_abort_behavior = MSVCRT__WRITE_ABORT_MSG | MSVCRT__CALL_REPORTFAULT;
63 static int MSVCRT_error_mode = MSVCRT__OUT_TO_DEFAULT;
65 void (*CDECL _aexit_rtn)(int) = MSVCRT__exit;
67 static int initialize_onexit_table(MSVCRT__onexit_table_t *table)
69 if (!table)
70 return -1;
72 if (table->_first == table->_end)
73 table->_last = table->_end = table->_first = NULL;
74 return 0;
77 static int register_onexit_function(MSVCRT__onexit_table_t *table, MSVCRT__onexit_t func)
79 if (!table)
80 return -1;
82 EnterCriticalSection(&MSVCRT_onexit_cs);
83 if (!table->_first)
85 table->_first = MSVCRT_calloc(32, sizeof(void *));
86 if (!table->_first)
88 WARN("failed to allocate initial table.\n");
89 LeaveCriticalSection(&MSVCRT_onexit_cs);
90 return -1;
92 table->_last = table->_first;
93 table->_end = table->_first + 32;
96 /* grow if full */
97 if (table->_last == table->_end)
99 int len = table->_end - table->_first;
100 MSVCRT__onexit_t *tmp = MSVCRT_realloc(table->_first, 2 * len * sizeof(void *));
101 if (!tmp)
103 WARN("failed to grow table.\n");
104 LeaveCriticalSection(&MSVCRT_onexit_cs);
105 return -1;
107 table->_first = tmp;
108 table->_end = table->_first + 2 * len;
109 table->_last = table->_first + len;
112 *table->_last = func;
113 table->_last++;
114 LeaveCriticalSection(&MSVCRT_onexit_cs);
115 return 0;
118 static int execute_onexit_table(MSVCRT__onexit_table_t *table)
120 MSVCRT__onexit_t *func;
121 MSVCRT__onexit_table_t copy;
123 if (!table)
124 return -1;
126 EnterCriticalSection(&MSVCRT_onexit_cs);
127 if (!table->_first || table->_first >= table->_last)
129 LeaveCriticalSection(&MSVCRT_onexit_cs);
130 return 0;
132 copy._first = table->_first;
133 copy._last = table->_last;
134 copy._end = table->_end;
135 memset(table, 0, sizeof(*table));
136 initialize_onexit_table(table);
137 LeaveCriticalSection(&MSVCRT_onexit_cs);
139 for (func = copy._last - 1; func >= copy._first; func--)
141 if (*func)
142 (*func)();
145 MSVCRT_free(copy._first);
146 return 0;
149 static void call_atexit(void)
151 /* Note: should only be called with the exit lock held */
152 if (tls_atexit_callback) tls_atexit_callback(NULL, DLL_PROCESS_DETACH, NULL);
153 execute_onexit_table(&MSVCRT_atexit_table);
156 /*********************************************************************
157 * __dllonexit (MSVCRT.@)
159 MSVCRT__onexit_t CDECL __dllonexit(MSVCRT__onexit_t func, MSVCRT__onexit_t **start, MSVCRT__onexit_t **end)
161 MSVCRT__onexit_t *tmp;
162 int len;
164 TRACE("(%p,%p,%p)\n", func, start, end);
166 if (!start || !*start || !end || !*end)
168 FIXME("bad table\n");
169 return NULL;
172 len = (*end - *start);
174 TRACE("table start %p-%p, %d entries\n", *start, *end, len);
176 if (++len <= 0)
177 return NULL;
179 tmp = MSVCRT_realloc(*start, len * sizeof(*tmp));
180 if (!tmp)
181 return NULL;
182 *start = tmp;
183 *end = tmp + len;
184 tmp[len - 1] = func;
185 TRACE("new table start %p-%p, %d entries\n", *start, *end, len);
186 return func;
189 /*********************************************************************
190 * _exit (MSVCRT.@)
192 void CDECL MSVCRT__exit(int exitcode)
194 TRACE("(%d)\n", exitcode);
195 ExitProcess(exitcode);
198 /* Print out an error message with an option to debug */
199 static void DoMessageBoxW(const MSVCRT_wchar_t *lead, const MSVCRT_wchar_t *message)
201 static const MSVCRT_wchar_t message_format[] = {'%','s','\n','\n','P','r','o','g','r','a','m',':',' ','%','s','\n',
202 '%','s','\n','\n','P','r','e','s','s',' ','O','K',' ','t','o',' ','e','x','i','t',' ','t','h','e',' ',
203 'p','r','o','g','r','a','m',',',' ','o','r',' ','C','a','n','c','e','l',' ','t','o',' ','s','t','a','r','t',' ',
204 't','h','e',' ','W','i','n','e',' ','d','e','b','u','g','g','e','r','.','\n',0};
205 static const WCHAR title[] =
206 {'W','i','n','e',' ','C','+','+',' ','R','u','n','t','i','m','e',' ','L','i','b','r','a','r','y',0};
208 MSGBOXPARAMSW msgbox;
209 MSVCRT_wchar_t text[2048];
210 INT ret;
212 MSVCRT__snwprintf(text, ARRAY_SIZE(text), message_format, lead, MSVCRT__wpgmptr, message);
214 msgbox.cbSize = sizeof(msgbox);
215 msgbox.hwndOwner = GetActiveWindow();
216 msgbox.hInstance = 0;
217 msgbox.lpszText = text;
218 msgbox.lpszCaption = title;
219 msgbox.dwStyle = MB_OKCANCEL|MB_ICONERROR;
220 msgbox.lpszIcon = NULL;
221 msgbox.dwContextHelpId = 0;
222 msgbox.lpfnMsgBoxCallback = NULL;
223 msgbox.dwLanguageId = LANG_NEUTRAL;
225 ret = MessageBoxIndirectW(&msgbox);
226 if (ret == IDCANCEL)
227 DebugBreak();
230 static void DoMessageBox(const char *lead, const char *message)
232 MSVCRT_wchar_t leadW[1024], messageW[1024];
234 MSVCRT_mbstowcs(leadW, lead, 1024);
235 MSVCRT_mbstowcs(messageW, message, 1024);
237 DoMessageBoxW(leadW, messageW);
240 /*********************************************************************
241 * _amsg_exit (MSVCRT.@)
243 void CDECL _amsg_exit(int errnum)
245 TRACE("(%d)\n", errnum);
247 if ((MSVCRT_error_mode == MSVCRT__OUT_TO_MSGBOX) ||
248 ((MSVCRT_error_mode == MSVCRT__OUT_TO_DEFAULT) && (MSVCRT_app_type == 2)))
250 char text[32];
251 sprintf(text, "Error: R60%d",errnum);
252 DoMessageBox("Runtime error!", text);
254 else
255 _cprintf("\nruntime error R60%d\n",errnum);
256 _aexit_rtn(255);
259 /*********************************************************************
260 * abort (MSVCRT.@)
262 void CDECL MSVCRT_abort(void)
264 TRACE("()\n");
266 if (MSVCRT_abort_behavior & MSVCRT__WRITE_ABORT_MSG)
268 if ((MSVCRT_error_mode == MSVCRT__OUT_TO_MSGBOX) ||
269 ((MSVCRT_error_mode == MSVCRT__OUT_TO_DEFAULT) && (MSVCRT_app_type == 2)))
271 DoMessageBox("Runtime error!", "abnormal program termination");
273 else
274 _cputs("\nabnormal program termination\n");
276 MSVCRT_raise(MSVCRT_SIGABRT);
277 /* in case raise() returns */
278 MSVCRT__exit(3);
281 #if _MSVCR_VER>=80
282 /*********************************************************************
283 * _set_abort_behavior (MSVCR80.@)
285 unsigned int CDECL MSVCRT__set_abort_behavior(unsigned int flags, unsigned int mask)
287 unsigned int old = MSVCRT_abort_behavior;
289 TRACE("%x, %x\n", flags, mask);
290 if (mask & MSVCRT__CALL_REPORTFAULT)
291 FIXME("_WRITE_CALL_REPORTFAULT unhandled\n");
293 MSVCRT_abort_behavior = (MSVCRT_abort_behavior & ~mask) | (flags & mask);
294 return old;
296 #endif
298 /*********************************************************************
299 * _wassert (MSVCRT.@)
301 void CDECL MSVCRT__wassert(const MSVCRT_wchar_t* str, const MSVCRT_wchar_t* file, unsigned int line)
303 static const MSVCRT_wchar_t assertion_failed[] = {'A','s','s','e','r','t','i','o','n',' ','f','a','i','l','e','d','!',0};
304 static const MSVCRT_wchar_t format_msgbox[] = {'F','i','l','e',':',' ','%','s','\n','L','i','n','e',':',' ','%','d',
305 '\n','\n','E','x','p','r','e','s','s','i','o','n',':',' ','\"','%','s','\"',0};
306 static const MSVCRT_wchar_t format_console[] = {'A','s','s','e','r','t','i','o','n',' ','f','a','i','l','e','d',':',' ',
307 '%','s',',',' ','f','i','l','e',' ','%','s',',',' ','l','i','n','e',' ','%','d','\n','\n',0};
309 TRACE("(%s,%s,%d)\n", debugstr_w(str), debugstr_w(file), line);
311 if ((MSVCRT_error_mode == MSVCRT__OUT_TO_MSGBOX) ||
312 ((MSVCRT_error_mode == MSVCRT__OUT_TO_DEFAULT) && (MSVCRT_app_type == 2)))
314 MSVCRT_wchar_t text[2048];
315 MSVCRT__snwprintf(text, sizeof(text), format_msgbox, file, line, str);
316 DoMessageBoxW(assertion_failed, text);
318 else
319 _cwprintf(format_console, str, file, line);
321 MSVCRT_raise(MSVCRT_SIGABRT);
322 MSVCRT__exit(3);
325 /*********************************************************************
326 * _assert (MSVCRT.@)
328 void CDECL MSVCRT__assert(const char* str, const char* file, unsigned int line)
330 MSVCRT_wchar_t strW[1024], fileW[1024];
332 MSVCRT_mbstowcs(strW, str, 1024);
333 MSVCRT_mbstowcs(fileW, file, 1024);
335 MSVCRT__wassert(strW, fileW, line);
338 /*********************************************************************
339 * _c_exit (MSVCRT.@)
341 void CDECL MSVCRT__c_exit(void)
343 TRACE("(void)\n");
344 /* All cleanup is done on DLL detach; Return to caller */
347 /*********************************************************************
348 * _cexit (MSVCRT.@)
350 void CDECL MSVCRT__cexit(void)
352 TRACE("(void)\n");
353 LOCK_EXIT;
354 call_atexit();
355 UNLOCK_EXIT;
358 /*********************************************************************
359 * _onexit (MSVCRT.@)
361 MSVCRT__onexit_t CDECL MSVCRT__onexit(MSVCRT__onexit_t func)
363 TRACE("(%p)\n",func);
365 if (!func)
366 return NULL;
368 LOCK_EXIT;
369 register_onexit_function(&MSVCRT_atexit_table, func);
370 UNLOCK_EXIT;
372 return func;
375 /*********************************************************************
376 * exit (MSVCRT.@)
378 void CDECL MSVCRT_exit(int exitcode)
380 HMODULE hmscoree;
381 static const WCHAR mscoreeW[] = {'m','s','c','o','r','e','e',0};
382 void (WINAPI *pCorExitProcess)(int);
384 TRACE("(%d)\n",exitcode);
385 MSVCRT__cexit();
387 hmscoree = GetModuleHandleW(mscoreeW);
389 if (hmscoree)
391 pCorExitProcess = (void*)GetProcAddress(hmscoree, "CorExitProcess");
393 if (pCorExitProcess)
394 pCorExitProcess(exitcode);
397 ExitProcess(exitcode);
400 /*********************************************************************
401 * atexit (MSVCRT.@)
403 int CDECL MSVCRT_atexit(void (__cdecl *func)(void))
405 TRACE("(%p)\n", func);
406 return MSVCRT__onexit((MSVCRT__onexit_t)func) == (MSVCRT__onexit_t)func ? 0 : -1;
409 #if _MSVCR_VER>=140
411 /*********************************************************************
412 * _crt_at_quick_exit (UCRTBASE.@)
414 int CDECL MSVCRT__crt_at_quick_exit(void (__cdecl *func)(void))
416 TRACE("(%p)\n", func);
417 return register_onexit_function(&MSVCRT_quick_exit_table, (MSVCRT__onexit_t)func);
420 /*********************************************************************
421 * quick_exit (MSVCRT.@)
423 void CDECL MSVCRT_quick_exit(int exitcode)
425 TRACE("(%d)\n", exitcode);
427 execute_onexit_table(&MSVCRT_quick_exit_table);
428 MSVCRT__exit(exitcode);
431 /*********************************************************************
432 * _crt_atexit (UCRTBASE.@)
434 int CDECL MSVCRT__crt_atexit(void (__cdecl *func)(void))
436 TRACE("(%p)\n", func);
437 return MSVCRT__onexit((MSVCRT__onexit_t)func) == (MSVCRT__onexit_t)func ? 0 : -1;
440 /*********************************************************************
441 * _initialize_onexit_table (UCRTBASE.@)
443 int CDECL MSVCRT__initialize_onexit_table(MSVCRT__onexit_table_t *table)
445 TRACE("(%p)\n", table);
447 return initialize_onexit_table(table);
450 /*********************************************************************
451 * _register_onexit_function (UCRTBASE.@)
453 int CDECL MSVCRT__register_onexit_function(MSVCRT__onexit_table_t *table, MSVCRT__onexit_t func)
455 TRACE("(%p %p)\n", table, func);
457 return register_onexit_function(table, func);
460 /*********************************************************************
461 * _execute_onexit_table (UCRTBASE.@)
463 int CDECL MSVCRT__execute_onexit_table(MSVCRT__onexit_table_t *table)
465 TRACE("(%p)\n", table);
467 return execute_onexit_table(table);
470 /*********************************************************************
471 * _register_thread_local_exe_atexit_callback (UCRTBASE.@)
473 void CDECL _register_thread_local_exe_atexit_callback(_tls_callback_type callback)
475 TRACE("(%p)\n", callback);
476 tls_atexit_callback = callback;
479 #endif /* _MSVCR_VER>=140 */
481 #if _MSVCR_VER>=71
482 /*********************************************************************
483 * _set_purecall_handler (MSVCR71.@)
485 MSVCRT_purecall_handler CDECL _set_purecall_handler(MSVCRT_purecall_handler function)
487 MSVCRT_purecall_handler ret = purecall_handler;
489 TRACE("(%p)\n", function);
490 purecall_handler = function;
491 return ret;
493 #endif
495 #if _MSVCR_VER>=80
496 /*********************************************************************
497 * _get_purecall_handler (MSVCR80.@)
499 MSVCRT_purecall_handler CDECL _get_purecall_handler(void)
501 TRACE("\n");
502 return purecall_handler;
504 #endif
506 /*********************************************************************
507 * _purecall (MSVCRT.@)
509 void CDECL _purecall(void)
511 TRACE("(void)\n");
513 if(purecall_handler)
514 purecall_handler();
515 _amsg_exit( 25 );
518 /******************************************************************************
519 * _set_error_mode (MSVCRT.@)
521 * Set the error mode, which describes where the C run-time writes error messages.
523 * PARAMS
524 * mode - the new error mode
526 * RETURNS
527 * The old error mode.
530 int CDECL _set_error_mode(int mode)
533 const int old = MSVCRT_error_mode;
534 if ( MSVCRT__REPORT_ERRMODE != mode ) {
535 MSVCRT_error_mode = mode;
537 return old;