Include the msvcrt headers, remove duplicate definitions.
[wine.git] / dlls / msvcrt / exit.c
blob569fb395f202a882c33545005a750563e87b94b6
1 /*
2 * msvcrt.dll exit functions
4 * Copyright 2000 Jon Griffiths
5 */
6 #include "msvcrt.h"
8 #include "msvcrt/conio.h"
9 #include "msvcrt/stdlib.h"
12 DEFAULT_DEBUG_CHANNEL(msvcrt);
14 /* MT */
15 extern CRITICAL_SECTION MSVCRT_exit_cs;
16 #define LOCK_EXIT EnterCriticalSection(&MSVCRT_exit_cs)
17 #define UNLOCK_EXIT LeaveCriticalSection(&MSVCRT_exit_cs)
19 static _onexit_t *MSVCRT_atexit_table = NULL;
20 static int MSVCRT_atexit_table_size = 0;
21 static int MSVCRT_atexit_registered = 0; /* Points to free slot */
23 extern int MSVCRT_app_type;
24 void *MSVCRT_realloc(void *ptr, unsigned int size);
26 /* INTERNAL: call atexit functions */
27 void __MSVCRT__call_atexit(void)
29 /* Note: should only be called with the exit lock held */
30 TRACE("%d atext functions to call\n", MSVCRT_atexit_registered);
31 /* Last registered gets executed first */
32 while (MSVCRT_atexit_registered > 0)
34 MSVCRT_atexit_registered--;
35 TRACE("next is %p\n",MSVCRT_atexit_table[MSVCRT_atexit_registered]);
36 if (MSVCRT_atexit_table[MSVCRT_atexit_registered])
37 (*MSVCRT_atexit_table[MSVCRT_atexit_registered])();
38 TRACE("returned\n");
42 /*********************************************************************
43 * __dllonexit (MSVCRT.@)
45 _onexit_t __dllonexit(_onexit_t func, _onexit_t **start, _onexit_t **end)
47 _onexit_t *tmp;
48 int len;
50 TRACE("(%p,%p,%p)\n", func, start, end);
52 if (!start || !*start || !end || !*end)
54 FIXME("bad table\n");
55 return NULL;
58 len = (*end - *start);
60 TRACE("table start %p-%p, %d entries\n", *start, *end, len);
62 if (++len <= 0)
63 return NULL;
65 tmp = (_onexit_t *)MSVCRT_realloc(*start, len * sizeof(tmp));
66 if (!tmp)
67 return NULL;
68 *start = tmp;
69 *end = tmp + len;
70 tmp[len - 1] = func;
71 TRACE("new table start %p-%p, %d entries\n", *start, *end, len);
72 return func;
75 /*********************************************************************
76 * _exit (MSVCRT.@)
78 void MSVCRT__exit(int exitcode)
80 TRACE("(%d)\n", exitcode);
81 ExitProcess(exitcode);
84 /*********************************************************************
85 * _amsg_exit (MSVCRT.@)
87 void MSVCRT__amsg_exit(int errnum)
89 TRACE("(%d)\n", errnum);
90 /* FIXME: text for the error number. */
91 if (MSVCRT_app_type == 2)
93 /* FIXME: MsgBox */
95 _cprintf("\nruntime error R60%d\n",errnum);
96 MSVCRT__exit(255);
99 /*********************************************************************
100 * abort (MSVCRT.@)
102 void MSVCRT_abort(void)
104 TRACE("(void)\n");
105 if (MSVCRT_app_type == 2)
107 /* FIXME: MsgBox */
109 _cputs("\nabnormal program termination\n");
110 MSVCRT__exit(3);
113 /*********************************************************************
114 * _assert (MSVCRT.@)
116 void MSVCRT__assert(const char* str, const char* file, unsigned int line)
118 TRACE("(%s,%s,%d)\n",str,file,line);
119 if (MSVCRT_app_type == 2)
121 /* FIXME: MsgBox */
123 _cprintf("Assertion failed: %s, file %s, line %d\n\n",str,file, line);
124 MSVCRT_abort();
127 /*********************************************************************
128 * _c_exit (MSVCRT.@)
130 void MSVCRT__c_exit(void)
132 TRACE("(void)\n");
133 /* All cleanup is done on DLL detach; Return to caller */
136 /*********************************************************************
137 * _cexit (MSVCRT.@)
139 void MSVCRT__cexit(void)
141 TRACE("(void)\n");
142 /* All cleanup is done on DLL detach; Return to caller */
145 /*********************************************************************
146 * _onexit (MSVCRT.@)
148 _onexit_t _onexit(_onexit_t func)
150 TRACE("(%p)\n",func);
152 if (!func)
153 return NULL;
155 LOCK_EXIT;
156 if (MSVCRT_atexit_registered > MSVCRT_atexit_table_size - 1)
158 _onexit_t *newtable;
159 TRACE("expanding table\n");
160 newtable = MSVCRT_calloc(sizeof(void *),MSVCRT_atexit_table_size + 32);
161 if (!newtable)
163 TRACE("failed!\n");
164 UNLOCK_EXIT;
165 return NULL;
167 memcpy (newtable, MSVCRT_atexit_table, MSVCRT_atexit_table_size);
168 MSVCRT_atexit_table_size += 32;
169 if (MSVCRT_atexit_table)
170 MSVCRT_free (MSVCRT_atexit_table);
171 MSVCRT_atexit_table = newtable;
173 MSVCRT_atexit_table[MSVCRT_atexit_registered] = func;
174 MSVCRT_atexit_registered++;
175 UNLOCK_EXIT;
176 return func;
179 /*********************************************************************
180 * exit (MSVCRT.@)
182 void MSVCRT_exit(int exitcode)
184 TRACE("(%d)\n",exitcode);
185 LOCK_EXIT;
186 __MSVCRT__call_atexit();
187 UNLOCK_EXIT;
188 ExitProcess(exitcode);
191 /*********************************************************************
192 * atexit (MSVCRT.@)
194 int MSVCRT_atexit(_onexit_t func)
196 TRACE("(%p)\n", func);
197 return _onexit(func) == func ? 0 : -1;
200 /*********************************************************************
201 * _purecall (MSVCRT.@)
203 void _purecall(void)
205 TRACE("(void)\n");
206 MSVCRT__amsg_exit( 25 );