2 Copyright © 2009-2013, The AROS Development Team. All rights reserved.
6 #include <exec/alerts.h>
7 #include <proto/exec.h>
13 #include <aros/debug.h>
15 #include "__stdc_intbase.h"
16 #include "__exitfunc.h"
18 /*****************************************************************************
21 void __stdc_program_startup(
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.
33 exitjmp - jmp_buf to jump to to exit the program
34 errorptr - pointer to store return value of program
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
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 /*****************************************************************************
69 void __stdc_program_end(
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.
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
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
));
110 etask
->et_Result1
= *StdCBase
->startup_errorptr
;
112 if (!(StdCBase
->flags
& ABNORMAL_EXIT
))
116 /*****************************************************************************
119 int *__stdc_set_errorptr(
125 This function sets the pointer to store error return value for
129 errorptr - new pointer to return value
132 old pointer to return value
144 ******************************************************************************/
146 struct StdCIntBase
*StdCBase
=
147 (struct StdCIntBase
*)__aros_getbase_StdCBase();
148 int *old
= StdCBase
->startup_errorptr
;
150 StdCBase
->startup_errorptr
= errorptr
;
155 /*****************************************************************************
158 int *__stdc_get_errorptr(
164 This function gets the pointer to store error return value for
171 pointer to return value
183 ******************************************************************************/
185 struct StdCIntBase
*StdCBase
=
186 (struct StdCIntBase
*)__aros_getbase_StdCBase();
187 return StdCBase
->startup_errorptr
;
190 /*****************************************************************************
193 void __stdc_set_exitjmp(
200 This function set the jmp_buf to use for directly exiting current
204 exitjmp - new jmp_buf for exiting
207 previous jmp_buf for exiting
219 ******************************************************************************/
221 struct StdCIntBase
*StdCBase
=
222 (struct StdCIntBase
*)__aros_getbase_StdCBase();
224 *previousjmp
= *StdCBase
->exit_jmpbuf
;
225 *StdCBase
->exit_jmpbuf
= *exitjmp
;
228 /*****************************************************************************
231 void __stdc_jmp2exit(
238 This function directly jumps to the exit of a program.
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.
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.
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
);
277 StdCBase
->flags
|= ABNORMAL_EXIT
;
279 *StdCBase
->startup_errorptr
= retcode
;
281 longjmp(StdCBase
->exit_jmpbuf
, 1);
283 assert(0); /* Not reached */