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