Files
Sprinter-SDCC/libc/sys/atexit.c
T
snark13 737c974400 Add mdview markdown viewer, reorganize tests/examples and libc layout
- Split tests/ (libc feature tests) and examples/ (real apps); shared
  app.mk in repo root, was examples/example.mk
- libc/io/* split into libc/{conio,env,errno,file,mouse,string,sys,
  time,video}/ — clearer module boundaries
- New examples/mdview/: markdown viewer (Phases 1-5 + light nested
  lists). Headers (H1-H4), HR, ulist/olist/quote with nesting via
  leading spaces, fenced code blocks, inline emphasis (bold/italic/
  underscore/code), wrap/unwrap mode with soft wrap (F2), horizontal
  pan (← →) with '>' truncation indicator
- libc additions: scroll() in conio (ESTEX SCROLL), strlwr/strupr,
  gets() test
- Makefile updates across tests/ for the new shared app.mk path

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-04 22:23:36 +03:00

58 lines
1.4 KiB
C

/*
* atexit + exit + _exit.
*
* atexit(fn) — register fn to be called at normal termination (max 8).
* exit(code) — run the atexit chain in LIFO, then ESTEX EXIT.
* _exit(code) — POSIX raw exit: skip the chain, go straight to ESTEX.
*
* These functions own termination entirely; crt0.s only does inline RST
* 10h #41 when main returns without an explicit exit(). That path skips
* the atexit chain — programs that need handlers should call exit() at
* the bottom of main (or return through a wrapper).
*/
#include <stdlib.h>
#include <sprinter_exit.h>
#define ATEXIT_MAX 8
static void (*atexit_stack[ATEXIT_MAX])(void);
static int atexit_top = 0;
int atexit(void (*fn)(void))
{
if (atexit_top >= ATEXIT_MAX) {
return -1;
}
atexit_stack[atexit_top++] = fn;
return 0;
}
/* exit() — runs the chain, then performs the raw ESTEX EXIT. */
void exit(int code)
{
while (atexit_top > 0) {
void (*fn)(void) = atexit_stack[--atexit_top];
if (fn) {
fn();
}
}
_exit(code); /* falls into the inline-asm raw exit below */
}
/* _exit() — POSIX raw termination, no atexit chain. */
void _exit(int code) __naked
{
(void)code;
__asm
;; HL = code (single int arg).
ld a, l
ld b, a
ld c, #0x41 ; ESTEX EXIT
rst #0x10
;; Should not return.
1$: halt
jr 1$
__endasm;
}