- Made sure that the files that contains the declarations
[wine] / dlls / msvcrt / exit.c
1 /*
2  * msvcrt.dll exit functions
3  *
4  * Copyright 2000 Jon Griffiths
5  */
6 #include "msvcrt.h"
7
8 #include "msvcrt/conio.h"
9 #include "msvcrt/stdlib.h"
10
11
12 DEFAULT_DEBUG_CHANNEL(msvcrt);
13
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)
18
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 */
22
23 extern int MSVCRT_app_type;
24
25 /* INTERNAL: call atexit functions */
26 void __MSVCRT__call_atexit(void)
27 {
28   /* Note: should only be called with the exit lock held */
29   TRACE("%d atext functions to call\n", MSVCRT_atexit_registered);
30   /* Last registered gets executed first */
31   while (MSVCRT_atexit_registered > 0)
32   {
33     MSVCRT_atexit_registered--;
34     TRACE("next is %p\n",MSVCRT_atexit_table[MSVCRT_atexit_registered]);
35     if (MSVCRT_atexit_table[MSVCRT_atexit_registered])
36       (*MSVCRT_atexit_table[MSVCRT_atexit_registered])();
37     TRACE("returned\n");
38   }
39 }
40
41 /*********************************************************************
42  *              __dllonexit (MSVCRT.@)
43  */
44 _onexit_t __dllonexit(_onexit_t func, _onexit_t **start, _onexit_t **end)
45 {
46   _onexit_t *tmp;
47   int len;
48
49   TRACE("(%p,%p,%p)\n", func, start, end);
50
51   if (!start || !*start || !end || !*end)
52   {
53    FIXME("bad table\n");
54    return NULL;
55   }
56
57   len = (*end - *start);
58
59   TRACE("table start %p-%p, %d entries\n", *start, *end, len);
60
61   if (++len <= 0)
62     return NULL;
63
64   tmp = (_onexit_t *)MSVCRT_realloc(*start, len * sizeof(tmp));
65   if (!tmp)
66     return NULL;
67   *start = tmp;
68   *end = tmp + len;
69   tmp[len - 1] = func;
70   TRACE("new table start %p-%p, %d entries\n", *start, *end, len);
71   return func;
72 }
73
74 /*********************************************************************
75  *              _exit (MSVCRT.@)
76  */
77 void MSVCRT__exit(int exitcode)
78 {
79   TRACE("(%d)\n", exitcode);
80   ExitProcess(exitcode);
81 }
82
83 /*********************************************************************
84  *              _amsg_exit (MSVCRT.@)
85  */
86 void MSVCRT__amsg_exit(int errnum)
87 {
88   TRACE("(%d)\n", errnum);
89   /* FIXME: text for the error number. */
90   if (MSVCRT_app_type == 2)
91   {
92     /* FIXME: MsgBox */
93   }
94   _cprintf("\nruntime error R60%d\n",errnum);
95   MSVCRT__exit(255);
96 }
97
98 /*********************************************************************
99  *              abort (MSVCRT.@)
100  */
101 void MSVCRT_abort(void)
102 {
103   TRACE("(void)\n");
104   if (MSVCRT_app_type == 2)
105   {
106     /* FIXME: MsgBox */
107   }
108   _cputs("\nabnormal program termination\n");
109   MSVCRT__exit(3);
110 }
111
112 /*********************************************************************
113  *              _assert (MSVCRT.@)
114  */
115 void MSVCRT__assert(const char* str, const char* file, unsigned int line)
116 {
117   TRACE("(%s,%s,%d)\n",str,file,line);
118   if (MSVCRT_app_type == 2)
119   {
120     /* FIXME: MsgBox */
121   }
122   _cprintf("Assertion failed: %s, file %s, line %d\n\n",str,file, line);
123   MSVCRT_abort();
124 }
125
126 /*********************************************************************
127  *              _c_exit (MSVCRT.@)
128  */
129 void MSVCRT__c_exit(void)
130 {
131   TRACE("(void)\n");
132   /* All cleanup is done on DLL detach; Return to caller */
133 }
134
135 /*********************************************************************
136  *              _cexit (MSVCRT.@)
137  */
138 void MSVCRT__cexit(void)
139 {
140   TRACE("(void)\n");
141   /* All cleanup is done on DLL detach; Return to caller */
142 }
143
144 /*********************************************************************
145  *              _onexit (MSVCRT.@)
146  */
147 _onexit_t _onexit(_onexit_t func)
148 {
149   TRACE("(%p)\n",func);
150
151   if (!func)
152     return NULL;
153
154   LOCK_EXIT;
155   if (MSVCRT_atexit_registered > MSVCRT_atexit_table_size - 1)
156   {
157     _onexit_t *newtable;
158     TRACE("expanding table\n");
159     newtable = MSVCRT_calloc(sizeof(void *),MSVCRT_atexit_table_size + 32);
160     if (!newtable)
161     {
162       TRACE("failed!\n");
163       UNLOCK_EXIT;
164       return NULL;
165     }
166     memcpy (newtable, MSVCRT_atexit_table, MSVCRT_atexit_table_size);
167     MSVCRT_atexit_table_size += 32;
168     if (MSVCRT_atexit_table)
169       MSVCRT_free (MSVCRT_atexit_table);
170     MSVCRT_atexit_table = newtable;
171   }
172   MSVCRT_atexit_table[MSVCRT_atexit_registered] = func;
173   MSVCRT_atexit_registered++;
174   UNLOCK_EXIT;
175   return func;
176 }
177
178 /*********************************************************************
179  *              exit (MSVCRT.@)
180  */
181 void MSVCRT_exit(int exitcode)
182 {
183   TRACE("(%d)\n",exitcode);
184   LOCK_EXIT;
185   __MSVCRT__call_atexit();
186   UNLOCK_EXIT;
187   ExitProcess(exitcode);
188 }
189
190 /*********************************************************************
191  *              atexit (MSVCRT.@)
192  */
193 int MSVCRT_atexit(_onexit_t func)
194 {
195   TRACE("(%p)\n", func);
196   return _onexit(func) == func ? 0 : -1;
197 }
198
199 /*********************************************************************
200  *              _purecall (MSVCRT.@)
201  */
202 void _purecall(void)
203 {
204   TRACE("(void)\n");
205   MSVCRT__amsg_exit( 25 );
206 }