libc.obj: add void abort(), int atexit( void (*func)(void) ), EXIT_ SUCCESS/FAILURE
Some checks failed
Build system / Build (pull_request) Failing after 1s
Build system / Check kernel codestyle (pull_request) Successful in 1m12s

This commit is contained in:
2026-01-10 23:20:18 +05:00
parent 0e4c573cb5
commit 8322ec954b
11 changed files with 117 additions and 12 deletions

View File

@@ -52,7 +52,10 @@ atol
atoll
atof
calloc
_exit
exit
atexit
abort
free
itoa
labs

View File

@@ -0,0 +1,6 @@
.vscode/*
.tup/*
*.o
*.kex
*.obj

View File

@@ -11,6 +11,10 @@
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define EXIT_SUCCESS 0 // Successful execution of a program
#define EXIT_FAILURE 1 // Unsuccessful execution of a program
typedef struct {
int quot;
int rem;
@@ -48,7 +52,12 @@ DLLAPI void free(void* ptr);
DLLAPI long int strtol(const char* str, char** endptr, int base);
DLLAPI void _exit(int status);
#define _Exit(status) _exit(status)
DLLAPI void abort();
DLLAPI void exit(int status);
DLLAPI int atexit( void (*func)(void) );
DLLAPI void srand(unsigned s);
DLLAPI int rand(void);

View File

@@ -27,17 +27,18 @@ BIN = \
libc_test.kex \
pipe.kex \
defgen.kex \
futex.kex
futex.kex \
atexit_test.kex
all: $(BIN)
%.kex : %.c
$(KTCC) $(CFLAGS) $(LDFLAGS) $< -o $@ $(LIBS)
$(KPACK) --nologo $@
# $(KPACK) --nologo $@
%.kex : %.asm
$(FASM) $< $@
$(KPACK) --nologo $@
# $(KPACK) --nologo $@
clean:
rm *.kex clayer/*.kex

View File

@@ -0,0 +1,18 @@
#include <stdlib.h>
#include <stdio.h>
void f()
{
static int c = 1;
printf("exit #%d\n", c);
c++;
}
int main()
{
atexit(&f);
atexit(&f);
atexit(&f);
exit(0);
}

View File

@@ -19,9 +19,6 @@ FILE* out = stdout;
FILE* out = stderr;
#endif
#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1
#define fprintf fprintf
void show_help()

View File

@@ -89,6 +89,8 @@
#include "stdlib/atol.c"
#include "stdlib/atoll.c"
#include "stdlib/calloc.c"
#include "stdlib/atexit.c" // должно быть до exit.c
#include "stdlib/_exit.c"
#include "stdlib/exit.c"
#include "stdlib/free.c"
#include "stdlib/itoa.c"
@@ -100,6 +102,7 @@
#include "stdlib/realloc.c"
#include "stdlib/strtod.c"
#include "stdlib/strtol.c"
#include "stdlib/abort.c"
#include "math/acosh.c"
#include "math/asinh.c"
@@ -170,6 +173,7 @@ ksys_dll_t EXPORTS[] = {
{ "atoll", &atoll },
{ "atof", &atof },
{ "calloc", &calloc },
{ "atexit", &atexit },
{ "exit", &exit },
{ "free", &free },
{ "itoa", &itoa },
@@ -178,6 +182,7 @@ ksys_dll_t EXPORTS[] = {
{ "malloc", &malloc },
{ "realloc", &realloc },
{ "strtol", &strtol },
{ "abort", &abort},
{ "srand", &srand },
{ "rand", &rand },
{ "qsort", &qsort },

View File

@@ -0,0 +1,16 @@
#include <conio.h>
#include <sys/ksys.h>
#include <stdio.h>
void _exit(int status)
{
if(status && status != 128)
{
printf("exit code: %d", status);
}
if (__con_is_load) {
con_exit(status);
}
_ksys_exit();
}

View File

@@ -0,0 +1,12 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/ksys.h>
void abort()
{
ksys_thread_t t;
_ksys_thread_info(&t, -1);
printf("\nAbort in %d\n", t.pid);
_exit(128);
}

View File

@@ -0,0 +1,38 @@
#include <stdlib.h>
struct atexit_n
{
struct atexit_n* last;
void (*func)(void);
};
struct atexit_n *__last_n = NULL;
int atexit( void (*func)(void) )
{
struct atexit_n* n = malloc(sizeof(struct atexit_n));
if(n == NULL)
{
return 1;
}
n->last = __last_n;
n->func = func;
__last_n = n;
return 0;
}
void __run_atexit()
{
struct atexit_n *n = __last_n;
while (n != NULL)
{
n->func();
struct atexit_n *to_free = n;
n = n->last;
free(to_free);
}
}

View File

@@ -1,12 +1,12 @@
/* Copyright (C) 2021 Logaev Maxim (turbocat2001), GPLv2 */
#include <conio.h>
#include <sys/ksys.h>
void exit(int status)
{
if (__con_is_load) {
con_exit(status);
}
_ksys_exit();
__run_atexit();
// fflush & delete tmp files
_exit(status);
}