revert between 56095 -> 55830 in arch
[AROS.git] / compiler / stdc / __stdc_startup.c
blobf897d5349b6973bba75910ef0cfc088553c9411a
1 /*
2 Copyright © 2009-2013, The AROS Development Team. All rights reserved.
3 $Id$
4 */
5 #include <dos/stdio.h>
6 #include <exec/alerts.h>
7 #include <proto/exec.h>
9 #include <assert.h>
10 #include <setjmp.h>
12 #define DEBUG 0
13 #include <aros/debug.h>
15 #include "__stdc_intbase.h"
16 #include "__exitfunc.h"
18 /*****************************************************************************
20 NAME */
21 void __stdc_program_startup(
23 /* SYNOPSIS */
24 jmp_buf exitjmp,
25 int *errorptr)
27 /* FUNCTION
28 This is called during program startup and before calling main.
29 This is to allow stdc.library to do some initialization that couldn't
30 be done when opening the library.
32 INPUTS
33 exitjmp - jmp_buf to jump to to exit the program
34 errorptr - pointer to store return value of program
36 RESULT
39 NOTES
40 This function is normally called by the startup code so one
41 should not need to do it oneself.
43 TODO: Maybe this function should be implemented using Tags so that
44 functionality can be extended in the future without breaking backwards
45 compatibility.
47 EXAMPLE
49 BUGS
51 SEE ALSO
53 INTERNALS
55 ******************************************************************************/
57 struct StdCIntBase *StdCBase =
58 (struct StdCIntBase *)__aros_getbase_StdCBase();
60 D(bug("[__stdc_program_startup] StdCBase 0x%p\n", StdCBase));
62 StdCBase->startup_errorptr = errorptr;
63 *StdCBase->exit_jmpbuf = *exitjmp;
66 /*****************************************************************************
68 NAME */
69 void __stdc_program_end(
71 /* SYNOPSIS */
72 void)
74 /* FUNCTION
75 This function is to be called when main() has returned or after
76 program has exited. This allows to stdc.library to do some
77 cleanup that can't be done during closing of the library.
79 INPUTS
82 RESULT
85 NOTES
86 This function is normally called by the startup code so one
87 should not need to do it oneself.
89 TODO: Maybe this function should be implemented using Tags so that
90 functionality can be extended in the future without breaking backwards
91 compatibility.
93 EXAMPLE
95 BUGS
97 SEE ALSO
99 INTERNALS
102 ******************************************************************************/
104 struct StdCIntBase *StdCBase =
105 (struct StdCIntBase *)__aros_getbase_StdCBase();
106 D(bug("[__stdc_program_end]\n"));
108 struct ETask *etask = GetETask(FindTask(NULL));
109 if (etask)
110 etask->et_Result1 = *StdCBase->startup_errorptr;
112 if (!(StdCBase->flags & ABNORMAL_EXIT))
113 __callexitfuncs();
116 /*****************************************************************************
118 NAME */
119 int *__stdc_set_errorptr(
121 /* SYNOPSIS */
122 int *errorptr)
124 /* FUNCTION
125 This function sets the pointer to store error return value for
126 program exit.
128 INPUTS
129 errorptr - new pointer to return value
131 RESULT
132 old pointer to return value
134 NOTES
136 EXAMPLE
138 BUGS
140 SEE ALSO
142 INTERNALS
144 ******************************************************************************/
146 struct StdCIntBase *StdCBase =
147 (struct StdCIntBase *)__aros_getbase_StdCBase();
148 int *old = StdCBase->startup_errorptr;
150 StdCBase->startup_errorptr = errorptr;
152 return old;
155 /*****************************************************************************
157 NAME */
158 int *__stdc_get_errorptr(
160 /* SYNOPSIS */
161 void)
163 /* FUNCTION
164 This function gets the pointer to store error return value for
165 program exit.
167 INPUTS
170 RESULT
171 pointer to return value
173 NOTES
175 EXAMPLE
177 BUGS
179 SEE ALSO
181 INTERNALS
183 ******************************************************************************/
185 struct StdCIntBase *StdCBase =
186 (struct StdCIntBase *)__aros_getbase_StdCBase();
187 return StdCBase->startup_errorptr;
190 /*****************************************************************************
192 NAME */
193 void __stdc_set_exitjmp(
195 /* SYNOPSIS */
196 jmp_buf exitjmp,
197 jmp_buf previousjmp)
199 /* FUNCTION
200 This function set the jmp_buf to use for directly exiting current
201 program.
203 INPUTS
204 exitjmp - new jmp_buf for exiting
206 RESULT
207 previous jmp_buf for exiting
209 NOTES
211 EXAMPLE
213 BUGS
215 SEE ALSO
217 INTERNALS
219 ******************************************************************************/
221 struct StdCIntBase *StdCBase =
222 (struct StdCIntBase *)__aros_getbase_StdCBase();
224 *previousjmp = *StdCBase->exit_jmpbuf;
225 *StdCBase->exit_jmpbuf = *exitjmp;
228 /*****************************************************************************
230 NAME */
231 void __stdc_jmp2exit(
233 /* SYNOPSIS */
234 int normal,
235 int retcode)
237 /* FUNCTION
238 This function directly jumps to the exit of a program.
240 INPUTS
241 normal - Indicates if exit is normal or not. When it is abnormal no
242 atexit functions will be called.
243 retcode - the return code for the program.
245 RESULT
248 NOTES
249 In normal operation this function does not return.
250 If this function returns it means that this function was called in a
251 context where jmp_buf for exit was not initialized. Likely cause is
252 a module that opened stdc.library.
253 Be sure to capture this situation.
255 EXAMPLE
257 BUGS
259 SEE ALSO
261 INTERNALS
263 ******************************************************************************/
265 struct StdCIntBase *StdCBase =
266 (struct StdCIntBase *)__aros_getbase_StdCBase();
268 /* No __stdc_progam_startup() called; Alert()
270 if (StdCBase->startup_errorptr == NULL)
272 kprintf("[__stdc_jmp2exit] Trying to exit without proper initialization\n");
273 Alert(AT_DeadEnd | AG_BadParm);
276 if (!normal)
277 StdCBase->flags |= ABNORMAL_EXIT;
279 *StdCBase->startup_errorptr = retcode;
281 longjmp(StdCBase->exit_jmpbuf, 1);
283 assert(0); /* Not reached */