Moved a few remaining 16-bit window functions to wnd16.c and moved it
[wine.git] / dlls / msvcrt / exit.c
blob1c39ea14067f9f565a1861d456c6bb9299ca8c61
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "msvcrt.h"
22 #include "msvcrt/conio.h"
23 #include "msvcrt/stdlib.h"
24 #include "mtdll.h"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
30 /* MT */
31 #define LOCK_EXIT _mlock(_EXIT_LOCK1)
32 #define UNLOCK_EXIT _munlock(_EXIT_LOCK1)
34 static _onexit_t *MSVCRT_atexit_table = NULL;
35 static int MSVCRT_atexit_table_size = 0;
36 static int MSVCRT_atexit_registered = 0; /* Points to free slot */
38 extern int MSVCRT_app_type;
40 /* INTERNAL: call atexit functions */
41 void __MSVCRT__call_atexit(void)
43 /* Note: should only be called with the exit lock held */
44 TRACE("%d atext functions to call\n", MSVCRT_atexit_registered);
45 /* Last registered gets executed first */
46 while (MSVCRT_atexit_registered > 0)
48 MSVCRT_atexit_registered--;
49 TRACE("next is %p\n",MSVCRT_atexit_table[MSVCRT_atexit_registered]);
50 if (MSVCRT_atexit_table[MSVCRT_atexit_registered])
51 (*MSVCRT_atexit_table[MSVCRT_atexit_registered])();
52 TRACE("returned\n");
56 /*********************************************************************
57 * __dllonexit (MSVCRT.@)
59 _onexit_t __dllonexit(_onexit_t func, _onexit_t **start, _onexit_t **end)
61 _onexit_t *tmp;
62 int len;
64 TRACE("(%p,%p,%p)\n", func, start, end);
66 if (!start || !*start || !end || !*end)
68 FIXME("bad table\n");
69 return NULL;
72 len = (*end - *start);
74 TRACE("table start %p-%p, %d entries\n", *start, *end, len);
76 if (++len <= 0)
77 return NULL;
79 tmp = (_onexit_t *)MSVCRT_realloc(*start, len * sizeof(tmp));
80 if (!tmp)
81 return NULL;
82 *start = tmp;
83 *end = tmp + len;
84 tmp[len - 1] = func;
85 TRACE("new table start %p-%p, %d entries\n", *start, *end, len);
86 return func;
89 /*********************************************************************
90 * _exit (MSVCRT.@)
92 void MSVCRT__exit(int exitcode)
94 TRACE("(%d)\n", exitcode);
95 ExitProcess(exitcode);
98 /*********************************************************************
99 * _amsg_exit (MSVCRT.@)
101 void MSVCRT__amsg_exit(int errnum)
103 TRACE("(%d)\n", errnum);
104 /* FIXME: text for the error number. */
105 if (MSVCRT_app_type == 2)
107 /* FIXME: MsgBox */
109 _cprintf("\nruntime error R60%d\n",errnum);
110 MSVCRT__exit(255);
113 /*********************************************************************
114 * abort (MSVCRT.@)
116 void MSVCRT_abort(void)
118 TRACE("(void)\n");
119 if (MSVCRT_app_type == 2)
121 /* FIXME: MsgBox */
123 _cputs("\nabnormal program termination\n");
124 MSVCRT__exit(3);
127 /*********************************************************************
128 * _assert (MSVCRT.@)
130 void MSVCRT__assert(const char* str, const char* file, unsigned int line)
132 TRACE("(%s,%s,%d)\n",str,file,line);
133 if (MSVCRT_app_type == 2)
135 /* FIXME: MsgBox */
137 _cprintf("Assertion failed: %s, file %s, line %d\n\n",str,file, line);
138 MSVCRT_abort();
141 /*********************************************************************
142 * _c_exit (MSVCRT.@)
144 void MSVCRT__c_exit(void)
146 TRACE("(void)\n");
147 /* All cleanup is done on DLL detach; Return to caller */
150 /*********************************************************************
151 * _cexit (MSVCRT.@)
153 void MSVCRT__cexit(void)
155 TRACE("(void)\n");
156 /* All cleanup is done on DLL detach; Return to caller */
159 /*********************************************************************
160 * _onexit (MSVCRT.@)
162 _onexit_t _onexit(_onexit_t func)
164 TRACE("(%p)\n",func);
166 if (!func)
167 return NULL;
169 LOCK_EXIT;
170 if (MSVCRT_atexit_registered > MSVCRT_atexit_table_size - 1)
172 _onexit_t *newtable;
173 TRACE("expanding table\n");
174 newtable = MSVCRT_calloc(sizeof(void *),MSVCRT_atexit_table_size + 32);
175 if (!newtable)
177 TRACE("failed!\n");
178 UNLOCK_EXIT;
179 return NULL;
181 memcpy (newtable, MSVCRT_atexit_table, MSVCRT_atexit_table_size);
182 MSVCRT_atexit_table_size += 32;
183 if (MSVCRT_atexit_table)
184 MSVCRT_free (MSVCRT_atexit_table);
185 MSVCRT_atexit_table = newtable;
187 MSVCRT_atexit_table[MSVCRT_atexit_registered] = func;
188 MSVCRT_atexit_registered++;
189 UNLOCK_EXIT;
190 return func;
193 /*********************************************************************
194 * exit (MSVCRT.@)
196 void MSVCRT_exit(int exitcode)
198 TRACE("(%d)\n",exitcode);
199 LOCK_EXIT;
200 __MSVCRT__call_atexit();
201 UNLOCK_EXIT;
202 ExitProcess(exitcode);
205 /*********************************************************************
206 * atexit (MSVCRT.@)
208 int MSVCRT_atexit(void (*func)(void))
210 TRACE("(%p)\n", func);
211 return _onexit((_onexit_t)func) == (_onexit_t)func ? 0 : -1;
215 /*********************************************************************
216 * _purecall (MSVCRT.@)
218 void _purecall(void)
220 TRACE("(void)\n");
221 MSVCRT__amsg_exit( 25 );