- Prefix many more functions, types, structs, etc. with MSVCRT_.
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 #include <stdio.h>
21 #include "msvcrt.h"
22
23 #include "msvcrt/conio.h"
24 #include "msvcrt/stdlib.h"
25 #include "mtdll.h"
26 #include "winuser.h"
27
28 #include "wine/debug.h"
29
30 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
31
32 /* MT */
33 #define LOCK_EXIT   _mlock(_EXIT_LOCK1)
34 #define UNLOCK_EXIT _munlock(_EXIT_LOCK1)
35
36 static MSVCRT__onexit_t *MSVCRT_atexit_table = NULL;
37 static int MSVCRT_atexit_table_size = 0;
38 static int MSVCRT_atexit_registered = 0; /* Points to free slot */
39
40 static LPCSTR szMsgBoxTitle = "Wine C++ Runtime Library";
41
42 extern int MSVCRT_app_type;
43 extern char *MSVCRT__pgmptr;
44
45 void (*_aexit_rtn)(int) = MSVCRT__exit;
46
47 /* INTERNAL: call atexit functions */
48 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 __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__onexit_t *)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 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 DoMessageBox(LPCSTR lead, LPCSTR message)
107 {
108   MSGBOXPARAMSA msgbox;
109   char text[2048];
110   INT ret;
111
112   snprintf(text,sizeof(text),"%s\n\nProgram: %s\n%s\n\n"
113                "Press OK to exit the program, or Cancel to start the Wine debugger.\n ",
114                lead, MSVCRT__pgmptr, message);
115
116   msgbox.cbSize = sizeof(msgbox);
117   msgbox.hwndOwner = GetActiveWindow();
118   msgbox.hInstance = 0;
119   msgbox.lpszText = text;
120   msgbox.lpszCaption = szMsgBoxTitle;
121   msgbox.dwStyle = MB_OKCANCEL|MB_ICONERROR;
122   msgbox.lpszIcon = NULL;
123   msgbox.dwContextHelpId = 0;
124   msgbox.lpfnMsgBoxCallback = NULL;
125   msgbox.dwLanguageId = LANG_NEUTRAL;
126
127   ret = MessageBoxIndirectA(&msgbox);
128   if (ret == IDCANCEL)
129     DebugBreak();
130 }
131
132 /*********************************************************************
133  *              _amsg_exit (MSVCRT.@)
134  */
135 void MSVCRT__amsg_exit(int errnum)
136 {
137   TRACE("(%d)\n", errnum);
138   /* FIXME: text for the error number. */
139   if (MSVCRT_app_type == 2)
140   {
141     char text[32];
142     sprintf(text, "Error: R60%d",errnum);
143     DoMessageBox("Runtime error!", text);
144   }
145   else
146     _cprintf("\nruntime error R60%d\n",errnum);
147   _aexit_rtn(255);
148 }
149
150 /*********************************************************************
151  *              abort (MSVCRT.@)
152  */
153 void MSVCRT_abort(void)
154 {
155   TRACE("()\n");
156   if (MSVCRT_app_type == 2)
157   {
158     DoMessageBox("Runtime error!", "abnormal program termination");
159   }
160   else
161     _cputs("\nabnormal program termination\n");
162   MSVCRT__exit(3);
163 }
164
165 /*********************************************************************
166  *              _assert (MSVCRT.@)
167  */
168 void MSVCRT__assert(const char* str, const char* file, unsigned int line)
169 {
170   TRACE("(%s,%s,%d)\n",str,file,line);
171   if (MSVCRT_app_type == 2)
172   {
173     char text[2048];
174     snprintf(text, sizeof(text), "File: %s\nLine: %d\n\nEpression: \"%s\"", file, line, str);
175     DoMessageBox("Assertion failed!", text);
176   }
177   else
178     _cprintf("Assertion failed: %s, file %s, line %d\n\n",str, file, line);
179   MSVCRT__exit(3);
180 }
181
182 /*********************************************************************
183  *              _c_exit (MSVCRT.@)
184  */
185 void MSVCRT__c_exit(void)
186 {
187   TRACE("(void)\n");
188   /* All cleanup is done on DLL detach; Return to caller */
189 }
190
191 /*********************************************************************
192  *              _cexit (MSVCRT.@)
193  */
194 void MSVCRT__cexit(void)
195 {
196   TRACE("(void)\n");
197   /* All cleanup is done on DLL detach; Return to caller */
198 }
199
200 /*********************************************************************
201  *              _onexit (MSVCRT.@)
202  */
203 MSVCRT__onexit_t MSVCRT__onexit(MSVCRT__onexit_t func)
204 {
205   TRACE("(%p)\n",func);
206
207   if (!func)
208     return NULL;
209
210   LOCK_EXIT;
211   if (MSVCRT_atexit_registered > MSVCRT_atexit_table_size - 1)
212   {
213     MSVCRT__onexit_t *newtable;
214     TRACE("expanding table\n");
215     newtable = MSVCRT_calloc(sizeof(void *),MSVCRT_atexit_table_size + 32);
216     if (!newtable)
217     {
218       TRACE("failed!\n");
219       UNLOCK_EXIT;
220       return NULL;
221     }
222     memcpy (newtable, MSVCRT_atexit_table, MSVCRT_atexit_table_size);
223     MSVCRT_atexit_table_size += 32;
224     if (MSVCRT_atexit_table)
225       MSVCRT_free (MSVCRT_atexit_table);
226     MSVCRT_atexit_table = newtable;
227   }
228   MSVCRT_atexit_table[MSVCRT_atexit_registered] = func;
229   MSVCRT_atexit_registered++;
230   UNLOCK_EXIT;
231   return func;
232 }
233
234 /*********************************************************************
235  *              exit (MSVCRT.@)
236  */
237 void MSVCRT_exit(int exitcode)
238 {
239   TRACE("(%d)\n",exitcode);
240   LOCK_EXIT;
241   __MSVCRT__call_atexit();
242   UNLOCK_EXIT;
243   ExitProcess(exitcode);
244 }
245
246 /*********************************************************************
247  *              atexit (MSVCRT.@)
248  */
249 int MSVCRT_atexit(void (*func)(void))
250 {
251   TRACE("(%p)\n", func);
252   return MSVCRT__onexit((MSVCRT__onexit_t)func) == (MSVCRT__onexit_t)func ? 0 : -1;
253 }
254
255
256 /*********************************************************************
257  *              _purecall (MSVCRT.@)
258  */
259 void _purecall(void)
260 {
261   TRACE("(void)\n");
262   MSVCRT__amsg_exit( 25 );
263 }