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
22 #include "msvcrt/conio.h"
23 #include "msvcrt/stdlib.h"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
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
])();
56 /*********************************************************************
57 * __dllonexit (MSVCRT.@)
59 _onexit_t
__dllonexit(_onexit_t func
, _onexit_t
**start
, _onexit_t
**end
)
64 TRACE("(%p,%p,%p)\n", func
, start
, end
);
66 if (!start
|| !*start
|| !end
|| !*end
)
72 len
= (*end
- *start
);
74 TRACE("table start %p-%p, %d entries\n", *start
, *end
, len
);
79 tmp
= (_onexit_t
*)MSVCRT_realloc(*start
, len
* sizeof(tmp
));
85 TRACE("new table start %p-%p, %d entries\n", *start
, *end
, len
);
89 /*********************************************************************
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)
109 _cprintf("\nruntime error R60%d\n",errnum
);
113 /*********************************************************************
116 void MSVCRT_abort(void)
119 if (MSVCRT_app_type
== 2)
123 _cputs("\nabnormal program termination\n");
127 /*********************************************************************
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)
137 _cprintf("Assertion failed: %s, file %s, line %d\n\n",str
,file
, line
);
141 /*********************************************************************
144 void MSVCRT__c_exit(void)
147 /* All cleanup is done on DLL detach; Return to caller */
150 /*********************************************************************
153 void MSVCRT__cexit(void)
156 /* All cleanup is done on DLL detach; Return to caller */
159 /*********************************************************************
162 _onexit_t
_onexit(_onexit_t func
)
164 TRACE("(%p)\n",func
);
170 if (MSVCRT_atexit_registered
> MSVCRT_atexit_table_size
- 1)
173 TRACE("expanding table\n");
174 newtable
= MSVCRT_calloc(sizeof(void *),MSVCRT_atexit_table_size
+ 32);
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
++;
193 /*********************************************************************
196 void MSVCRT_exit(int exitcode
)
198 TRACE("(%d)\n",exitcode
);
200 __MSVCRT__call_atexit();
202 ExitProcess(exitcode
);
205 /*********************************************************************
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.@)
221 MSVCRT__amsg_exit( 25 );