libc.obj: add void abort(), int atexit( void (*func)(void) ), EXIT_ SUCCESS/FAILURE
This commit is contained in:
@@ -52,7 +52,10 @@ atol
|
||||
atoll
|
||||
atof
|
||||
calloc
|
||||
_exit
|
||||
exit
|
||||
atexit
|
||||
abort
|
||||
free
|
||||
itoa
|
||||
labs
|
||||
|
||||
6
programs/develop/ktcc/trunk/libc.obj/.gitignore
vendored
Normal file
6
programs/develop/ktcc/trunk/libc.obj/.gitignore
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
.vscode/*
|
||||
.tup/*
|
||||
|
||||
*.o
|
||||
*.kex
|
||||
*.obj
|
||||
@@ -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);
|
||||
|
||||
@@ -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
|
||||
|
||||
18
programs/develop/ktcc/trunk/libc.obj/samples/atexit_test.c
Normal file
18
programs/develop/ktcc/trunk/libc.obj/samples/atexit_test.c
Normal 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);
|
||||
}
|
||||
@@ -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()
|
||||
|
||||
@@ -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 },
|
||||
|
||||
16
programs/develop/ktcc/trunk/libc.obj/source/stdlib/_exit.c
Normal file
16
programs/develop/ktcc/trunk/libc.obj/source/stdlib/_exit.c
Normal 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();
|
||||
}
|
||||
12
programs/develop/ktcc/trunk/libc.obj/source/stdlib/abort.c
Normal file
12
programs/develop/ktcc/trunk/libc.obj/source/stdlib/abort.c
Normal 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);
|
||||
}
|
||||
38
programs/develop/ktcc/trunk/libc.obj/source/stdlib/atexit.c
Normal file
38
programs/develop/ktcc/trunk/libc.obj/source/stdlib/atexit.c
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user