source: trunk/src/crtdll/exit.c@ 6666

Last change on this file since 6666 was 6645, checked in by bird, 24 years ago

Added $Id:$ keyword.

File size: 6.0 KB
Line 
1/* $Id: exit.c,v 1.2 2001-09-05 12:14:24 bird Exp $ */
2/*
3 * CRTDLL exit/abort/atexit functions
4 *
5 * Copyright 1996,1998 Marcus Meissner
6 * Copyright 1996 Jukka Iivonen
7 * Copyright 1997,2000 Uwe Bonnes
8 * Copyright 2000 Jon Griffiths
9 *
10 * exit functions differ in whether they perform cleanup
11 * and whether they return to the caller (really!).
12 * return do
13 * Name to caller? cleanup?
14 * _c_exit Y N
15 * _cexit Y Y
16 * _exit N N
17 * exit N Y
18 *
19 * Implementation Notes:
20 * Not MT Safe - Adding/Executing exit() functions should be locked
21 * for MT safety.
22 *
23 * FIXME:
24 * Need a better way of printing errors for GUI programs(MsgBox?).
25 * Is there really a difference between onexit/atexit?
26 */
27#include "crtdll.h"
28#include <errno.h>
29#include "process.h"
30
31
32DEFAULT_DEBUG_CHANNEL(crtdll);
33
34/* INTERNAL: Table of registered atexit() functions */
35/* FIXME: This should be dynamically allocated */
36#define CRTDLL_ATEXIT_TABLE_SIZE 16
37
38static atexit_function atexit_table[CRTDLL_ATEXIT_TABLE_SIZE];
39static int atexit_registered = 0; /* Points to free slot */
40
41
42/* INTERNAL: call atexit functions */
43void __CRTDLL__call_atexit(VOID);
44void __CRTDLL__call_atexit(VOID)
45{
46 /* Last registered gets executed first */
47 while (atexit_registered > 0)
48 {
49 atexit_registered--;
50 TRACE(":call function (%p)\n",atexit_table[atexit_registered]);
51 (*atexit_table[atexit_registered])();
52 }
53}
54
55
56/*********************************************************************
57 * __dllonexit (CRTDLL.25)
58 */
59VOID CDECL CRTDLL___dllonexit ()
60{
61 dprintf(("__dllonexit not implemented.\n"));
62 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
63}
64
65
66/*********************************************************************
67 * _abnormal_termination (CRTDLL.36)
68 */
69int CDECL CRTDLL__abnormal_termination(void)
70{
71 dprintf(("CRTDLL: _abnormal_termination not implemented.\n"));
72 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
73 return FALSE; /* FIXME: Can we determine if we are in an exception? */
74}
75
76
77/*********************************************************************
78 * _amsg_exit (CRTDLL.040)
79 *
80 * Print an error message and terminate execution. Returns 255 to the
81 * host OS.
82 */
83VOID CDECL CRTDLL__amsg_exit(int errnum)
84{
85 dprintf2(("CRTDLL: _amsg_exit\n"));
86
87 // fprintf(stderr,strerror(errnum));
88 // ExitProcess(-1);
89
90 CRTDLL_fprintf(CRTDLL_stderr,"\nrun-time error:\nError Code %d\n",errnum);
91 CRTDLL__exit(255);
92}
93
94
95/*********************************************************************
96 * _assert (CRTDLL.041)
97 *
98 * Print an assertion message and call abort(). Really only present
99 * for win binaries. Winelib programs would typically use libc's
100 * version.
101 */
102VOID CDECL CRTDLL__assert(LPVOID str, LPVOID file, UINT line)
103{
104 dprintf2(("CRTDLL: _assert\n"));
105
106 CRTDLL_fprintf(CRTDLL_stderr,"Assertion failed: %s, file %s, line %d\n\n",
107 (char*)str,(char*)file, line);
108 CRTDLL_abort();
109
110 // _assert(str, file, line);
111}
112
113
114/*********************************************************************
115 * _c_exit (CRTDLL.047)
116 * @@@PH verify if argument ret is correct, or void is correct (WINE)
117 */
118VOID CDECL CRTDLL__c_exit(INT ret)
119{
120 dprintf2(("_c_exit(%d)\n",ret));
121 FIXME("not calling CRTDLL cleanup\n");
122
123 /* dont exit, return to caller */
124
125 ExitProcess(ret);
126}
127
128
129/*********************************************************************
130 * _cexit (CRTDLL.049)
131 * @@@PH verify if argument ret is correct, or void is correct (WINE)
132 */
133VOID CDECL CRTDLL__cexit(INT ret)
134{
135 dprintf2(("_cexit(%d)\n",ret));
136 FIXME("not calling CRTDLL cleanup\n");
137 /* dont exit, return to caller */
138
139 ExitProcess(ret);
140}
141
142
143/*********************************************************************
144 * _exit (CRTDLL.087)
145 */
146VOID CDECL CRTDLL__exit(LONG ret)
147{
148 dprintf2(("CRTDLL: _exit (%08xh)\n",
149 ret));
150 TRACE(":exit code %ld\n",ret);
151 CRTDLL__c_exit(ret);
152 ExitProcess(ret);
153}
154
155
156/*********************************************************************
157 * _onexit (CRTDLL.236)
158 *
159 * Register a function to be called when the process terminates.
160 */
161atexit_function CDECL CRTDLL__onexit( atexit_function func)
162{
163 TRACE("registering function (%p)\n",func);
164 if (func && atexit_registered <= CRTDLL_ATEXIT_TABLE_SIZE - 1)
165 {
166 atexit_table[atexit_registered] = (atexit_function)func;
167 atexit_registered++;
168 return func; /* successful */
169 }
170 ERR(":Too many onexit() functions, or NULL function - not registered!\n");
171 return NULL;
172
173 // onexit_t CDECL CRTDLL__onexit(onexit_t t)
174 // {
175 // dprintf2(("CRTDLL: _onexit\n"));
176 // return (_onexit(t));
177 // }
178}
179
180
181/*********************************************************************
182 * exit (CRTDLL.359)
183 */
184VOID CDECL CRTDLL_exit(DWORD ret)
185{
186 TRACE(":exit code %ld\n",ret);
187 __CRTDLL__call_atexit();
188 CRTDLL__cexit(ret);
189 ExitProcess(ret);
190}
191
192
193/*********************************************************************
194 * abort (CRTDLL.335)
195 *
196 * Terminate the progam with an abnormal termination message. Returns
197 * 3 to the host OS.
198 */
199VOID CDECL CRTDLL_abort()
200{
201 dprintf2(("CRTDLL: abort\n"));
202
203 CRTDLL_fprintf(CRTDLL_stderr,"\nabnormal program termination\n");
204 CRTDLL__exit(3);
205 //abort();
206}
207
208
209/*********************************************************************
210 * atexit (CRTDLL.342)
211 *
212 * Register a function to be called when the process terminates.
213 */
214INT CDECL CRTDLL_atexit(void (*func)(void))
215{
216 dprintf(("CRTDLL: atexit\n"));
217
218 return CRTDLL__onexit(func) == func ? 0 : -1;
219
220 // if (_atexit_n >= sizeof (_atexit_v) / sizeof (_atexit_v[0]))
221 // return -1;
222 // _atexit_v[_atexit_n++] = func;
223 // return 0;
224}
225
226
227/*********************************************************************
228 * _aexit_rtn_dll (CRTDLL.39)
229 */
230VOID CDECL CRTDLL__aexit_rtn_dll(int exitcode)
231{
232 dprintf2(("CRTDLL: _aexit_rtn_dll\n"));
233 ExitProcess(exitcode);
234}
235
Note: See TracBrowser for help on using the repository browser.