Release 1.5.29.
[wine] / dlls / msvcrt / exit.c
1 /*
2  * msvcrt.dll exit functions
3  *
4  * Copyright 2000 Jon Griffiths
5  *
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.
10  *
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.
15  *
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
19  */
20 #include <stdio.h>
21 #include "msvcrt.h"
22 #include "mtdll.h"
23 #include "winuser.h"
24 #include "wine/debug.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
27
28 /* MT */
29 #define LOCK_EXIT   _mlock(_EXIT_LOCK1)
30 #define UNLOCK_EXIT _munlock(_EXIT_LOCK1)
31
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;
36
37 static const char szMsgBoxTitle[] = "Wine C++ Runtime Library";
38
39 extern int MSVCRT_app_type;
40 extern MSVCRT_wchar_t *MSVCRT__wpgmptr;
41
42 static unsigned int MSVCRT_abort_behavior =  MSVCRT__WRITE_ABORT_MSG | MSVCRT__CALL_REPORTFAULT;
43 static int MSVCRT_error_mode = MSVCRT__OUT_TO_DEFAULT;
44
45 void (*CDECL _aexit_rtn)(int) = MSVCRT__exit;
46
47 /* INTERNAL: call atexit functions */
48 static void __MSVCRT__call_atexit(void)
49 {
50   /* Note: should only be called with the exit lock held */
51   TRACE("%d atext functions to call\n", MSVCRT_atexit_registered);
52   /* Last registered gets executed first */
53   while (MSVCRT_atexit_registered > 0)
54   {
55     MSVCRT_atexit_registered--;
56     TRACE("next is %p\n",MSVCRT_atexit_table[MSVCRT_atexit_registered]);
57     if (MSVCRT_atexit_table[MSVCRT_atexit_registered])
58       (*MSVCRT_atexit_table[MSVCRT_atexit_registered])();
59     TRACE("returned\n");
60   }
61 }
62
63 /*********************************************************************
64  *              __dllonexit (MSVCRT.@)
65  */
66 MSVCRT__onexit_t CDECL __dllonexit(MSVCRT__onexit_t func, MSVCRT__onexit_t **start, MSVCRT__onexit_t **end)
67 {
68   MSVCRT__onexit_t *tmp;
69   int len;
70
71   TRACE("(%p,%p,%p)\n", func, start, end);
72
73   if (!start || !*start || !end || !*end)
74   {
75    FIXME("bad table\n");
76    return NULL;
77   }
78
79   len = (*end - *start);
80
81   TRACE("table start %p-%p, %d entries\n", *start, *end, len);
82
83   if (++len <= 0)
84     return NULL;
85
86   tmp = MSVCRT_realloc(*start, len * sizeof(*tmp));
87   if (!tmp)
88     return NULL;
89   *start = tmp;
90   *end = tmp + len;
91   tmp[len - 1] = func;
92   TRACE("new table start %p-%p, %d entries\n", *start, *end, len);
93   return func;
94 }
95
96 /*********************************************************************
97  *              _exit (MSVCRT.@)
98  */
99 void CDECL MSVCRT__exit(int exitcode)
100 {
101   TRACE("(%d)\n", exitcode);
102   ExitProcess(exitcode);
103 }
104
105 /* Print out an error message with an option to debug */
106 static void DoMessageBoxW(const MSVCRT_wchar_t *lead, const MSVCRT_wchar_t *message)
107 {
108   static const MSVCRT_wchar_t message_format[] = {'%','s','\n','\n','P','r','o','g','r','a','m',':',' ','%','s','\n',
109     '%','s','\n','\n','P','r','e','s','s',' ','O','K',' ','t','o',' ','e','x','i','t',' ','t','h','e',' ',
110     'p','r','o','g','r','a','m',',',' ','o','r',' ','C','a','n','c','e','l',' ','t','o',' ','s','t','a','r','t',' ',
111     't','h','e',' ','W','i','n','e',' ','d','e','b','b','u','g','e','r','.','\n',0};
112
113   MSGBOXPARAMSW msgbox;
114   MSVCRT_wchar_t text[2048];
115   INT ret;
116
117   MSVCRT__snwprintf(text,sizeof(text),message_format, lead, MSVCRT__wpgmptr, message);
118
119   msgbox.cbSize = sizeof(msgbox);
120   msgbox.hwndOwner = GetActiveWindow();
121   msgbox.hInstance = 0;
122   msgbox.lpszText = (LPCWSTR)text;
123   msgbox.lpszCaption = (LPCWSTR)szMsgBoxTitle;
124   msgbox.dwStyle = MB_OKCANCEL|MB_ICONERROR;
125   msgbox.lpszIcon = NULL;
126   msgbox.dwContextHelpId = 0;
127   msgbox.lpfnMsgBoxCallback = NULL;
128   msgbox.dwLanguageId = LANG_NEUTRAL;
129
130   ret = MessageBoxIndirectW(&msgbox);
131   if (ret == IDCANCEL)
132     DebugBreak();
133 }
134
135 static void DoMessageBox(const char *lead, const char *message)
136 {
137   MSVCRT_wchar_t leadW[1024], messageW[1024];
138
139   MSVCRT_mbstowcs(leadW, lead, 1024);
140   MSVCRT_mbstowcs(messageW, message, 1024);
141
142   DoMessageBoxW(leadW, messageW);
143 }
144
145 /*********************************************************************
146  *              _amsg_exit (MSVCRT.@)
147  */
148 void CDECL _amsg_exit(int errnum)
149 {
150   TRACE("(%d)\n", errnum);
151
152   if ((MSVCRT_error_mode == MSVCRT__OUT_TO_MSGBOX) ||
153      ((MSVCRT_error_mode == MSVCRT__OUT_TO_DEFAULT) && (MSVCRT_app_type == 2)))
154   {
155     char text[32];
156     sprintf(text, "Error: R60%d",errnum);
157     DoMessageBox("Runtime error!", text);
158   }
159   else
160     _cprintf("\nruntime error R60%d\n",errnum);
161   _aexit_rtn(255);
162 }
163
164 /*********************************************************************
165  *              abort (MSVCRT.@)
166  */
167 void CDECL MSVCRT_abort(void)
168 {
169   TRACE("()\n");
170
171   if (MSVCRT_abort_behavior & MSVCRT__WRITE_ABORT_MSG)
172   {
173     if ((MSVCRT_error_mode == MSVCRT__OUT_TO_MSGBOX) ||
174        ((MSVCRT_error_mode == MSVCRT__OUT_TO_DEFAULT) && (MSVCRT_app_type == 2)))
175     {
176       DoMessageBox("Runtime error!", "abnormal program termination");
177     }
178     else
179       _cputs("\nabnormal program termination\n");
180   }
181   MSVCRT_raise(MSVCRT_SIGABRT);
182   /* in case raise() returns */
183   MSVCRT__exit(3);
184 }
185
186 /*********************************************************************
187  *              _set_abort_behavior (MSVCRT.@)
188  *
189  * Not exported by native msvcrt, added in msvcr80
190  */
191 unsigned int CDECL MSVCRT__set_abort_behavior(unsigned int flags, unsigned int mask)
192 {
193   unsigned int old = MSVCRT_abort_behavior;
194
195   TRACE("%x, %x\n", flags, mask);
196   if (mask & MSVCRT__CALL_REPORTFAULT)
197     FIXME("_WRITE_CALL_REPORTFAULT unhandled\n");
198
199   MSVCRT_abort_behavior = (MSVCRT_abort_behavior & ~mask) | (flags & mask);
200   return old;
201 }
202
203 /*********************************************************************
204  *              _wassert (MSVCRT.@)
205  */
206 void CDECL MSVCRT__wassert(const MSVCRT_wchar_t* str, const MSVCRT_wchar_t* file, unsigned int line)
207 {
208   static const MSVCRT_wchar_t assertion_failed[] = {'A','s','s','e','r','t','i','o','n',' ','f','a','i','l','e','d','!',0};
209   static const MSVCRT_wchar_t format_msgbox[] = {'F','i','l','e',':',' ','%','s','\n','L','i','n','e',':',' ','%','d',
210       '\n','\n','E','x','p','r','e','s','s','i','o','n',':',' ','\"','%','s','\"',0};
211   static const MSVCRT_wchar_t format_console[] = {'A','s','s','e','r','t','i','o','n',' ','f','a','i','l','e','d',':',' ',
212       '%','s',',',' ','f','i','l','e',' ','%','s',',',' ','l','i','n','e',' ','%','d','\n','\n',0};
213
214   TRACE("(%s,%s,%d)\n", debugstr_w(str), debugstr_w(file), line);
215
216   if ((MSVCRT_error_mode == MSVCRT__OUT_TO_MSGBOX) ||
217      ((MSVCRT_error_mode == MSVCRT__OUT_TO_DEFAULT) && (MSVCRT_app_type == 2)))
218   {
219     MSVCRT_wchar_t text[2048];
220     MSVCRT__snwprintf(text, sizeof(text), format_msgbox, file, line, str);
221     DoMessageBoxW(assertion_failed, text);
222   }
223   else
224     _cwprintf(format_console, str, file, line);
225
226   MSVCRT_raise(MSVCRT_SIGABRT);
227   MSVCRT__exit(3);
228 }
229
230 /*********************************************************************
231  *              _assert (MSVCRT.@)
232  */
233 void CDECL MSVCRT__assert(const char* str, const char* file, unsigned int line)
234 {
235     MSVCRT_wchar_t strW[1024], fileW[1024];
236
237     MSVCRT_mbstowcs(strW, str, 1024);
238     MSVCRT_mbstowcs(fileW, file, 1024);
239
240     MSVCRT__wassert(strW, fileW, line);
241 }
242
243 /*********************************************************************
244  *              _c_exit (MSVCRT.@)
245  */
246 void CDECL MSVCRT__c_exit(void)
247 {
248   TRACE("(void)\n");
249   /* All cleanup is done on DLL detach; Return to caller */
250 }
251
252 /*********************************************************************
253  *              _cexit (MSVCRT.@)
254  */
255 void CDECL MSVCRT__cexit(void)
256 {
257   TRACE("(void)\n");
258   LOCK_EXIT;
259   __MSVCRT__call_atexit();
260   UNLOCK_EXIT;
261 }
262
263 /*********************************************************************
264  *              _onexit (MSVCRT.@)
265  */
266 MSVCRT__onexit_t CDECL MSVCRT__onexit(MSVCRT__onexit_t func)
267 {
268   TRACE("(%p)\n",func);
269
270   if (!func)
271     return NULL;
272
273   LOCK_EXIT;
274   if (MSVCRT_atexit_registered > MSVCRT_atexit_table_size - 1)
275   {
276     MSVCRT__onexit_t *newtable;
277     TRACE("expanding table\n");
278     newtable = MSVCRT_calloc(sizeof(void *),MSVCRT_atexit_table_size + 32);
279     if (!newtable)
280     {
281       TRACE("failed!\n");
282       UNLOCK_EXIT;
283       return NULL;
284     }
285     memcpy (newtable, MSVCRT_atexit_table, MSVCRT_atexit_table_size);
286     MSVCRT_atexit_table_size += 32;
287     MSVCRT_free (MSVCRT_atexit_table);
288     MSVCRT_atexit_table = newtable;
289   }
290   MSVCRT_atexit_table[MSVCRT_atexit_registered] = func;
291   MSVCRT_atexit_registered++;
292   UNLOCK_EXIT;
293   return func;
294 }
295
296 /*********************************************************************
297  *              exit (MSVCRT.@)
298  */
299 void CDECL MSVCRT_exit(int exitcode)
300 {
301   HMODULE hmscoree;
302   static const WCHAR mscoreeW[] = {'m','s','c','o','r','e','e',0};
303   void (WINAPI *pCorExitProcess)(int);
304
305   TRACE("(%d)\n",exitcode);
306   MSVCRT__cexit();
307
308   hmscoree = GetModuleHandleW(mscoreeW);
309
310   if (hmscoree)
311   {
312     pCorExitProcess = (void*)GetProcAddress(hmscoree, "CorExitProcess");
313
314     if (pCorExitProcess)
315       pCorExitProcess(exitcode);
316   }
317
318   ExitProcess(exitcode);
319 }
320
321 /*********************************************************************
322  *              atexit (MSVCRT.@)
323  */
324 int CDECL MSVCRT_atexit(void (*func)(void))
325 {
326   TRACE("(%p)\n", func);
327   return MSVCRT__onexit((MSVCRT__onexit_t)func) == (MSVCRT__onexit_t)func ? 0 : -1;
328 }
329
330 /* _set_purecall_handler - not exported in native msvcrt */
331 MSVCRT_purecall_handler CDECL _set_purecall_handler(MSVCRT_purecall_handler function)
332 {
333     MSVCRT_purecall_handler ret = purecall_handler;
334
335     TRACE("(%p)\n", function);
336     purecall_handler = function;
337     return ret;
338 }
339
340 /*********************************************************************
341  *              _purecall (MSVCRT.@)
342  */
343 void CDECL _purecall(void)
344 {
345   TRACE("(void)\n");
346
347   if(purecall_handler)
348       purecall_handler();
349   _amsg_exit( 25 );
350 }
351
352 /******************************************************************************
353  *              _set_error_mode (MSVCRT.@)
354  *
355  * Set the error mode, which describes where the C run-time writes error messages.
356  *
357  * PARAMS
358  *   mode - the new error mode
359  *
360  * RETURNS
361  *   The old error mode.
362  *
363  */
364 int CDECL _set_error_mode(int mode)
365 {
366
367   const int old = MSVCRT_error_mode;
368   if ( MSVCRT__REPORT_ERRMODE != mode ) {
369     MSVCRT_error_mode = mode;
370   }
371   return old;
372 }