forked from KolibriOS/kolibrios
kolibri-libc:
Move to folder with tcc. Part 1 git-svn-id: svn://kolibrios.org@8793 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
28
programs/develop/ktcc/trunk/libc.obj/source/Makefile
Normal file
28
programs/develop/ktcc/trunk/libc.obj/source/Makefile
Normal file
@@ -0,0 +1,28 @@
|
||||
ifndef GCC
|
||||
GCC=kos32-gcc
|
||||
endif
|
||||
|
||||
KPACK=kpack
|
||||
FASM=fasm
|
||||
|
||||
CFLAGS = -c -nostdinc -I../include -DGNUC -D_BUILD_LIBC -fno-common -Os -fno-builtin -fno-leading-underscore -fno-pie
|
||||
|
||||
SRC=libc.c
|
||||
LIB=../lib/libc.obj
|
||||
|
||||
all:
|
||||
$(MAKE) -C ../linuxtools/src
|
||||
mkdir -p exports ../lib
|
||||
../linuxtools/ExportGen symbols.txt exports/exports.c
|
||||
$(FASM) crt/crt0.asm ../lib/crt0.o
|
||||
$(GCC) $(CFLAGS) $(SRC) -o $(LIB)
|
||||
$(KPACK) $(LIB)
|
||||
../linuxtools/LoaderGen symbols.txt ../loader
|
||||
../linuxtools/LoaderBuild ../loader
|
||||
$(MAKE) -C libtcc
|
||||
rm -rf exports
|
||||
install:
|
||||
cp -f ../lib/libc.obj ~/.kex/root/RD/1/LIB
|
||||
|
||||
clean:
|
||||
rm ../lib/*
|
13
programs/develop/ktcc/trunk/libc.obj/source/Tupfile.lua
Executable file
13
programs/develop/ktcc/trunk/libc.obj/source/Tupfile.lua
Executable file
@@ -0,0 +1,13 @@
|
||||
if tup.getconfig("NO_GCC") ~= "" then return end
|
||||
HELPERDIR = (tup.getconfig("HELPERDIR") == "") and "../../../../" or tup.getconfig("HELPERDIR")
|
||||
tup.include(HELPERDIR .. "/use_gcc.lua")
|
||||
|
||||
CFLAGS = " -c -w -nostdinc -DGNUC -D_BUILD_LIBC -Os -fno-common -fno-builtin -fno-leading-underscore -fno-pie"
|
||||
INCLUDES = " -I../include"
|
||||
|
||||
tup.rule("../linuxtools/src/ExportGen.c", "gcc %f -o %o" , "../linuxtools/ExportGen")
|
||||
tup.rule("../linuxtools/src/LoaderGen.c", "gcc %f -o %o" , "../linuxtools/LoaderGen")
|
||||
|
||||
tup.rule({"symbols.txt",extra_inputs = {"../linuxtools/ExportGen"}}, "../linuxtools/ExportGen %f %o" , "exports/exports.c")
|
||||
|
||||
tup.rule({"libc.c",extra_inputs = {"exports/exports.c"}} , "kos32-gcc" .. CFLAGS .. INCLUDES .. " -o %o %f " .. tup.getconfig("KPACK_CMD"), "libc.obj")
|
221
programs/develop/ktcc/trunk/libc.obj/source/crt/crt0.asm
Normal file
221
programs/develop/ktcc/trunk/libc.obj/source/crt/crt0.asm
Normal file
@@ -0,0 +1,221 @@
|
||||
format ELF
|
||||
section '.text' executable
|
||||
public start
|
||||
public start as '_start'
|
||||
;extrn mf_init
|
||||
extrn main
|
||||
include '../../../../../proc32.inc'
|
||||
include '../../../../../macros.inc'
|
||||
include '../../../../../dll.inc'
|
||||
;include '../../../../../debug.inc'
|
||||
|
||||
;start_:
|
||||
virtual at 0
|
||||
db 'MENUET01' ; 1. Magic number (8 bytes)
|
||||
dd 0x01 ; 2. Version of executable file
|
||||
dd start ; 3. Start address
|
||||
imgsz dd 0x0 ; 4. Size of image
|
||||
dd 0x100000 ; 5. Size of needed memory
|
||||
dd 0x100000 ; 6. Pointer to stack
|
||||
hparams dd 0x0 ; 7. Pointer to program arguments
|
||||
hpath dd 0x0 ; 8. Pointer to program path
|
||||
end virtual
|
||||
|
||||
start:
|
||||
;DEBUGF 'Start programm\n'
|
||||
;init heap of memory
|
||||
mov eax,68
|
||||
mov ebx,11
|
||||
int 0x40
|
||||
|
||||
mov [argc], 0
|
||||
mov eax, [hparams]
|
||||
test eax, eax
|
||||
jz .without_path
|
||||
mov eax, path
|
||||
cmp word ptr eax, 32fh ; '/#3' UTF8
|
||||
jne .without_path
|
||||
mov word ptr eax, 12fh ; '/#1' fix to CP866
|
||||
.without_path:
|
||||
mov esi, eax
|
||||
call push_param
|
||||
; retrieving parameters
|
||||
mov esi, params
|
||||
xor edx, edx ; dl - <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(1) <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(0)
|
||||
; dh - <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> (1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, 0 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
|
||||
mov ecx, 1 ; cl = 1
|
||||
; ch = 0 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>
|
||||
.parse:
|
||||
lodsb
|
||||
test al, al
|
||||
jz .run
|
||||
test dl, dl
|
||||
jnz .findendparam
|
||||
;{<7B><><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
cmp al, ' '
|
||||
jz .parse ;<3B><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
mov dl, cl ;<3B><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
cmp al, '"'
|
||||
jz @f ;<3B><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
mov dh, ch ;<3B><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
dec esi
|
||||
call push_param
|
||||
inc esi
|
||||
jmp .parse
|
||||
|
||||
@@:
|
||||
mov dh, cl ;<3B><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
call push_param ;<3B><><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
jmp .parse ;<3B><><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>}
|
||||
|
||||
.findendparam:
|
||||
test dh, dh
|
||||
jz @f ; <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
cmp al, '"'
|
||||
jz .clear
|
||||
jmp .parse
|
||||
@@:
|
||||
cmp al, ' '
|
||||
jnz .parse
|
||||
|
||||
.clear:
|
||||
lea ebx, [esi - 1]
|
||||
mov [ebx], ch
|
||||
mov dl, ch
|
||||
jmp .parse
|
||||
|
||||
.run:
|
||||
call load_imports
|
||||
push argv
|
||||
push [argc]
|
||||
call main
|
||||
.exit:
|
||||
xor eax,eax
|
||||
dec eax
|
||||
int 0x40
|
||||
dd -1
|
||||
.crash:
|
||||
jmp .exit
|
||||
;============================
|
||||
push_param:
|
||||
;============================
|
||||
;parameters
|
||||
; esi - pointer
|
||||
;description
|
||||
; procedure increase argc
|
||||
; and add pointer to array argv
|
||||
; procedure changes ebx
|
||||
mov ebx, [argc]
|
||||
cmp ebx, max_parameters
|
||||
jae .dont_add
|
||||
mov [argv+4*ebx], esi
|
||||
inc [argc]
|
||||
.dont_add:
|
||||
ret
|
||||
;==============================
|
||||
|
||||
;==============================
|
||||
load_imports:
|
||||
;==============================
|
||||
;parameters
|
||||
; none
|
||||
;description
|
||||
; imports must be located at end of image (but before BSS sections)
|
||||
; the address of end of imports (next byte after imports) is located in imgsz
|
||||
; look at each import from that address up to illegal import
|
||||
; legal import is such that:
|
||||
; first pointer points to procedure name
|
||||
; and is smaller than imgsz
|
||||
; second pointer points lo library name, starting with 0x55, 0xAA
|
||||
; and is smaller than imgsz
|
||||
; each library should be initialized as appropriate, once
|
||||
; so as library is initialized, its name will be replaced 0x00
|
||||
mov ebx, [imgsz] ; byte after imports
|
||||
.handle_next_import:
|
||||
sub ebx, 4 ; ebx = pointer to pointer to library name
|
||||
mov esi, dword[ebx] ; esi = pointer to library name
|
||||
push ebx
|
||||
push esi
|
||||
call load_library ; eax = pointer to library exports
|
||||
pop esi
|
||||
pop ebx
|
||||
test eax, eax
|
||||
jz .done
|
||||
sub ebx, 4 ; ebx = pointer to pointer to symbol name
|
||||
push ebx
|
||||
stdcall dll.GetProcAddress, eax, dword[ebx]
|
||||
pop ebx
|
||||
test eax, eax
|
||||
jz .fail
|
||||
mov dword[ebx], eax
|
||||
jmp .handle_next_import
|
||||
.done:
|
||||
;DEBUGF 1, "Library: %s not loaded!\n", esi
|
||||
;mcall -1
|
||||
ret
|
||||
.fail:
|
||||
ret
|
||||
|
||||
;==============================
|
||||
|
||||
;==============================
|
||||
load_library:
|
||||
;==============================
|
||||
;parameters
|
||||
; ebx: library name address
|
||||
;description
|
||||
; each library should be initialized as appropriate, once
|
||||
; so as library is initialized, its name will be replaced 0x00
|
||||
; and 4 next bytes will be set to address of library
|
||||
; first two bytes of library name must be 0x55, 0xAA (is like a magic)
|
||||
cld ; move esi further, not back
|
||||
cmp esi, [imgsz]
|
||||
ja .fail
|
||||
lodsb ; al = first byte of library name
|
||||
cmp al, 0x55
|
||||
jne .fail
|
||||
lodsb ; al = second byte of library name
|
||||
cmp al, 0xAA
|
||||
jne .fail
|
||||
lodsb ; al = third byte of library name (0x00 if the library is already loaded)
|
||||
test al, al
|
||||
jnz .load
|
||||
lodsd ; if we here, then third byte is 0x00 => address of library is in next 4 bytes
|
||||
; now eax contains address of library
|
||||
ret
|
||||
.load:
|
||||
dec esi ; we checked on 0 before, let's go back
|
||||
mov eax, 68
|
||||
mov ebx, 19
|
||||
mov ecx, esi
|
||||
int 0x40 ; eax = address of exports
|
||||
mov byte[esi], 0 ; library is loaded, let's place 0 in first byte of name
|
||||
mov [esi + 1], eax ; now next 4 bytes of library name are replaced by address of library
|
||||
; call lib_init
|
||||
stdcall dll.GetProcAddress, eax, lib_init_str ; eax = address of lib_init
|
||||
test eax, eax
|
||||
jz .ret
|
||||
stdcall dll.Init, eax
|
||||
.ret:
|
||||
mov eax, [esi + 1] ; put address of library into eax
|
||||
ret
|
||||
.fail:
|
||||
mov eax, 0
|
||||
ret
|
||||
|
||||
;==============================
|
||||
|
||||
lib_init_str db 'lib_init', 0
|
||||
|
||||
public argc as '__argc'
|
||||
public params as '__argv'
|
||||
public path as '__path'
|
||||
|
||||
section '.bss'
|
||||
buf_len = 0x400
|
||||
max_parameters=0x20
|
||||
argc rd 1
|
||||
argv rd max_parameters
|
||||
path rb buf_len
|
||||
params rb buf_len
|
||||
|
21
programs/develop/ktcc/trunk/libc.obj/source/ctype/is.c
Normal file
21
programs/develop/ktcc/trunk/libc.obj/source/ctype/is.c
Normal file
@@ -0,0 +1,21 @@
|
||||
#include <ctype.h>
|
||||
|
||||
unsigned short __is[129] = {
|
||||
0, /* EOF */
|
||||
0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
|
||||
0x004, 0x104, 0x104, 0x104, 0x104, 0x104, 0x004, 0x004,
|
||||
0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
|
||||
0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
|
||||
0x140, 0x0D0, 0x0D0, 0x0D0, 0x0D0, 0x0D0, 0x0D0, 0x0D0,
|
||||
0x0D0, 0x0D0, 0x0D0, 0x0D0, 0x0D0, 0x0D0, 0x0D0, 0x0D0,
|
||||
0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459,
|
||||
0x459, 0x459, 0x0D0, 0x0D0, 0x0D0, 0x0D0, 0x0D0, 0x0D0,
|
||||
0x0D0, 0x653, 0x653, 0x653, 0x653, 0x653, 0x653, 0x253,
|
||||
0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253,
|
||||
0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253,
|
||||
0x253, 0x253, 0x253, 0x0D0, 0x0D0, 0x0D0, 0x0D0, 0x0D0,
|
||||
0x0D0, 0x473, 0x473, 0x473, 0x473, 0x473, 0x473, 0x073,
|
||||
0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073,
|
||||
0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073,
|
||||
0x073, 0x073, 0x073, 0x0D0, 0x0D0, 0x0D0, 0x0D0, 0x004
|
||||
};
|
@@ -0,0 +1,7 @@
|
||||
#include <ctype.h>
|
||||
|
||||
int tolower(int c)
|
||||
{
|
||||
if (isupper(c)) return c | 32;
|
||||
return c;
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
#include <ctype.h>
|
||||
|
||||
int toupper(int c)
|
||||
{
|
||||
if (islower(c)) return c & 0x5f;
|
||||
return c;
|
||||
}
|
145
programs/develop/ktcc/trunk/libc.obj/source/libc.c
Normal file
145
programs/develop/ktcc/trunk/libc.obj/source/libc.c
Normal file
@@ -0,0 +1,145 @@
|
||||
#include "ctype/is.c"
|
||||
#include "ctype/tolower.c"
|
||||
#include "ctype/toupper.c"
|
||||
|
||||
#include "sys/rewinddir.c"
|
||||
#include "sys/readdir.c"
|
||||
#include "sys/seekdir.c"
|
||||
#include "sys/opendir.c"
|
||||
#include "sys/telldir.c"
|
||||
#include "sys/closedir.c"
|
||||
#include "sys/dir.c"
|
||||
#include "sys/socket.c"
|
||||
|
||||
#include "stdio/clearerr.c"
|
||||
#include "stdio/gets.c"
|
||||
#include "stdio/setbuf.c"
|
||||
#include "stdio/fgetc.c"
|
||||
#include "stdio/fopen.c"
|
||||
#include "stdio/format_print.c"
|
||||
#include "stdio/vprintf.c"
|
||||
#include "stdio/feof.c"
|
||||
#include "stdio/fwrite.c"
|
||||
#include "stdio/fread.c"
|
||||
#include "stdio/fseek.c"
|
||||
#include "stdio/fgetpos.c"
|
||||
#include "stdio/fclose.c"
|
||||
#include "stdio/snprintf.c"
|
||||
#include "stdio/rename.c"
|
||||
#include "stdio/getchar.c"
|
||||
#include "stdio/remove.c"
|
||||
#include "stdio/ferror.c"
|
||||
#include "stdio/tmpfile.c"
|
||||
#include "stdio/fputs.c"
|
||||
#include "stdio/fputc.c"
|
||||
#include "stdio/fgets.c"
|
||||
#include "stdio/fflush.c"
|
||||
#include "stdio/format_scan.c"
|
||||
#include "stdio/printf.c"
|
||||
#include "stdio/fscanf.c"
|
||||
#include "stdio/debug_printf.c"
|
||||
#include "stdio/fsetpos.c"
|
||||
#include "stdio/setvbuf.c"
|
||||
#include "stdio/sscanf.c"
|
||||
#include "stdio/scanf.c"
|
||||
#include "stdio/freopen.c"
|
||||
#include "stdio/puts.c"
|
||||
#include "stdio/sprintf.c"
|
||||
#include "stdio/vsnprintf.c"
|
||||
#include "stdio/conio.c"
|
||||
#include "stdio/perror.c"
|
||||
#include "stdio/ftell.c"
|
||||
#include "stdio/tmpnam.c"
|
||||
#include "stdio/rewind.c"
|
||||
#include "stdio/vfprintf.c"
|
||||
#include "stdio/fprintf.c"
|
||||
#include "stdio/ungetc.c"
|
||||
|
||||
#include "string/strerror.c"
|
||||
#include "string/strxfrm.c"
|
||||
#include "string/strrchr.c"
|
||||
#include "string/strcspn.c"
|
||||
#include "string/strlen.c"
|
||||
#include "string/strrev.c"
|
||||
#include "string/memccpy.c"
|
||||
#include "string/strchr.c"
|
||||
#include "string/strcoll.c"
|
||||
#include "string/strpbrk.c"
|
||||
#include "string/strstr.c"
|
||||
#include "string/memcmp.c"
|
||||
#include "string/strspn.c"
|
||||
#include "string/strcpy.c"
|
||||
#include "string/strncpy.c"
|
||||
#include "string/strdup.c"
|
||||
#include "string/strcat.c"
|
||||
#include "string/memchr.c"
|
||||
#include "string/strncmp.c"
|
||||
#include "string/strncat.c"
|
||||
#include "string/strtok.c"
|
||||
#include "string/strcmp.c"
|
||||
#include "string/memset.c"
|
||||
#include "string/memcpy.c"
|
||||
#include "string/memmove.c"
|
||||
|
||||
#include "stdlib/calloc.c"
|
||||
#include "stdlib/lldiv.c"
|
||||
#include "stdlib/malloc.c"
|
||||
#include "stdlib/atoll.c"
|
||||
#include "stdlib/free.c"
|
||||
#include "stdlib/llabs.c"
|
||||
#include "stdlib/exit.c"
|
||||
#include "stdlib/atoi.c"
|
||||
#include "stdlib/labs.c"
|
||||
#include "stdlib/realloc.c"
|
||||
#include "stdlib/ldiv.c"
|
||||
#include "stdlib/abs.c"
|
||||
#include "stdlib/div.c"
|
||||
#include "stdlib/atol.c"
|
||||
#include "stdlib/itoa.c"
|
||||
#include "stdlib/strtol.c"
|
||||
#include "stdlib/rand.c"
|
||||
|
||||
#include "math/acosh.c"
|
||||
#include "math/asinh.c"
|
||||
#include "math/atanh.c"
|
||||
#include "math/cosh.c"
|
||||
#include "math/frexp.c"
|
||||
#include "math/hypot.c"
|
||||
#include "math/ldexp.c"
|
||||
#include "math/sinh.c"
|
||||
#include "math/tanh.c"
|
||||
|
||||
#include "time/difftime.c"
|
||||
#include "time/localtime.c"
|
||||
#include "time/mktime.c"
|
||||
#include "time/time.c"
|
||||
#include "time/asctime.c"
|
||||
|
||||
__asm__(
|
||||
".include \"math/acos.s\"\n\t"
|
||||
".include \"math/asin.s\"\n\t"
|
||||
".include \"math/atan.s\"\n\t"
|
||||
".include \"math/atan2.s\"\n\t"
|
||||
".include \"math/ceil.s\"\n\t"
|
||||
".include \"math/cos.s\"\n\t"
|
||||
".include \"math/exp.s\"\n\t"
|
||||
".include \"math/fabs.s\"\n\t"
|
||||
".include \"math/floor.s\"\n\t"
|
||||
".include \"math/fmod.s\"\n\t"
|
||||
".include \"math/log.s\"\n\t"
|
||||
".include \"math/modf.s\"\n\t"
|
||||
".include \"math/modfl.s\"\n\t"
|
||||
".include \"math/pow.s\"\n\t"
|
||||
".include \"math/pow2.s\"\n\t"
|
||||
".include \"math/pow10.s\"\n\t"
|
||||
".include \"math/sqrt.s\"\n\t"
|
||||
);
|
||||
|
||||
__asm__(
|
||||
".include \"setjmp/longjmp.s\"\n\t"
|
||||
".include \"setjmp/setjmp.s\""
|
||||
);
|
||||
|
||||
#include "libtcc/libtcc1.c"
|
||||
#include "stdlib/___chkstk_ms.c"
|
||||
#include "exports/exports.c"
|
17
programs/develop/ktcc/trunk/libc.obj/source/libtcc/Makefile
Executable file
17
programs/develop/ktcc/trunk/libc.obj/source/libtcc/Makefile
Executable file
@@ -0,0 +1,17 @@
|
||||
KTCC_DIR=../../../../ktcc/trunk
|
||||
|
||||
KTCC=$(KTCC_DIR)/bin/kos32-tcc
|
||||
FASM=fasm
|
||||
|
||||
OBJ= memcpy.o memmove.o memset.o libtcc1.o
|
||||
|
||||
all: $(OBJ)
|
||||
ar -crs ../../lib/libtcc.a $(OBJ)
|
||||
rm -f *.o
|
||||
|
||||
%.o : %.c
|
||||
$(KTCC) -c $< -o $@
|
||||
|
||||
%.o : %.asm
|
||||
$(FASM) $< $@
|
||||
|
763
programs/develop/ktcc/trunk/libc.obj/source/libtcc/libtcc1.c
Normal file
763
programs/develop/ktcc/trunk/libc.obj/source/libtcc/libtcc1.c
Normal file
@@ -0,0 +1,763 @@
|
||||
/* TCC runtime library.
|
||||
Parts of this code are (c) 2002 Fabrice Bellard
|
||||
|
||||
Copyright (C) 1987, 1988, 1992, 1994, 1995 Free Software Foundation, Inc.
|
||||
|
||||
This file is free software; you can redistribute it and/or modify it
|
||||
under the terms of the GNU General Public License as published by the
|
||||
Free Software Foundation; either version 2, or (at your option) any
|
||||
later version.
|
||||
|
||||
In addition to the permissions in the GNU General Public License, the
|
||||
Free Software Foundation gives you unlimited permission to link the
|
||||
compiled version of this file into combinations with other programs,
|
||||
and to distribute those combinations without any restriction coming
|
||||
from the use of this file. (The General Public License restrictions
|
||||
do apply in other respects; for example, they cover modification of
|
||||
the file, and distribution when not linked into a combine
|
||||
executable.)
|
||||
|
||||
This file is distributed in the hope that it will be useful, but
|
||||
WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not, write to
|
||||
the Free Software Foundation, 59 Temple Place - Suite 330,
|
||||
Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
|
||||
//#include <stdint.h>
|
||||
#define TCC_TARGET_I386
|
||||
|
||||
#define W_TYPE_SIZE 32
|
||||
#define BITS_PER_UNIT 8
|
||||
|
||||
typedef int Wtype;
|
||||
typedef unsigned int UWtype;
|
||||
typedef unsigned int USItype;
|
||||
typedef long long DWtype;
|
||||
typedef unsigned long long UDWtype;
|
||||
|
||||
struct DWstruct {
|
||||
Wtype low, high;
|
||||
};
|
||||
|
||||
typedef union
|
||||
{
|
||||
struct DWstruct s;
|
||||
DWtype ll;
|
||||
} DWunion;
|
||||
|
||||
typedef long double XFtype;
|
||||
#define WORD_SIZE (sizeof (Wtype) * BITS_PER_UNIT)
|
||||
#define HIGH_WORD_COEFF (((UDWtype) 1) << WORD_SIZE)
|
||||
|
||||
/* the following deal with IEEE single-precision numbers */
|
||||
#define EXCESS 126
|
||||
#define SIGNBIT 0x80000000
|
||||
#define HIDDEN (1 << 23)
|
||||
#define SIGN(fp) ((fp) & SIGNBIT)
|
||||
#define EXP(fp) (((fp) >> 23) & 0xFF)
|
||||
#define MANT(fp) (((fp) & 0x7FFFFF) | HIDDEN)
|
||||
#define PACK(s,e,m) ((s) | ((e) << 23) | (m))
|
||||
|
||||
/* the following deal with IEEE double-precision numbers */
|
||||
#define EXCESSD 1022
|
||||
#define HIDDEND (1 << 20)
|
||||
#define EXPD(fp) (((fp.l.upper) >> 20) & 0x7FF)
|
||||
#define SIGND(fp) ((fp.l.upper) & SIGNBIT)
|
||||
#define MANTD(fp) (((((fp.l.upper) & 0xFFFFF) | HIDDEND) << 10) | \
|
||||
(fp.l.lower >> 22))
|
||||
#define HIDDEND_LL ((long long)1 << 52)
|
||||
#define MANTD_LL(fp) ((fp.ll & (HIDDEND_LL-1)) | HIDDEND_LL)
|
||||
#define PACKD_LL(s,e,m) (((long long)((s)+((e)<<20))<<32)|(m))
|
||||
|
||||
/* the following deal with x86 long double-precision numbers */
|
||||
#define EXCESSLD 16382
|
||||
#define EXPLD(fp) (fp.l.upper & 0x7fff)
|
||||
#define SIGNLD(fp) ((fp.l.upper) & 0x8000)
|
||||
|
||||
/* only for x86 */
|
||||
union ldouble_long {
|
||||
long double ld;
|
||||
struct {
|
||||
unsigned long long lower;
|
||||
unsigned short upper;
|
||||
} l;
|
||||
};
|
||||
|
||||
union double_long {
|
||||
double d;
|
||||
#if 1
|
||||
struct {
|
||||
unsigned int lower;
|
||||
int upper;
|
||||
} l;
|
||||
#else
|
||||
struct {
|
||||
int upper;
|
||||
unsigned int lower;
|
||||
} l;
|
||||
#endif
|
||||
long long ll;
|
||||
};
|
||||
|
||||
union float_long {
|
||||
float f;
|
||||
unsigned int l;
|
||||
};
|
||||
|
||||
/* XXX: we don't support several builtin supports for now */
|
||||
#if !defined(TCC_TARGET_X86_64) && !defined(TCC_TARGET_ARM)
|
||||
|
||||
/* XXX: use gcc/tcc intrinsic ? */
|
||||
#if defined(TCC_TARGET_I386)
|
||||
#define sub_ddmmss(sh, sl, ah, al, bh, bl) \
|
||||
__asm__ ("subl %5,%1\n\tsbbl %3,%0" \
|
||||
: "=r" ((USItype) (sh)), \
|
||||
"=&r" ((USItype) (sl)) \
|
||||
: "0" ((USItype) (ah)), \
|
||||
"g" ((USItype) (bh)), \
|
||||
"1" ((USItype) (al)), \
|
||||
"g" ((USItype) (bl)))
|
||||
#define umul_ppmm(w1, w0, u, v) \
|
||||
__asm__ ("mull %3" \
|
||||
: "=a" ((USItype) (w0)), \
|
||||
"=d" ((USItype) (w1)) \
|
||||
: "%0" ((USItype) (u)), \
|
||||
"rm" ((USItype) (v)))
|
||||
#define udiv_qrnnd(q, r, n1, n0, dv) \
|
||||
__asm__ ("divl %4" \
|
||||
: "=a" ((USItype) (q)), \
|
||||
"=d" ((USItype) (r)) \
|
||||
: "0" ((USItype) (n0)), \
|
||||
"1" ((USItype) (n1)), \
|
||||
"rm" ((USItype) (dv)))
|
||||
#define count_leading_zeros(count, x) \
|
||||
do { \
|
||||
USItype __cbtmp; \
|
||||
__asm__ ("bsrl %1,%0" \
|
||||
: "=r" (__cbtmp) : "rm" ((USItype) (x))); \
|
||||
(count) = __cbtmp ^ 31; \
|
||||
} while (0)
|
||||
#else
|
||||
#error unsupported CPU type
|
||||
#endif
|
||||
|
||||
/* most of this code is taken from libgcc2.c from gcc */
|
||||
UDWtype __udivmoddi4 (UDWtype n, UDWtype d, UDWtype *rp)
|
||||
{
|
||||
DWunion ww;
|
||||
DWunion nn, dd;
|
||||
DWunion rr;
|
||||
UWtype d0, d1, n0, n1, n2;
|
||||
UWtype q0, q1;
|
||||
UWtype b, bm;
|
||||
|
||||
nn.ll = n;
|
||||
dd.ll = d;
|
||||
|
||||
d0 = dd.s.low;
|
||||
d1 = dd.s.high;
|
||||
n0 = nn.s.low;
|
||||
n1 = nn.s.high;
|
||||
|
||||
#if !defined(UDIV_NEEDS_NORMALIZATION)
|
||||
if (d1 == 0)
|
||||
{
|
||||
if (d0 > n1)
|
||||
{
|
||||
/* 0q = nn / 0D */
|
||||
|
||||
udiv_qrnnd (q0, n0, n1, n0, d0);
|
||||
q1 = 0;
|
||||
|
||||
/* Remainder in n0. */
|
||||
}
|
||||
else
|
||||
{
|
||||
/* qq = NN / 0d */
|
||||
|
||||
if (d0 == 0)
|
||||
d0 = 1 / d0; /* Divide intentionally by zero. */
|
||||
|
||||
udiv_qrnnd (q1, n1, 0, n1, d0);
|
||||
udiv_qrnnd (q0, n0, n1, n0, d0);
|
||||
|
||||
/* Remainder in n0. */
|
||||
}
|
||||
|
||||
if (rp != 0)
|
||||
{
|
||||
rr.s.low = n0;
|
||||
rr.s.high = 0;
|
||||
*rp = rr.ll;
|
||||
}
|
||||
}
|
||||
|
||||
#else /* UDIV_NEEDS_NORMALIZATION */
|
||||
|
||||
if (d1 == 0)
|
||||
{
|
||||
if (d0 > n1)
|
||||
{
|
||||
/* 0q = nn / 0D */
|
||||
|
||||
count_leading_zeros (bm, d0);
|
||||
|
||||
if (bm != 0)
|
||||
{
|
||||
/* Normalize, i.e. make the most significant bit of the
|
||||
denominator set. */
|
||||
|
||||
d0 = d0 << bm;
|
||||
n1 = (n1 << bm) | (n0 >> (W_TYPE_SIZE - bm));
|
||||
n0 = n0 << bm;
|
||||
}
|
||||
|
||||
udiv_qrnnd (q0, n0, n1, n0, d0);
|
||||
q1 = 0;
|
||||
|
||||
/* Remainder in n0 >> bm. */
|
||||
}
|
||||
else
|
||||
{
|
||||
/* qq = NN / 0d */
|
||||
|
||||
if (d0 == 0)
|
||||
d0 = 1 / d0; /* Divide intentionally by zero. */
|
||||
|
||||
count_leading_zeros (bm, d0);
|
||||
|
||||
if (bm == 0)
|
||||
{
|
||||
/* From (n1 >= d0) /\ (the most significant bit of d0 is set),
|
||||
conclude (the most significant bit of n1 is set) /\ (the
|
||||
leading quotient digit q1 = 1).
|
||||
|
||||
This special case is necessary, not an optimization.
|
||||
(Shifts counts of W_TYPE_SIZE are undefined.) */
|
||||
|
||||
n1 -= d0;
|
||||
q1 = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Normalize. */
|
||||
|
||||
b = W_TYPE_SIZE - bm;
|
||||
|
||||
d0 = d0 << bm;
|
||||
n2 = n1 >> b;
|
||||
n1 = (n1 << bm) | (n0 >> b);
|
||||
n0 = n0 << bm;
|
||||
|
||||
udiv_qrnnd (q1, n1, n2, n1, d0);
|
||||
}
|
||||
|
||||
/* n1 != d0... */
|
||||
|
||||
udiv_qrnnd (q0, n0, n1, n0, d0);
|
||||
|
||||
/* Remainder in n0 >> bm. */
|
||||
}
|
||||
|
||||
if (rp != 0)
|
||||
{
|
||||
rr.s.low = n0 >> bm;
|
||||
rr.s.high = 0;
|
||||
*rp = rr.ll;
|
||||
}
|
||||
}
|
||||
#endif /* UDIV_NEEDS_NORMALIZATION */
|
||||
|
||||
else
|
||||
{
|
||||
if (d1 > n1)
|
||||
{
|
||||
/* 00 = nn / DD */
|
||||
|
||||
q0 = 0;
|
||||
q1 = 0;
|
||||
|
||||
/* Remainder in n1n0. */
|
||||
if (rp != 0)
|
||||
{
|
||||
rr.s.low = n0;
|
||||
rr.s.high = n1;
|
||||
*rp = rr.ll;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* 0q = NN / dd */
|
||||
|
||||
count_leading_zeros (bm, d1);
|
||||
if (bm == 0)
|
||||
{
|
||||
/* From (n1 >= d1) /\ (the most significant bit of d1 is set),
|
||||
conclude (the most significant bit of n1 is set) /\ (the
|
||||
quotient digit q0 = 0 or 1).
|
||||
|
||||
This special case is necessary, not an optimization. */
|
||||
|
||||
/* The condition on the next line takes advantage of that
|
||||
n1 >= d1 (true due to program flow). */
|
||||
if (n1 > d1 || n0 >= d0)
|
||||
{
|
||||
q0 = 1;
|
||||
sub_ddmmss (n1, n0, n1, n0, d1, d0);
|
||||
}
|
||||
else
|
||||
q0 = 0;
|
||||
|
||||
q1 = 0;
|
||||
|
||||
if (rp != 0)
|
||||
{
|
||||
rr.s.low = n0;
|
||||
rr.s.high = n1;
|
||||
*rp = rr.ll;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
UWtype m1, m0;
|
||||
/* Normalize. */
|
||||
|
||||
b = W_TYPE_SIZE - bm;
|
||||
|
||||
d1 = (d1 << bm) | (d0 >> b);
|
||||
d0 = d0 << bm;
|
||||
n2 = n1 >> b;
|
||||
n1 = (n1 << bm) | (n0 >> b);
|
||||
n0 = n0 << bm;
|
||||
|
||||
udiv_qrnnd (q0, n1, n2, n1, d1);
|
||||
umul_ppmm (m1, m0, q0, d0);
|
||||
|
||||
if (m1 > n1 || (m1 == n1 && m0 > n0))
|
||||
{
|
||||
q0--;
|
||||
sub_ddmmss (m1, m0, m1, m0, d1, d0);
|
||||
}
|
||||
|
||||
q1 = 0;
|
||||
|
||||
/* Remainder in (n1n0 - m1m0) >> bm. */
|
||||
if (rp != 0)
|
||||
{
|
||||
sub_ddmmss (n1, n0, n1, n0, m1, m0);
|
||||
rr.s.low = (n1 << b) | (n0 >> bm);
|
||||
rr.s.high = n1 >> bm;
|
||||
*rp = rr.ll;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ww.s.low = q0;
|
||||
ww.s.high = q1;
|
||||
return ww.ll;
|
||||
}
|
||||
|
||||
#define __negdi2(a) (-(a))
|
||||
|
||||
long long __divdi3(long long u, long long v)
|
||||
{
|
||||
int c = 0;
|
||||
DWunion uu, vv;
|
||||
DWtype w;
|
||||
|
||||
uu.ll = u;
|
||||
vv.ll = v;
|
||||
|
||||
if (uu.s.high < 0) {
|
||||
c = ~c;
|
||||
uu.ll = __negdi2 (uu.ll);
|
||||
}
|
||||
if (vv.s.high < 0) {
|
||||
c = ~c;
|
||||
vv.ll = __negdi2 (vv.ll);
|
||||
}
|
||||
w = __udivmoddi4 (uu.ll, vv.ll, (UDWtype *) 0);
|
||||
if (c)
|
||||
w = __negdi2 (w);
|
||||
return w;
|
||||
}
|
||||
|
||||
// https://github.com/KaMeHb-UA/UE4m/blob/1d9ad5bfead06520570c7f24dad062f9f8717c1a/\
|
||||
Engine/Extras/ThirdPartyNotUE/emsdk/emscripten/incoming/system/lib/compiler-rt/lib/\
|
||||
builtins/divmoddi4.c
|
||||
long long __divmoddi4(long long a, long long b, long long* rem)
|
||||
{
|
||||
long long d = __divdi3(a, b);
|
||||
*rem = a - (d * b);
|
||||
return d;
|
||||
}
|
||||
|
||||
long long __moddi3(long long u, long long v)
|
||||
{
|
||||
int c = 0;
|
||||
DWunion uu, vv;
|
||||
DWtype w;
|
||||
|
||||
uu.ll = u;
|
||||
vv.ll = v;
|
||||
|
||||
if (uu.s.high < 0) {
|
||||
c = ~c;
|
||||
uu.ll = __negdi2 (uu.ll);
|
||||
}
|
||||
if (vv.s.high < 0)
|
||||
vv.ll = __negdi2 (vv.ll);
|
||||
|
||||
__udivmoddi4 (uu.ll, vv.ll, (UDWtype *) &w);
|
||||
if (c)
|
||||
w = __negdi2 (w);
|
||||
return w;
|
||||
}
|
||||
|
||||
unsigned long long __udivdi3(unsigned long long u, unsigned long long v)
|
||||
{
|
||||
return __udivmoddi4 (u, v, (UDWtype *) 0);
|
||||
}
|
||||
|
||||
unsigned long long __umoddi3(unsigned long long u, unsigned long long v)
|
||||
{
|
||||
UDWtype w;
|
||||
|
||||
__udivmoddi4 (u, v, &w);
|
||||
return w;
|
||||
}
|
||||
|
||||
/* XXX: fix tcc's code generator to do this instead */
|
||||
long long __ashrdi3(long long a, int b)
|
||||
{
|
||||
#ifdef __TINYC__
|
||||
DWunion u;
|
||||
u.ll = a;
|
||||
if (b >= 32) {
|
||||
u.s.low = u.s.high >> (b - 32);
|
||||
u.s.high = u.s.high >> 31;
|
||||
} else if (b != 0) {
|
||||
u.s.low = ((unsigned)u.s.low >> b) | (u.s.high << (32 - b));
|
||||
u.s.high = u.s.high >> b;
|
||||
}
|
||||
return u.ll;
|
||||
#else
|
||||
return a >> b;
|
||||
#endif
|
||||
}
|
||||
|
||||
/* XXX: fix tcc's code generator to do this instead */
|
||||
unsigned long long __lshrdi3(unsigned long long a, int b)
|
||||
{
|
||||
#ifdef __TINYC__
|
||||
DWunion u;
|
||||
u.ll = a;
|
||||
if (b >= 32) {
|
||||
u.s.low = (unsigned)u.s.high >> (b - 32);
|
||||
u.s.high = 0;
|
||||
} else if (b != 0) {
|
||||
u.s.low = ((unsigned)u.s.low >> b) | (u.s.high << (32 - b));
|
||||
u.s.high = (unsigned)u.s.high >> b;
|
||||
}
|
||||
return u.ll;
|
||||
#else
|
||||
return a >> b;
|
||||
#endif
|
||||
}
|
||||
|
||||
/* XXX: fix tcc's code generator to do this instead */
|
||||
long long __ashldi3(long long a, int b)
|
||||
{
|
||||
#ifdef __TINYC__
|
||||
DWunion u;
|
||||
u.ll = a;
|
||||
if (b >= 32) {
|
||||
u.s.high = (unsigned)u.s.low << (b - 32);
|
||||
u.s.low = 0;
|
||||
} else if (b != 0) {
|
||||
u.s.high = ((unsigned)u.s.high << b) | ((unsigned)u.s.low >> (32 - b));
|
||||
u.s.low = (unsigned)u.s.low << b;
|
||||
}
|
||||
return u.ll;
|
||||
#else
|
||||
return a << b;
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifndef COMMIT_4ad186c5ef61_IS_FIXED
|
||||
long long __tcc_cvt_ftol(long double x)
|
||||
{
|
||||
unsigned c0, c1;
|
||||
long long ret;
|
||||
__asm__ __volatile__ ("fnstcw %0" : "=m" (c0));
|
||||
c1 = c0 | 0x0C00;
|
||||
__asm__ __volatile__ ("fldcw %0" : : "m" (c1));
|
||||
__asm__ __volatile__ ("fistpll %0" : "=m" (ret));
|
||||
__asm__ __volatile__ ("fldcw %0" : : "m" (c0));
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* !__x86_64__ */
|
||||
|
||||
/* XXX: fix tcc's code generator to do this instead */
|
||||
float __floatundisf(unsigned long long a)
|
||||
{
|
||||
DWunion uu;
|
||||
XFtype r;
|
||||
|
||||
uu.ll = a;
|
||||
if (uu.s.high >= 0) {
|
||||
return (float)uu.ll;
|
||||
} else {
|
||||
r = (XFtype)uu.ll;
|
||||
r += 18446744073709551616.0;
|
||||
return (float)r;
|
||||
}
|
||||
}
|
||||
|
||||
double __floatundidf(unsigned long long a)
|
||||
{
|
||||
DWunion uu;
|
||||
XFtype r;
|
||||
|
||||
uu.ll = a;
|
||||
if (uu.s.high >= 0) {
|
||||
return (double)uu.ll;
|
||||
} else {
|
||||
r = (XFtype)uu.ll;
|
||||
r += 18446744073709551616.0;
|
||||
return (double)r;
|
||||
}
|
||||
}
|
||||
|
||||
long double __floatundixf(unsigned long long a)
|
||||
{
|
||||
DWunion uu;
|
||||
XFtype r;
|
||||
|
||||
uu.ll = a;
|
||||
if (uu.s.high >= 0) {
|
||||
return (long double)uu.ll;
|
||||
} else {
|
||||
r = (XFtype)uu.ll;
|
||||
r += 18446744073709551616.0;
|
||||
return (long double)r;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned long long __fixunssfdi (float a1)
|
||||
{
|
||||
register union float_long fl1;
|
||||
register int exp;
|
||||
register unsigned long l;
|
||||
|
||||
fl1.f = a1;
|
||||
|
||||
if (fl1.l == 0)
|
||||
return (0);
|
||||
|
||||
exp = EXP (fl1.l) - EXCESS - 24;
|
||||
|
||||
l = MANT(fl1.l);
|
||||
if (exp >= 41)
|
||||
return (unsigned long long)-1;
|
||||
else if (exp >= 0)
|
||||
return (unsigned long long)l << exp;
|
||||
else if (exp >= -23)
|
||||
return l >> -exp;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned long long __fixunsdfdi (double a1)
|
||||
{
|
||||
register union double_long dl1;
|
||||
register int exp;
|
||||
register unsigned long long l;
|
||||
|
||||
dl1.d = a1;
|
||||
|
||||
if (dl1.ll == 0)
|
||||
return (0);
|
||||
|
||||
exp = EXPD (dl1) - EXCESSD - 53;
|
||||
|
||||
l = MANTD_LL(dl1);
|
||||
|
||||
if (exp >= 12)
|
||||
return (unsigned long long)-1;
|
||||
else if (exp >= 0)
|
||||
return l << exp;
|
||||
else if (exp >= -52)
|
||||
return l >> -exp;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned long long __fixunsxfdi (long double a1)
|
||||
{
|
||||
register union ldouble_long dl1;
|
||||
register int exp;
|
||||
register unsigned long long l;
|
||||
|
||||
dl1.ld = a1;
|
||||
|
||||
if (dl1.l.lower == 0 && dl1.l.upper == 0)
|
||||
return (0);
|
||||
|
||||
exp = EXPLD (dl1) - EXCESSLD - 64;
|
||||
|
||||
l = dl1.l.lower;
|
||||
|
||||
if (exp > 0)
|
||||
return (unsigned long long)-1;
|
||||
else if (exp >= -63)
|
||||
return l >> -exp;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
long long __fixsfdi (float a1)
|
||||
{
|
||||
long long ret; int s;
|
||||
ret = __fixunssfdi((s = a1 >= 0) ? a1 : -a1);
|
||||
return s ? ret : -ret;
|
||||
}
|
||||
|
||||
long long __fixdfdi (double a1)
|
||||
{
|
||||
long long ret; int s;
|
||||
ret = __fixunsdfdi((s = a1 >= 0) ? a1 : -a1);
|
||||
return s ? ret : -ret;
|
||||
}
|
||||
|
||||
long long __fixxfdi (long double a1)
|
||||
{
|
||||
long long ret; int s;
|
||||
ret = __fixunsxfdi((s = a1 >= 0) ? a1 : -a1);
|
||||
return s ? ret : -ret;
|
||||
}
|
||||
|
||||
#if defined(TCC_TARGET_X86_64) && !defined(_WIN64)
|
||||
|
||||
#ifndef __TINYC__
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#else
|
||||
/* Avoid including stdlib.h because it is not easily available when
|
||||
cross compiling */
|
||||
#include <stddef.h> /* size_t definition is needed for a x86_64-tcc to parse memset() */
|
||||
void *malloc(unsigned long long);
|
||||
void *memset(void *s, int c, size_t n);
|
||||
void free(void*);
|
||||
void abort(void);
|
||||
#endif
|
||||
|
||||
enum __va_arg_type {
|
||||
__va_gen_reg, __va_float_reg, __va_stack
|
||||
};
|
||||
|
||||
//This should be in sync with the declaration on our include/stdarg.h
|
||||
/* GCC compatible definition of va_list. */
|
||||
typedef struct {
|
||||
unsigned int gp_offset;
|
||||
unsigned int fp_offset;
|
||||
union {
|
||||
unsigned int overflow_offset;
|
||||
char *overflow_arg_area;
|
||||
};
|
||||
char *reg_save_area;
|
||||
} __va_list_struct;
|
||||
|
||||
#undef __va_start
|
||||
#undef __va_arg
|
||||
#undef __va_copy
|
||||
#undef __va_end
|
||||
|
||||
void __va_start(__va_list_struct *ap, void *fp)
|
||||
{
|
||||
memset(ap, 0, sizeof(__va_list_struct));
|
||||
*ap = *(__va_list_struct *)((char *)fp - 16);
|
||||
ap->overflow_arg_area = (char *)fp + ap->overflow_offset;
|
||||
ap->reg_save_area = (char *)fp - 176 - 16;
|
||||
}
|
||||
|
||||
void *__va_arg(__va_list_struct *ap,
|
||||
enum __va_arg_type arg_type,
|
||||
int size, int align)
|
||||
{
|
||||
size = (size + 7) & ~7;
|
||||
align = (align + 7) & ~7;
|
||||
switch (arg_type) {
|
||||
case __va_gen_reg:
|
||||
if (ap->gp_offset + size <= 48) {
|
||||
ap->gp_offset += size;
|
||||
return ap->reg_save_area + ap->gp_offset - size;
|
||||
}
|
||||
goto use_overflow_area;
|
||||
|
||||
case __va_float_reg:
|
||||
if (ap->fp_offset < 128 + 48) {
|
||||
ap->fp_offset += 16;
|
||||
return ap->reg_save_area + ap->fp_offset - 16;
|
||||
}
|
||||
size = 8;
|
||||
goto use_overflow_area;
|
||||
|
||||
case __va_stack:
|
||||
use_overflow_area:
|
||||
ap->overflow_arg_area += size;
|
||||
ap->overflow_arg_area = (char*)((intptr_t)(ap->overflow_arg_area + align - 1) & -(intptr_t)align);
|
||||
return ap->overflow_arg_area - size;
|
||||
|
||||
default:
|
||||
#ifndef __TINYC__
|
||||
fprintf(stderr, "unknown ABI type for __va_arg\n");
|
||||
#endif
|
||||
abort();
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* __x86_64__ */
|
||||
|
||||
/* Flushing for tccrun */
|
||||
#if defined(TCC_TARGET_X86_64) || defined(TCC_TARGET_I386)
|
||||
|
||||
void __clear_cache(void *beginning, void *end)
|
||||
{
|
||||
}
|
||||
|
||||
#elif defined(TCC_TARGET_ARM)
|
||||
|
||||
#define _GNU_SOURCE
|
||||
#include <unistd.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <stdio.h>
|
||||
|
||||
void __clear_cache(void *beginning, void *end)
|
||||
{
|
||||
/* __ARM_NR_cacheflush is kernel private and should not be used in user space.
|
||||
* However, there is no ARM asm parser in tcc so we use it for now */
|
||||
#if 1
|
||||
syscall(__ARM_NR_cacheflush, beginning, end, 0);
|
||||
#else
|
||||
__asm__ ("push {r7}\n\t"
|
||||
"mov r7, #0xf0002\n\t"
|
||||
"mov r2, #0\n\t"
|
||||
"swi 0\n\t"
|
||||
"pop {r7}\n\t"
|
||||
"ret");
|
||||
#endif
|
||||
}
|
||||
|
||||
#else
|
||||
#warning __clear_cache not defined for this architecture, avoid using tcc -run
|
||||
#endif
|
@@ -0,0 +1,24 @@
|
||||
format ELF
|
||||
|
||||
section '.text' executable
|
||||
include '../../../../../proc32.inc'
|
||||
|
||||
public memcpy
|
||||
|
||||
proc memcpy c, to:dword,from:dword,count:dword
|
||||
push esi
|
||||
push edi
|
||||
mov ecx,[count]
|
||||
test ecx,ecx
|
||||
jz no_copy_block
|
||||
mov esi,[from]
|
||||
mov edi,[to]
|
||||
cld
|
||||
rep movsb
|
||||
no_copy_block:
|
||||
|
||||
pop edi
|
||||
pop esi
|
||||
mov eax, [to]
|
||||
ret
|
||||
endp
|
@@ -0,0 +1,34 @@
|
||||
format ELF
|
||||
|
||||
section '.text' executable
|
||||
include '../../../../../proc32.inc'
|
||||
|
||||
public memmove
|
||||
|
||||
proc memmove c, to:dword,from:dword,count:dword
|
||||
|
||||
push esi
|
||||
push edi
|
||||
mov ecx,[count]
|
||||
test ecx,ecx
|
||||
jz no_copy_block_
|
||||
mov esi,[from]
|
||||
mov edi,[to]
|
||||
cmp esi, edi
|
||||
je no_copy_block_
|
||||
jg copy_
|
||||
add esi, ecx
|
||||
add edi, ecx
|
||||
dec esi
|
||||
dec edi
|
||||
std
|
||||
copy_:
|
||||
rep movsb
|
||||
cld
|
||||
no_copy_block_:
|
||||
|
||||
pop edi
|
||||
pop esi
|
||||
mov eax,[to]
|
||||
ret
|
||||
endp
|
@@ -0,0 +1,16 @@
|
||||
format ELF
|
||||
section '.text' executable
|
||||
public memset
|
||||
|
||||
memset:
|
||||
push edi
|
||||
mov edi,[esp+8]
|
||||
mov eax,[esp+12]
|
||||
mov ecx,[esp+16]
|
||||
jecxz .no_set
|
||||
cld
|
||||
rep stosb
|
||||
.no_set:
|
||||
mov eax, [esp+8]
|
||||
pop edi
|
||||
ret
|
23
programs/develop/ktcc/trunk/libc.obj/source/math/acos.s
Normal file
23
programs/develop/ktcc/trunk/libc.obj/source/math/acos.s
Normal file
@@ -0,0 +1,23 @@
|
||||
.text
|
||||
|
||||
acos.LC0:
|
||||
.double 0d1.00000000000000000000e+00
|
||||
|
||||
.global acos;
|
||||
|
||||
acos:
|
||||
fldl 4(%esp)
|
||||
fld1
|
||||
fsubp %st(0),%st(1)
|
||||
fsqrt
|
||||
|
||||
fldl 4(%esp)
|
||||
fld1
|
||||
faddp %st(0),%st(1)
|
||||
fsqrt
|
||||
|
||||
fpatan
|
||||
|
||||
fld %st(0)
|
||||
faddp
|
||||
ret
|
8
programs/develop/ktcc/trunk/libc.obj/source/math/acosh.c
Normal file
8
programs/develop/ktcc/trunk/libc.obj/source/math/acosh.c
Normal file
@@ -0,0 +1,8 @@
|
||||
/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
|
||||
#include <math.h>
|
||||
|
||||
double
|
||||
acosh(double x)
|
||||
{
|
||||
return log(x + sqrt(x*x - 1));
|
||||
}
|
16
programs/develop/ktcc/trunk/libc.obj/source/math/asin.s
Normal file
16
programs/develop/ktcc/trunk/libc.obj/source/math/asin.s
Normal file
@@ -0,0 +1,16 @@
|
||||
/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
|
||||
|
||||
.global asin;
|
||||
|
||||
asin:
|
||||
fldl 4(%esp)
|
||||
fld %st(0)
|
||||
fmulp
|
||||
fld1
|
||||
fsubp
|
||||
fsqrt
|
||||
fldl 4(%esp)
|
||||
fxch %st(1)
|
||||
fpatan
|
||||
ret
|
||||
|
9
programs/develop/ktcc/trunk/libc.obj/source/math/asinh.c
Normal file
9
programs/develop/ktcc/trunk/libc.obj/source/math/asinh.c
Normal file
@@ -0,0 +1,9 @@
|
||||
/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
|
||||
#include <math.h>
|
||||
|
||||
double
|
||||
asinh(double x)
|
||||
{
|
||||
return x>0 ? log(x + sqrt(x*x + 1)) : -log(sqrt(x*x+1)-x);
|
||||
}
|
||||
|
10
programs/develop/ktcc/trunk/libc.obj/source/math/atan.s
Normal file
10
programs/develop/ktcc/trunk/libc.obj/source/math/atan.s
Normal file
@@ -0,0 +1,10 @@
|
||||
/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
|
||||
|
||||
.global atan;
|
||||
|
||||
atan:
|
||||
fldl 4(%esp)
|
||||
fld1
|
||||
fpatan
|
||||
ret
|
||||
|
37
programs/develop/ktcc/trunk/libc.obj/source/math/atan2.s
Normal file
37
programs/develop/ktcc/trunk/libc.obj/source/math/atan2.s
Normal file
@@ -0,0 +1,37 @@
|
||||
/* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
|
||||
|
||||
.data
|
||||
.align 2
|
||||
nan:
|
||||
.long 0xffffffff
|
||||
.byte 0xff
|
||||
.byte 0xff
|
||||
.byte 0xff
|
||||
.byte 0x7f
|
||||
|
||||
.text
|
||||
|
||||
.global atan2;
|
||||
|
||||
atan2:
|
||||
fldl 4(%esp)
|
||||
fldl 12(%esp)
|
||||
ftst
|
||||
fnstsw %ax
|
||||
sahf
|
||||
jne doit
|
||||
fxch %st(1)
|
||||
ftst
|
||||
fnstsw %ax
|
||||
sahf
|
||||
je isanan
|
||||
fxch %st(1)
|
||||
doit:
|
||||
fpatan
|
||||
ret
|
||||
isanan:
|
||||
movl $1, _errno
|
||||
fstp %st(0)
|
||||
fstp %st(0)
|
||||
fldl nan
|
||||
ret
|
8
programs/develop/ktcc/trunk/libc.obj/source/math/atanh.c
Normal file
8
programs/develop/ktcc/trunk/libc.obj/source/math/atanh.c
Normal file
@@ -0,0 +1,8 @@
|
||||
/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
|
||||
#include <math.h>
|
||||
|
||||
double
|
||||
atanh(double x)
|
||||
{
|
||||
return log((1+x)/(1-x)) / 2.0;
|
||||
}
|
26
programs/develop/ktcc/trunk/libc.obj/source/math/ceil.s
Normal file
26
programs/develop/ktcc/trunk/libc.obj/source/math/ceil.s
Normal file
@@ -0,0 +1,26 @@
|
||||
/* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
|
||||
|
||||
.global ceil;
|
||||
|
||||
ceil:
|
||||
pushl %ebp
|
||||
movl %esp,%ebp
|
||||
subl $8,%esp
|
||||
|
||||
fstcw -4(%ebp)
|
||||
fwait
|
||||
movw -4(%ebp),%ax
|
||||
andw $0xf3ff,%ax
|
||||
orw $0x0800,%ax
|
||||
movw %ax,-2(%ebp)
|
||||
fldcw -2(%ebp)
|
||||
|
||||
fldl 8(%ebp)
|
||||
frndint
|
||||
|
||||
fldcw -4(%ebp)
|
||||
|
||||
movl %ebp,%esp
|
||||
popl %ebp
|
||||
ret
|
||||
|
17
programs/develop/ktcc/trunk/libc.obj/source/math/cos.s
Normal file
17
programs/develop/ktcc/trunk/libc.obj/source/math/cos.s
Normal file
@@ -0,0 +1,17 @@
|
||||
cos.L0:
|
||||
.quad 0xffffffffffffffff
|
||||
|
||||
.global cos;
|
||||
|
||||
cos:
|
||||
fldl 4(%esp)
|
||||
fcos
|
||||
fstsw
|
||||
sahf
|
||||
jnp cos.L1
|
||||
fstp %st(0)
|
||||
fldl cos.L0
|
||||
|
||||
cos.L1:
|
||||
ret
|
||||
|
8
programs/develop/ktcc/trunk/libc.obj/source/math/cosh.c
Normal file
8
programs/develop/ktcc/trunk/libc.obj/source/math/cosh.c
Normal file
@@ -0,0 +1,8 @@
|
||||
/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
|
||||
#include <math.h>
|
||||
|
||||
double cosh(double x)
|
||||
{
|
||||
const double ebig = exp(fabs(x));
|
||||
return (ebig + 1.0/ebig) / 2.0;
|
||||
}
|
33
programs/develop/ktcc/trunk/libc.obj/source/math/exp.s
Normal file
33
programs/develop/ktcc/trunk/libc.obj/source/math/exp.s
Normal file
@@ -0,0 +1,33 @@
|
||||
|
||||
.data
|
||||
exp.LCW1:
|
||||
.word 0
|
||||
exp.LCW2:
|
||||
.word 0
|
||||
exp.LC0:
|
||||
.double 0d1.0e+00
|
||||
|
||||
.text
|
||||
|
||||
.global exp;
|
||||
|
||||
exp:
|
||||
fldl 4(%esp)
|
||||
fldl2e
|
||||
fmulp
|
||||
fstcw exp.LCW1
|
||||
fstcw exp.LCW2
|
||||
fwait
|
||||
andw $0xf3ff, exp.LCW2
|
||||
orw $0x0400, exp.LCW2
|
||||
fldcw exp.LCW2
|
||||
fldl %st(0)
|
||||
frndint
|
||||
fldcw exp.LCW1
|
||||
fxch %st(1)
|
||||
fsub %st(1),%st
|
||||
f2xm1
|
||||
faddl exp.LC0
|
||||
fscale
|
||||
fstp %st(1)
|
||||
ret
|
8
programs/develop/ktcc/trunk/libc.obj/source/math/fabs.s
Normal file
8
programs/develop/ktcc/trunk/libc.obj/source/math/fabs.s
Normal file
@@ -0,0 +1,8 @@
|
||||
/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
|
||||
|
||||
.global fabs;
|
||||
|
||||
fabs:
|
||||
fldl 4(%esp)
|
||||
fabs
|
||||
ret
|
26
programs/develop/ktcc/trunk/libc.obj/source/math/floor.s
Normal file
26
programs/develop/ktcc/trunk/libc.obj/source/math/floor.s
Normal file
@@ -0,0 +1,26 @@
|
||||
/* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
|
||||
|
||||
.global floor;
|
||||
|
||||
floor:
|
||||
pushl %ebp
|
||||
movl %esp,%ebp
|
||||
subl $8,%esp
|
||||
|
||||
fstcw -4(%ebp)
|
||||
fwait
|
||||
movw -4(%ebp),%ax
|
||||
andw $0xf3ff,%ax
|
||||
orw $0x0400,%ax
|
||||
movw %ax,-2(%ebp)
|
||||
fldcw -2(%ebp)
|
||||
|
||||
fldl 8(%ebp)
|
||||
frndint
|
||||
|
||||
fldcw -4(%ebp)
|
||||
|
||||
movl %ebp,%esp
|
||||
popl %ebp
|
||||
ret
|
||||
|
26
programs/develop/ktcc/trunk/libc.obj/source/math/fmod.s
Normal file
26
programs/develop/ktcc/trunk/libc.obj/source/math/fmod.s
Normal file
@@ -0,0 +1,26 @@
|
||||
/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
|
||||
|
||||
.text
|
||||
|
||||
.global fmod;
|
||||
|
||||
fmod:
|
||||
fldl 4(%esp)
|
||||
fldl 12(%esp)
|
||||
ftst
|
||||
fnstsw %ax
|
||||
fxch %st(1)
|
||||
sahf
|
||||
jnz next
|
||||
fstpl %st(0)
|
||||
jmp out
|
||||
next:
|
||||
fprem
|
||||
fnstsw %ax
|
||||
sahf
|
||||
jpe next
|
||||
fstpl %st(1)
|
||||
out:
|
||||
ret
|
||||
|
||||
|
26
programs/develop/ktcc/trunk/libc.obj/source/math/frexp.c
Normal file
26
programs/develop/ktcc/trunk/libc.obj/source/math/frexp.c
Normal file
@@ -0,0 +1,26 @@
|
||||
/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
|
||||
#include <math.h>
|
||||
|
||||
double
|
||||
frexp(double x, int *exptr)
|
||||
{
|
||||
union {
|
||||
double d;
|
||||
unsigned char c[8];
|
||||
} u;
|
||||
|
||||
u.d = x;
|
||||
/*
|
||||
* The format of the number is:
|
||||
* Sign, 12 exponent bits, 51 mantissa bits
|
||||
* The exponent is 1023 biased and there is an implicit zero.
|
||||
* We get the exponent from the upper bits and set the exponent
|
||||
* to 0x3fe (1022).
|
||||
*/
|
||||
*exptr = (int)(((u.c[7] & 0x7f) << 4) | (u.c[6] >> 4)) - 1022;
|
||||
u.c[7] &= 0x80;
|
||||
u.c[7] |= 0x3f;
|
||||
u.c[6] &= 0x0f;
|
||||
u.c[6] |= 0xe0;
|
||||
return u.d;
|
||||
}
|
100
programs/develop/ktcc/trunk/libc.obj/source/math/hypot.c
Normal file
100
programs/develop/ktcc/trunk/libc.obj/source/math/hypot.c
Normal file
@@ -0,0 +1,100 @@
|
||||
/* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
|
||||
/*
|
||||
* hypot() function for DJGPP.
|
||||
*
|
||||
* hypot() computes sqrt(x^2 + y^2). The problem with the obvious
|
||||
* naive implementation is that it might fail for very large or
|
||||
* very small arguments. For instance, for large x or y the result
|
||||
* might overflow even if the value of the function should not,
|
||||
* because squaring a large number might trigger an overflow. For
|
||||
* very small numbers, their square might underflow and will be
|
||||
* silently replaced by zero; this won't cause an exception, but might
|
||||
* have an adverse effect on the accuracy of the result.
|
||||
*
|
||||
* This implementation tries to avoid the above pitfals, without
|
||||
* inflicting too much of a performance hit.
|
||||
*
|
||||
*/
|
||||
|
||||
/// #include <float.h>
|
||||
#include <math.h>
|
||||
#include <errno.h>
|
||||
|
||||
/* Approximate square roots of DBL_MAX and DBL_MIN. Numbers
|
||||
between these two shouldn't neither overflow nor underflow
|
||||
when squared. */
|
||||
#define __SQRT_DBL_MAX 1.3e+154
|
||||
#define __SQRT_DBL_MIN 2.3e-162
|
||||
|
||||
double
|
||||
hypot(double x, double y)
|
||||
{
|
||||
double abig = fabs(x), asmall = fabs(y);
|
||||
double ratio;
|
||||
|
||||
/* Make abig = max(|x|, |y|), asmall = min(|x|, |y|). */
|
||||
if (abig < asmall)
|
||||
{
|
||||
double temp = abig;
|
||||
|
||||
abig = asmall;
|
||||
asmall = temp;
|
||||
}
|
||||
|
||||
/* Trivial case. */
|
||||
if (asmall == 0.)
|
||||
return abig;
|
||||
|
||||
/* Scale the numbers as much as possible by using its ratio.
|
||||
For example, if both ABIG and ASMALL are VERY small, then
|
||||
X^2 + Y^2 might be VERY inaccurate due to loss of
|
||||
significant digits. Dividing ASMALL by ABIG scales them
|
||||
to a certain degree, so that accuracy is better. */
|
||||
|
||||
if ((ratio = asmall / abig) > __SQRT_DBL_MIN && abig < __SQRT_DBL_MAX)
|
||||
return abig * sqrt(1.0 + ratio*ratio);
|
||||
else
|
||||
{
|
||||
/* Slower but safer algorithm due to Moler and Morrison. Never
|
||||
produces any intermediate result greater than roughly the
|
||||
larger of X and Y. Should converge to machine-precision
|
||||
accuracy in 3 iterations. */
|
||||
|
||||
double r = ratio*ratio, t, s, p = abig, q = asmall;
|
||||
|
||||
do {
|
||||
t = 4. + r;
|
||||
if (t == 4.)
|
||||
break;
|
||||
s = r / t;
|
||||
p += 2. * s * p;
|
||||
q *= s;
|
||||
r = (q / p) * (q / p);
|
||||
} while (1);
|
||||
|
||||
return p;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef TEST
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
printf("hypot(3, 4) =\t\t\t %25.17e\n", hypot(3., 4.));
|
||||
printf("hypot(3*10^150, 4*10^150) =\t %25.17g\n", hypot(3.e+150, 4.e+150));
|
||||
printf("hypot(3*10^306, 4*10^306) =\t %25.17g\n", hypot(3.e+306, 4.e+306));
|
||||
printf("hypot(3*10^-320, 4*10^-320) =\t %25.17g\n",
|
||||
hypot(3.e-320, 4.e-320));
|
||||
printf("hypot(0.7*DBL_MAX, 0.7*DBL_MAX) =%25.17g\n",
|
||||
hypot(0.7*DBL_MAX, 0.7*DBL_MAX));
|
||||
printf("hypot(DBL_MAX, 1.0) =\t\t %25.17g\n", hypot(DBL_MAX, 1.0));
|
||||
printf("hypot(1.0, DBL_MAX) =\t\t %25.17g\n", hypot(1.0, DBL_MAX));
|
||||
printf("hypot(0.0, DBL_MAX) =\t\t %25.17g\n", hypot(0.0, DBL_MAX));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
32
programs/develop/ktcc/trunk/libc.obj/source/math/ldexp.c
Normal file
32
programs/develop/ktcc/trunk/libc.obj/source/math/ldexp.c
Normal file
@@ -0,0 +1,32 @@
|
||||
/* Copyright (C) 1996 DJ Delorie, see COPYING.DJ for details */
|
||||
/* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
|
||||
#include <math.h>
|
||||
|
||||
double
|
||||
ldexp(double v, int e)
|
||||
{
|
||||
double two = 2.0;
|
||||
|
||||
if (e < 0)
|
||||
{
|
||||
e = -e; /* This just might overflow on two-complement machines. */
|
||||
if (e < 0) return 0.0;
|
||||
while (e > 0)
|
||||
{
|
||||
if (e & 1) v /= two;
|
||||
two *= two;
|
||||
e >>= 1;
|
||||
}
|
||||
}
|
||||
else if (e > 0)
|
||||
{
|
||||
while (e > 0)
|
||||
{
|
||||
if (e & 1) v *= two;
|
||||
two *= two;
|
||||
e >>= 1;
|
||||
}
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
9
programs/develop/ktcc/trunk/libc.obj/source/math/log.s
Normal file
9
programs/develop/ktcc/trunk/libc.obj/source/math/log.s
Normal file
@@ -0,0 +1,9 @@
|
||||
/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
|
||||
|
||||
.global log;
|
||||
|
||||
log:
|
||||
fldln2
|
||||
fldl 4(%esp)
|
||||
fyl2x
|
||||
ret
|
35
programs/develop/ktcc/trunk/libc.obj/source/math/modf.s
Normal file
35
programs/develop/ktcc/trunk/libc.obj/source/math/modf.s
Normal file
@@ -0,0 +1,35 @@
|
||||
/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
|
||||
.text
|
||||
|
||||
.global modf;
|
||||
|
||||
modf:
|
||||
pushl %ebp
|
||||
movl %esp,%ebp
|
||||
subl $16,%esp
|
||||
pushl %ebx
|
||||
fnstcw -4(%ebp)
|
||||
fwait
|
||||
movw -4(%ebp),%ax
|
||||
orw $0x0c3f,%ax
|
||||
movw %ax,-8(%ebp)
|
||||
fldcw -8(%ebp)
|
||||
fwait
|
||||
fldl 8(%ebp)
|
||||
frndint
|
||||
fstpl -16(%ebp)
|
||||
fwait
|
||||
movl -16(%ebp),%edx
|
||||
movl -12(%ebp),%ecx
|
||||
movl 16(%ebp),%ebx
|
||||
movl %edx,(%ebx)
|
||||
movl %ecx,4(%ebx)
|
||||
fldl 8(%ebp)
|
||||
fsubl -16(%ebp)
|
||||
leal -20(%ebp),%esp
|
||||
fclex
|
||||
fldcw -4(%ebp)
|
||||
fwait
|
||||
popl %ebx
|
||||
leave
|
||||
ret
|
23
programs/develop/ktcc/trunk/libc.obj/source/math/modfl.s
Normal file
23
programs/develop/ktcc/trunk/libc.obj/source/math/modfl.s
Normal file
@@ -0,0 +1,23 @@
|
||||
/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
|
||||
|
||||
.global modfl;
|
||||
|
||||
modfl:
|
||||
pushl %ebp
|
||||
movl %esp,%ebp
|
||||
subl $4,%esp
|
||||
fldt 8(%ebp)
|
||||
movl 20(%ebp),%eax
|
||||
fnstcw -2(%ebp)
|
||||
movw -2(%ebp),%dx
|
||||
orb $0x0c,%dh
|
||||
movw %dx,-4(%ebp)
|
||||
fldcw -4(%ebp)
|
||||
fld %st(0)
|
||||
frndint
|
||||
fldcw -2(%ebp)
|
||||
fld %st(0)
|
||||
fstpt (%eax)
|
||||
fsubrp %st,%st(1)
|
||||
leave
|
||||
ret
|
87
programs/develop/ktcc/trunk/libc.obj/source/math/pow.s
Normal file
87
programs/develop/ktcc/trunk/libc.obj/source/math/pow.s
Normal file
@@ -0,0 +1,87 @@
|
||||
/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
|
||||
.data
|
||||
yint:
|
||||
.word 0,0
|
||||
pow.LCW1:
|
||||
.word 0
|
||||
pow.LCW2:
|
||||
.word 0
|
||||
|
||||
.text
|
||||
pow.LC0:
|
||||
.double 0d1.0e+00
|
||||
|
||||
.global pow;
|
||||
|
||||
frac:
|
||||
fstcw pow.LCW1
|
||||
fstcw pow.LCW2
|
||||
fwait
|
||||
andw $0xf3ff,pow.LCW2
|
||||
orw $0x0400,pow.LCW2
|
||||
fldcw pow.LCW2
|
||||
fldl %st(0)
|
||||
frndint
|
||||
fldcw pow.LCW1
|
||||
fxch %st(1)
|
||||
fsub %st(1),%st
|
||||
ret
|
||||
|
||||
Lpow2:
|
||||
call frac
|
||||
f2xm1
|
||||
faddl pow.LC0
|
||||
fscale
|
||||
fstp %st(1)
|
||||
ret
|
||||
|
||||
pow:
|
||||
fldl 12(%esp)
|
||||
fldl 4(%esp)
|
||||
ftst
|
||||
fnstsw %ax
|
||||
sahf
|
||||
jbe xltez
|
||||
fyl2x
|
||||
jmp Lpow2
|
||||
xltez:
|
||||
jb xltz
|
||||
fstp %st(0)
|
||||
ftst
|
||||
fnstsw %ax
|
||||
sahf
|
||||
ja ygtz
|
||||
jb error
|
||||
fstp %st(0)
|
||||
fld1
|
||||
fchs
|
||||
error:
|
||||
fsqrt
|
||||
ret
|
||||
ygtz:
|
||||
fstp %st(0)
|
||||
fldz
|
||||
ret
|
||||
xltz:
|
||||
fabs
|
||||
fxch %st(1)
|
||||
call frac
|
||||
ftst
|
||||
fnstsw %ax
|
||||
fstp %st(0)
|
||||
sahf
|
||||
je yisint
|
||||
fstp %st(0)
|
||||
fchs
|
||||
jmp error
|
||||
yisint:
|
||||
fistl yint
|
||||
fxch %st(1)
|
||||
fyl2x
|
||||
call Lpow2
|
||||
andl $1,yint
|
||||
jz yeven
|
||||
fchs
|
||||
yeven:
|
||||
ret
|
||||
|
34
programs/develop/ktcc/trunk/libc.obj/source/math/pow10.s
Normal file
34
programs/develop/ktcc/trunk/libc.obj/source/math/pow10.s
Normal file
@@ -0,0 +1,34 @@
|
||||
/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
|
||||
|
||||
.data
|
||||
pow10.LCW1:
|
||||
.word 0
|
||||
pow10.LCW2:
|
||||
.word 0
|
||||
pow10.LC0:
|
||||
.double 0d1.0e+00
|
||||
|
||||
.text
|
||||
|
||||
.global pow10;
|
||||
|
||||
pow10:
|
||||
fldl 4(%esp)
|
||||
fldl2t
|
||||
fmulp
|
||||
fstcw pow10.LCW1
|
||||
fstcw pow10.LCW2
|
||||
fwait
|
||||
andw $0xf3ff,pow10.LCW2
|
||||
orw $0x0400,pow10.LCW2
|
||||
fldcw pow10.LCW2
|
||||
fldl %st(0)
|
||||
frndint
|
||||
fldcw pow10.LCW1
|
||||
fxch %st(1)
|
||||
fsub %st(1),%st
|
||||
f2xm1
|
||||
faddl pow10.LC0
|
||||
fscale
|
||||
fstp %st(1)
|
||||
ret
|
32
programs/develop/ktcc/trunk/libc.obj/source/math/pow2.s
Normal file
32
programs/develop/ktcc/trunk/libc.obj/source/math/pow2.s
Normal file
@@ -0,0 +1,32 @@
|
||||
/* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
|
||||
|
||||
.data
|
||||
pow2.LCW1:
|
||||
.word 0
|
||||
pow2.LCW2:
|
||||
.word 0
|
||||
pow2.LC0:
|
||||
.double 0d1.0e+00
|
||||
|
||||
.text
|
||||
|
||||
.global pow2;
|
||||
|
||||
pow2:
|
||||
fldl 4(%esp)
|
||||
fstcw pow2.LCW1
|
||||
fstcw pow2.LCW2
|
||||
fwait
|
||||
andw $0xf3ff,pow2.LCW2
|
||||
orw $0x0400,pow2.LCW2
|
||||
fldcw pow2.LCW2
|
||||
fldl %st(0)
|
||||
frndint
|
||||
fldcw pow2.LCW1
|
||||
fxch %st(1)
|
||||
fsub %st(1),%st
|
||||
f2xm1
|
||||
faddl pow2.LC0
|
||||
fscale
|
||||
fstp %st(1)
|
||||
ret
|
18
programs/develop/ktcc/trunk/libc.obj/source/math/sin.s
Normal file
18
programs/develop/ktcc/trunk/libc.obj/source/math/sin.s
Normal file
@@ -0,0 +1,18 @@
|
||||
/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
|
||||
|
||||
.global sin;
|
||||
|
||||
sin.L0:
|
||||
.quad 0xffffffffffffffff
|
||||
|
||||
sin:
|
||||
fldl 4(%esp)
|
||||
fsin
|
||||
fstsw
|
||||
sahf
|
||||
jnp sin.L1
|
||||
fstp %st(0)
|
||||
fldl L0
|
||||
sin.L1:
|
||||
ret
|
||||
|
16
programs/develop/ktcc/trunk/libc.obj/source/math/sinh.c
Normal file
16
programs/develop/ktcc/trunk/libc.obj/source/math/sinh.c
Normal file
@@ -0,0 +1,16 @@
|
||||
/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
|
||||
#include <math.h>
|
||||
|
||||
double sinh(double x)
|
||||
{
|
||||
if(x >= 0.0)
|
||||
{
|
||||
const double epos = exp(x);
|
||||
return (epos - 1.0/epos) / 2.0;
|
||||
}
|
||||
else
|
||||
{
|
||||
const double eneg = exp(-x);
|
||||
return (1.0/eneg - eneg) / 2.0;
|
||||
}
|
||||
}
|
8
programs/develop/ktcc/trunk/libc.obj/source/math/sqrt.s
Normal file
8
programs/develop/ktcc/trunk/libc.obj/source/math/sqrt.s
Normal file
@@ -0,0 +1,8 @@
|
||||
/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
|
||||
|
||||
.global sqrt;
|
||||
|
||||
sqrt:
|
||||
fldl 4(%esp)
|
||||
fsqrt
|
||||
ret
|
16
programs/develop/ktcc/trunk/libc.obj/source/math/tan.s
Normal file
16
programs/develop/ktcc/trunk/libc.obj/source/math/tan.s
Normal file
@@ -0,0 +1,16 @@
|
||||
/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
|
||||
|
||||
tan.L0:
|
||||
.quad 0xffffffffffffffff
|
||||
|
||||
tan:
|
||||
fldl 4(%esp)
|
||||
fptan
|
||||
fstsw
|
||||
fstp %st(0)
|
||||
sahf
|
||||
jnp tan.L1
|
||||
/* fstp %st(0) - if exception, there is nothing on the stack */
|
||||
fldl tan.L0
|
||||
tan.L1:
|
||||
ret
|
17
programs/develop/ktcc/trunk/libc.obj/source/math/tanh.c
Normal file
17
programs/develop/ktcc/trunk/libc.obj/source/math/tanh.c
Normal file
@@ -0,0 +1,17 @@
|
||||
/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
|
||||
#include <math.h>
|
||||
|
||||
double tanh(double x)
|
||||
{
|
||||
if (x > 50)
|
||||
return 1;
|
||||
else if (x < -50)
|
||||
return -1;
|
||||
else
|
||||
{
|
||||
const double ebig = exp(x);
|
||||
const double esmall = 1.0/ebig;
|
||||
return (ebig - esmall) / (ebig + esmall);
|
||||
}
|
||||
}
|
||||
|
13
programs/develop/ktcc/trunk/libc.obj/source/setjmp/longjmp.s
Normal file
13
programs/develop/ktcc/trunk/libc.obj/source/setjmp/longjmp.s
Normal file
@@ -0,0 +1,13 @@
|
||||
.global longjmp
|
||||
|
||||
longjmp:
|
||||
mov 4(%esp),%edx
|
||||
mov 8(%esp),%eax
|
||||
cmp $1,%eax
|
||||
adc $0, %al
|
||||
mov (%edx),%ebx
|
||||
mov 4(%edx),%esi
|
||||
mov 8(%edx),%edi
|
||||
mov 12(%edx),%ebp
|
||||
mov 16(%edx),%esp
|
||||
jmp *20(%edx)
|
15
programs/develop/ktcc/trunk/libc.obj/source/setjmp/setjmp.s
Normal file
15
programs/develop/ktcc/trunk/libc.obj/source/setjmp/setjmp.s
Normal file
@@ -0,0 +1,15 @@
|
||||
|
||||
.global setjmp
|
||||
|
||||
setjmp:
|
||||
mov 4(%esp), %eax
|
||||
mov %ebx, (%eax)
|
||||
mov %esi, 4(%eax)
|
||||
mov %edi, 8(%eax)
|
||||
mov %ebp, 12(%eax)
|
||||
lea 4(%esp), %ecx
|
||||
mov %ecx, 16(%eax)
|
||||
mov (%esp), %ecx
|
||||
mov %ecx, 20(%eax)
|
||||
xor %eax, %eax
|
||||
ret
|
@@ -0,0 +1,6 @@
|
||||
#include <stdio.h>
|
||||
|
||||
void clearerr(FILE *stream) {
|
||||
stream->error = 0;
|
||||
stream->eof = 0;
|
||||
}
|
81
programs/develop/ktcc/trunk/libc.obj/source/stdio/conio.c
Normal file
81
programs/develop/ktcc/trunk/libc.obj/source/stdio/conio.c
Normal file
@@ -0,0 +1,81 @@
|
||||
#include <sys/ksys.h>
|
||||
#include <conio.h>
|
||||
#include "stdio.h"
|
||||
|
||||
static char* __con_caption = "Console application";
|
||||
static char* __con_dllname = "/sys/lib/console.obj";
|
||||
|
||||
int __con_is_load = 0;
|
||||
|
||||
void stdcall (*__con_init_hidden)(int wnd_width, unsigned wnd_height, int scr_width, int scr_height, const char* title);
|
||||
void stdcall (*con_exit)(int);
|
||||
void stdcall (*con_set_title)(const char* title);
|
||||
void stdcall (*con_write_asciiz)(const char* str);
|
||||
void stdcall (*con_write_string)(const char* str, dword length);
|
||||
int cdecl (*con_printf)(const char* format, ...);
|
||||
dword stdcall (*con_get_flags)(void);
|
||||
dword stdcall (*con_set_flags)(dword new_flags);
|
||||
int stdcall (*con_get_font_height)(void);
|
||||
int stdcall (*con_get_cursor_height)(void);
|
||||
int stdcall (*con_set_cursor_height)(int new_height);
|
||||
int stdcall (*con_getch)(void);
|
||||
word stdcall (*con_getch2)(void);
|
||||
int stdcall (*con_kbhit)(void);
|
||||
char* stdcall (*con_gets)(char* str, int n);
|
||||
char* stdcall (*con_gets2)(con_gets2_callback callback, char* str, int n);
|
||||
void stdcall (*con_cls)();
|
||||
void stdcall (*con_get_cursor_pos)(int* px, int* py);
|
||||
void stdcall (*con_set_cursor_pos)(int x, int y);
|
||||
|
||||
static void __con_panic(char* func_name)
|
||||
{
|
||||
debug_printf("In console.obj %s=NULL!\n", func_name);
|
||||
_ksys_exit();
|
||||
}
|
||||
|
||||
static void __con_lib_link(ksys_coff_etable_t *exp)
|
||||
{
|
||||
__con_init_hidden = _ksys_get_coff_func(exp, "con_init", __con_panic);
|
||||
con_exit = _ksys_get_coff_func(exp, "con_exit", __con_panic);
|
||||
con_set_title = _ksys_get_coff_func(exp, "con_set_title", __con_panic);
|
||||
con_write_asciiz = _ksys_get_coff_func(exp, "con_write_asciiz", __con_panic);
|
||||
con_write_string = _ksys_get_coff_func(exp, "con_write_string", __con_panic);
|
||||
con_printf = _ksys_get_coff_func(exp, "con_printf", __con_panic);
|
||||
con_get_flags = _ksys_get_coff_func(exp, "con_get_flags", __con_panic);
|
||||
con_set_flags = _ksys_get_coff_func(exp, "con_set_flags", __con_panic);
|
||||
con_get_font_height = _ksys_get_coff_func(exp, "con_get_font_height", __con_panic);
|
||||
con_get_cursor_height = _ksys_get_coff_func(exp, "con_get_cursor_height", __con_panic);
|
||||
con_set_cursor_height = _ksys_get_coff_func(exp, "con_set_cursor_height", __con_panic);
|
||||
con_getch = _ksys_get_coff_func(exp, "con_getch", __con_panic);
|
||||
con_getch2 = _ksys_get_coff_func(exp, "con_getch2", __con_panic);
|
||||
con_kbhit = _ksys_get_coff_func(exp, "con_kbhit", __con_panic);
|
||||
con_gets = _ksys_get_coff_func(exp, "con_gets", __con_panic);
|
||||
con_gets2 = _ksys_get_coff_func(exp, "con_gets2", __con_panic);
|
||||
con_cls = _ksys_get_coff_func(exp, "con_cls", __con_panic);
|
||||
con_get_cursor_pos = _ksys_get_coff_func(exp, "con_get_cursor_pos", __con_panic);
|
||||
con_set_cursor_pos = _ksys_get_coff_func(exp, "con_set_cursor_pos", __con_panic);
|
||||
}
|
||||
|
||||
int con_init_opt(dword wnd_width, dword wnd_height, dword scr_width, dword scr_height, const char* title)
|
||||
{
|
||||
if(!__con_is_load){
|
||||
ksys_coff_etable_t *__con_lib;
|
||||
__con_lib = _ksys_load_coff(__con_dllname);
|
||||
if(__con_lib==NULL){
|
||||
_ksys_debug_puts("Error! Can't load console.obj lib\n");
|
||||
return 1;
|
||||
}
|
||||
__con_lib_link(__con_lib);
|
||||
__con_init_hidden(wnd_width, wnd_height, scr_width, scr_height, title);
|
||||
__con_is_load= 1;
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int con_init(void)
|
||||
{
|
||||
return con_init_opt(-1, -1, -1, -1, __con_caption);
|
||||
}
|
||||
|
@@ -0,0 +1,15 @@
|
||||
/* Copyright (C) 2021 Logaev Maxim (turbocat2001), GPLv2 */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <limits.h>
|
||||
|
||||
void debug_printf(const char *format,...)
|
||||
{
|
||||
va_list ap;
|
||||
static char log_board[STDIO_MAX_MEM];
|
||||
va_start (ap, format);
|
||||
vsnprintf(log_board, STDIO_MAX_MEM, format, ap);
|
||||
va_end(ap);
|
||||
_ksys_debug_puts(log_board);
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int fclose(FILE *stream) {
|
||||
free(stream);
|
||||
return 0;
|
||||
}
|
5
programs/develop/ktcc/trunk/libc.obj/source/stdio/feof.c
Normal file
5
programs/develop/ktcc/trunk/libc.obj/source/stdio/feof.c
Normal file
@@ -0,0 +1,5 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int feof(FILE *stream) {
|
||||
return stream->eof;
|
||||
}
|
@@ -0,0 +1,5 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int ferror(FILE *stream) {
|
||||
return stream->error;
|
||||
}
|
@@ -0,0 +1,5 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int fflush(FILE *stream) {
|
||||
return 0;
|
||||
}
|
11
programs/develop/ktcc/trunk/libc.obj/source/stdio/fgetc.c
Normal file
11
programs/develop/ktcc/trunk/libc.obj/source/stdio/fgetc.c
Normal file
@@ -0,0 +1,11 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int fgetc(FILE* stream)
|
||||
{
|
||||
int c=0;
|
||||
if(fread(&c, sizeof(char), 1, stream)==1){
|
||||
return c;
|
||||
}else{
|
||||
return EOF;
|
||||
}
|
||||
}
|
@@ -0,0 +1,6 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int fgetpos(FILE *restrict stream, fpos_t *restrict pos) {
|
||||
*pos = stream->position;
|
||||
return 0;
|
||||
}
|
24
programs/develop/ktcc/trunk/libc.obj/source/stdio/fgets.c
Normal file
24
programs/develop/ktcc/trunk/libc.obj/source/stdio/fgets.c
Normal file
@@ -0,0 +1,24 @@
|
||||
#include <stdio.h>
|
||||
#include "conio.h"
|
||||
#include <errno.h>
|
||||
|
||||
char *fgets(char *str, int n, FILE *stream)
|
||||
{
|
||||
int i=0, sym_code;
|
||||
|
||||
if(!stream || !str){
|
||||
errno = EINVAL;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
while (i<n-1){
|
||||
sym_code = fgetc(stream);
|
||||
if(sym_code =='\n' || sym_code == EOF){ break; }
|
||||
str[i]=(char)sym_code;
|
||||
i++;
|
||||
}
|
||||
|
||||
if(i<1){ return NULL; }
|
||||
return str;
|
||||
}
|
||||
|
@@ -0,0 +1,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
FILE *fopen(const char *restrict _name, const char *restrict _mode) {
|
||||
FILE *out = malloc(sizeof(FILE));
|
||||
return freopen(_name, _mode, out);
|
||||
}
|
817
programs/develop/ktcc/trunk/libc.obj/source/stdio/format_print.c
Normal file
817
programs/develop/ktcc/trunk/libc.obj/source/stdio/format_print.c
Normal file
@@ -0,0 +1,817 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// \author (c) Marco Paland (info@paland.com)
|
||||
// 2014-2019, PALANDesign Hannover, Germany
|
||||
//
|
||||
// \license The MIT License (MIT)
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
//
|
||||
// \brief Tiny printf, sprintf and (v)snprintf implementation, optimized for speed on
|
||||
// embedded systems with a very limited resources. These routines are thread
|
||||
// safe and reentrant!
|
||||
// Use this instead of the bloated standard/newlib printf cause these use
|
||||
// malloc for printf (and may not be thread safe).
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include "format_print.h"
|
||||
#include <math.h>
|
||||
|
||||
// 'ntoa' conversion buffer size, this must be big enough to hold one converted
|
||||
// numeric number including padded zeros (dynamically created on stack)
|
||||
// default: 32 byte
|
||||
#ifndef PRINTF_NTOA_BUFFER_SIZE
|
||||
#define PRINTF_NTOA_BUFFER_SIZE 32U
|
||||
#endif
|
||||
|
||||
// 'ftoa' conversion buffer size, this must be big enough to hold one converted
|
||||
// float number including padded zeros (dynamically created on stack)
|
||||
// default: 32 byte
|
||||
#ifndef PRINTF_FTOA_BUFFER_SIZE
|
||||
#define PRINTF_FTOA_BUFFER_SIZE 32U
|
||||
#endif
|
||||
|
||||
// support for the floating point type (%f)
|
||||
// default: activated
|
||||
#ifndef PRINTF_DISABLE_SUPPORT_FLOAT
|
||||
#define PRINTF_SUPPORT_FLOAT
|
||||
#endif
|
||||
|
||||
// support for exponential floating point notation (%e/%g)
|
||||
// default: activated
|
||||
#ifndef PRINTF_DISABLE_SUPPORT_EXPONENTIAL
|
||||
#define PRINTF_SUPPORT_EXPONENTIAL
|
||||
#endif
|
||||
|
||||
// define the default floating point precision
|
||||
// default: 6 digits
|
||||
#ifndef PRINTF_DEFAULT_FLOAT_PRECISION
|
||||
#define PRINTF_DEFAULT_FLOAT_PRECISION 6U
|
||||
#endif
|
||||
|
||||
// define the largest float suitable to print with %f
|
||||
// default: 1e9
|
||||
#ifndef PRINTF_MAX_FLOAT
|
||||
#define PRINTF_MAX_FLOAT 1e9
|
||||
#endif
|
||||
|
||||
// support for the long long types (%llu or %p)
|
||||
// default: activated
|
||||
#ifndef PRINTF_DISABLE_SUPPORT_LONG_LONG
|
||||
#define PRINTF_SUPPORT_LONG_LONG
|
||||
#endif
|
||||
|
||||
// support for the ptrdiff_t type (%t)
|
||||
// ptrdiff_t is normally defined in <stddef.h> as long or long long type
|
||||
// default: activated
|
||||
#ifndef PRINTF_DISABLE_SUPPORT_PTRDIFF_T
|
||||
#define PRINTF_SUPPORT_PTRDIFF_T
|
||||
#endif
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// internal flag definitions
|
||||
#define FLAGS_ZEROPAD (1U << 0U)
|
||||
#define FLAGS_LEFT (1U << 1U)
|
||||
#define FLAGS_PLUS (1U << 2U)
|
||||
#define FLAGS_SPACE (1U << 3U)
|
||||
#define FLAGS_HASH (1U << 4U)
|
||||
#define FLAGS_UPPERCASE (1U << 5U)
|
||||
#define FLAGS_CHAR (1U << 6U)
|
||||
#define FLAGS_SHORT (1U << 7U)
|
||||
#define FLAGS_LONG (1U << 8U)
|
||||
#define FLAGS_LONG_LONG (1U << 9U)
|
||||
#define FLAGS_PRECISION (1U << 10U)
|
||||
#define FLAGS_ADAPT_EXP (1U << 11U)
|
||||
|
||||
|
||||
// import float.h for DBL_MAX
|
||||
#if defined(PRINTF_SUPPORT_FLOAT)
|
||||
#include <float.h>
|
||||
#endif
|
||||
|
||||
|
||||
// internal buffer output
|
||||
void _out_buffer(char character, void* buffer, size_t idx, size_t maxlen)
|
||||
{
|
||||
if (idx < maxlen) {
|
||||
((char*)buffer)[idx] = character;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// internal null output
|
||||
void _out_null(char character, void* buffer, size_t idx, size_t maxlen)
|
||||
{
|
||||
(void)character; (void)buffer; (void)idx; (void)maxlen;
|
||||
}
|
||||
|
||||
|
||||
// internal secure strlen
|
||||
// \return The length of the string (excluding the terminating 0) limited by 'maxsize'
|
||||
static inline unsigned int _strnlen_s(const char* str, size_t maxsize)
|
||||
{
|
||||
const char* s;
|
||||
for (s = str; *s && maxsize--; ++s);
|
||||
return (unsigned int)(s - str);
|
||||
}
|
||||
|
||||
|
||||
// internal test if char is a digit (0-9)
|
||||
// \return true if char is a digit
|
||||
static inline bool _is_digit(char ch)
|
||||
{
|
||||
return (ch >= '0') && (ch <= '9');
|
||||
}
|
||||
|
||||
|
||||
// internal ASCII string to unsigned int conversion
|
||||
static unsigned int _atoi(const char** str)
|
||||
{
|
||||
unsigned int i = 0U;
|
||||
while (_is_digit(**str)) {
|
||||
i = i * 10U + (unsigned int)(*((*str)++) - '0');
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
|
||||
// output the specified string in reverse, taking care of any zero-padding
|
||||
static size_t _out_rev(out_fct_type out, char* buffer, size_t idx, size_t maxlen, const char* buf, size_t len, unsigned int width, unsigned int flags)
|
||||
{
|
||||
const size_t start_idx = idx;
|
||||
|
||||
// pad spaces up to given width
|
||||
if (!(flags & FLAGS_LEFT) && !(flags & FLAGS_ZEROPAD)) {
|
||||
for (size_t i = len; i < width; i++) {
|
||||
out(' ', buffer, idx++, maxlen);
|
||||
}
|
||||
}
|
||||
|
||||
// reverse string
|
||||
while (len) {
|
||||
out(buf[--len], buffer, idx++, maxlen);
|
||||
}
|
||||
|
||||
// append pad spaces up to given width
|
||||
if (flags & FLAGS_LEFT) {
|
||||
while (idx - start_idx < width) {
|
||||
out(' ', buffer, idx++, maxlen);
|
||||
}
|
||||
}
|
||||
|
||||
return idx;
|
||||
}
|
||||
|
||||
|
||||
// internal itoa format
|
||||
static size_t _ntoa_format(out_fct_type out, char* buffer, size_t idx, size_t maxlen, char* buf, size_t len, bool negative, unsigned int base, unsigned int prec, unsigned int width, unsigned int flags)
|
||||
{
|
||||
// pad leading zeros
|
||||
if (!(flags & FLAGS_LEFT)) {
|
||||
if (width && (flags & FLAGS_ZEROPAD) && (negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) {
|
||||
width--;
|
||||
}
|
||||
while ((len < prec) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
|
||||
buf[len++] = '0';
|
||||
}
|
||||
while ((flags & FLAGS_ZEROPAD) && (len < width) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
|
||||
buf[len++] = '0';
|
||||
}
|
||||
}
|
||||
|
||||
// handle hash
|
||||
if (flags & FLAGS_HASH) {
|
||||
if (!(flags & FLAGS_PRECISION) && len && ((len == prec) || (len == width))) {
|
||||
len--;
|
||||
if (len && (base == 16U)) {
|
||||
len--;
|
||||
}
|
||||
}
|
||||
if ((base == 16U) && !(flags & FLAGS_UPPERCASE) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
|
||||
buf[len++] = 'x';
|
||||
}
|
||||
else if ((base == 16U) && (flags & FLAGS_UPPERCASE) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
|
||||
buf[len++] = 'X';
|
||||
}
|
||||
else if ((base == 2U) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
|
||||
buf[len++] = 'b';
|
||||
}
|
||||
if (len < PRINTF_NTOA_BUFFER_SIZE) {
|
||||
buf[len++] = '0';
|
||||
}
|
||||
}
|
||||
|
||||
if (len < PRINTF_NTOA_BUFFER_SIZE) {
|
||||
if (negative) {
|
||||
buf[len++] = '-';
|
||||
}
|
||||
else if (flags & FLAGS_PLUS) {
|
||||
buf[len++] = '+'; // ignore the space if the '+' exists
|
||||
}
|
||||
else if (flags & FLAGS_SPACE) {
|
||||
buf[len++] = ' ';
|
||||
}
|
||||
}
|
||||
|
||||
return _out_rev(out, buffer, idx, maxlen, buf, len, width, flags);
|
||||
}
|
||||
|
||||
|
||||
// internal itoa for 'long' type
|
||||
static size_t _ntoa_long(out_fct_type out, char* buffer, size_t idx, size_t maxlen, unsigned long value, bool negative, unsigned long base, unsigned int prec, unsigned int width, unsigned int flags)
|
||||
{
|
||||
char buf[PRINTF_NTOA_BUFFER_SIZE];
|
||||
size_t len = 0U;
|
||||
|
||||
// no hash for 0 values
|
||||
if (!value) {
|
||||
flags &= ~FLAGS_HASH;
|
||||
}
|
||||
|
||||
// write if precision != 0 and value is != 0
|
||||
if (!(flags & FLAGS_PRECISION) || value) {
|
||||
do {
|
||||
const char digit = (char)(value % base);
|
||||
buf[len++] = digit < 10 ? '0' + digit : (flags & FLAGS_UPPERCASE ? 'A' : 'a') + digit - 10;
|
||||
value /= base;
|
||||
} while (value && (len < PRINTF_NTOA_BUFFER_SIZE));
|
||||
}
|
||||
|
||||
return _ntoa_format(out, buffer, idx, maxlen, buf, len, negative, (unsigned int)base, prec, width, flags);
|
||||
}
|
||||
|
||||
|
||||
// internal itoa for 'long long' type
|
||||
#if defined(PRINTF_SUPPORT_LONG_LONG)
|
||||
static size_t _ntoa_long_long(out_fct_type out, char* buffer, size_t idx, size_t maxlen, unsigned long long value, bool negative, unsigned long long base, unsigned int prec, unsigned int width, unsigned int flags)
|
||||
{
|
||||
char buf[PRINTF_NTOA_BUFFER_SIZE];
|
||||
size_t len = 0U;
|
||||
|
||||
// no hash for 0 values
|
||||
if (!value) {
|
||||
flags &= ~FLAGS_HASH;
|
||||
}
|
||||
|
||||
// write if precision != 0 and value is != 0
|
||||
if (!(flags & FLAGS_PRECISION) || value) {
|
||||
do {
|
||||
const char digit = (char)(value % base);
|
||||
buf[len++] = digit < 10 ? '0' + digit : (flags & FLAGS_UPPERCASE ? 'A' : 'a') + digit - 10;
|
||||
value /= base;
|
||||
} while (value && (len < PRINTF_NTOA_BUFFER_SIZE));
|
||||
}
|
||||
|
||||
return _ntoa_format(out, buffer, idx, maxlen, buf, len, negative, (unsigned int)base, prec, width, flags);
|
||||
}
|
||||
#endif // PRINTF_SUPPORT_LONG_LONG
|
||||
|
||||
|
||||
#if defined(PRINTF_SUPPORT_FLOAT)
|
||||
|
||||
#if defined(PRINTF_SUPPORT_EXPONENTIAL)
|
||||
// forward declaration so that _ftoa can switch to exp notation for values > PRINTF_MAX_FLOAT
|
||||
static size_t _etoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags);
|
||||
#endif
|
||||
|
||||
|
||||
// internal ftoa for fixed decimal floating point
|
||||
static size_t _ftoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags)
|
||||
{
|
||||
char buf[PRINTF_FTOA_BUFFER_SIZE];
|
||||
size_t len = 0U;
|
||||
double diff = 0.0;
|
||||
|
||||
// powers of 10
|
||||
static const double pow10[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 };
|
||||
|
||||
// test for special values
|
||||
if (value != value)
|
||||
return _out_rev(out, buffer, idx, maxlen, "nan", 3, width, flags);
|
||||
if (value < -DBL_MAX)
|
||||
return _out_rev(out, buffer, idx, maxlen, "fni-", 4, width, flags);
|
||||
if (value > DBL_MAX)
|
||||
return _out_rev(out, buffer, idx, maxlen, (flags & FLAGS_PLUS) ? "fni+" : "fni", (flags & FLAGS_PLUS) ? 4U : 3U, width, flags);
|
||||
|
||||
// test for very large values
|
||||
// standard printf behavior is to print EVERY whole number digit -- which could be 100s of characters overflowing your buffers == bad
|
||||
if ((value > PRINTF_MAX_FLOAT) || (value < -PRINTF_MAX_FLOAT)) {
|
||||
#if defined(PRINTF_SUPPORT_EXPONENTIAL)
|
||||
return _etoa(out, buffer, idx, maxlen, value, prec, width, flags);
|
||||
#else
|
||||
return 0U;
|
||||
#endif
|
||||
}
|
||||
|
||||
// test for negative
|
||||
bool negative = false;
|
||||
if (value < 0) {
|
||||
negative = true;
|
||||
value = 0 - value;
|
||||
}
|
||||
|
||||
// set default precision, if not set explicitly
|
||||
if (!(flags & FLAGS_PRECISION)) {
|
||||
prec = PRINTF_DEFAULT_FLOAT_PRECISION;
|
||||
}
|
||||
// limit precision to 9, cause a prec >= 10 can lead to overflow errors
|
||||
while ((len < PRINTF_FTOA_BUFFER_SIZE) && (prec > 9U)) {
|
||||
buf[len++] = '0';
|
||||
prec--;
|
||||
}
|
||||
|
||||
int whole = (int)value;
|
||||
double tmp = (value - whole) * pow10[prec];
|
||||
unsigned long frac = (unsigned long)tmp;
|
||||
diff = tmp - frac;
|
||||
|
||||
if (diff > 0.5) {
|
||||
++frac;
|
||||
// handle rollover, e.g. case 0.99 with prec 1 is 1.0
|
||||
if (frac >= pow10[prec]) {
|
||||
frac = 0;
|
||||
++whole;
|
||||
}
|
||||
}
|
||||
else if (diff < 0.5) {
|
||||
}
|
||||
else if ((frac == 0U) || (frac & 1U)) {
|
||||
// if halfway, round up if odd OR if last digit is 0
|
||||
++frac;
|
||||
}
|
||||
|
||||
if (prec == 0U) {
|
||||
diff = value - (double)whole;
|
||||
if ((!(diff < 0.5) || (diff > 0.5)) && (whole & 1)) {
|
||||
// exactly 0.5 and ODD, then round up
|
||||
// 1.5 -> 2, but 2.5 -> 2
|
||||
++whole;
|
||||
}
|
||||
}
|
||||
else {
|
||||
unsigned int count = prec;
|
||||
// now do fractional part, as an unsigned number
|
||||
while (len < PRINTF_FTOA_BUFFER_SIZE) {
|
||||
--count;
|
||||
buf[len++] = (char)(48U + (frac % 10U));
|
||||
if (!(frac /= 10U)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
// add extra 0s
|
||||
while ((len < PRINTF_FTOA_BUFFER_SIZE) && (count-- > 0U)) {
|
||||
buf[len++] = '0';
|
||||
}
|
||||
if (len < PRINTF_FTOA_BUFFER_SIZE) {
|
||||
// add decimal
|
||||
buf[len++] = '.';
|
||||
}
|
||||
}
|
||||
|
||||
// do whole part, number is reversed
|
||||
while (len < PRINTF_FTOA_BUFFER_SIZE) {
|
||||
buf[len++] = (char)(48 + (whole % 10));
|
||||
if (!(whole /= 10)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// pad leading zeros
|
||||
if (!(flags & FLAGS_LEFT) && (flags & FLAGS_ZEROPAD)) {
|
||||
if (width && (negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) {
|
||||
width--;
|
||||
}
|
||||
while ((len < width) && (len < PRINTF_FTOA_BUFFER_SIZE)) {
|
||||
buf[len++] = '0';
|
||||
}
|
||||
}
|
||||
|
||||
if (len < PRINTF_FTOA_BUFFER_SIZE) {
|
||||
if (negative) {
|
||||
buf[len++] = '-';
|
||||
}
|
||||
else if (flags & FLAGS_PLUS) {
|
||||
buf[len++] = '+'; // ignore the space if the '+' exists
|
||||
}
|
||||
else if (flags & FLAGS_SPACE) {
|
||||
buf[len++] = ' ';
|
||||
}
|
||||
}
|
||||
|
||||
return _out_rev(out, buffer, idx, maxlen, buf, len, width, flags);
|
||||
}
|
||||
|
||||
|
||||
#if defined(PRINTF_SUPPORT_EXPONENTIAL)
|
||||
// internal ftoa variant for exponential floating-point type, contributed by Martijn Jasperse <m.jasperse@gmail.com>
|
||||
static size_t _etoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags)
|
||||
{
|
||||
// check for NaN and special values
|
||||
if ((value != value) || (value > DBL_MAX) || (value < -DBL_MAX)) {
|
||||
return _ftoa(out, buffer, idx, maxlen, value, prec, width, flags);
|
||||
}
|
||||
|
||||
// determine the sign
|
||||
const bool negative = value < 0;
|
||||
if (negative) {
|
||||
value = -value;
|
||||
}
|
||||
|
||||
// default precision
|
||||
if (!(flags & FLAGS_PRECISION)) {
|
||||
prec = PRINTF_DEFAULT_FLOAT_PRECISION;
|
||||
}
|
||||
|
||||
// determine the decimal exponent
|
||||
// based on the algorithm by David Gay (https://www.ampl.com/netlib/fp/dtoa.c)
|
||||
union {
|
||||
uint64_t U;
|
||||
double F;
|
||||
} conv;
|
||||
|
||||
conv.F = value;
|
||||
int exp2 = (int)((conv.U >> 52U) & 0x07FFU) - 1023; // effectively log2
|
||||
conv.U = (conv.U & ((1ULL << 52U) - 1U)) | (1023ULL << 52U); // drop the exponent so conv.F is now in [1,2)
|
||||
// now approximate log10 from the log2 integer part and an expansion of ln around 1.5
|
||||
int expval = (int)(0.1760912590558 + exp2 * 0.301029995663981 + (conv.F - 1.5) * 0.289529654602168);
|
||||
// now we want to compute 10^expval but we want to be sure it won't overflow
|
||||
exp2 = (int)(expval * 3.321928094887362 + 0.5);
|
||||
const double z = expval * 2.302585092994046 - exp2 * 0.6931471805599453;
|
||||
const double z2 = z * z;
|
||||
conv.U = (uint64_t)(exp2 + 1023) << 52U;
|
||||
// compute exp(z) using continued fractions, see https://en.wikipedia.org/wiki/Exponential_function#Continued_fractions_for_ex
|
||||
conv.F *= 1 + 2 * z / (2 - z + (z2 / (6 + (z2 / (10 + z2 / 14)))));
|
||||
// correct for rounding errors
|
||||
if (value < conv.F) {
|
||||
expval--;
|
||||
conv.F /= 10;
|
||||
}
|
||||
|
||||
// the exponent format is "%+03d" and largest value is "307", so set aside 4-5 characters
|
||||
unsigned int minwidth = ((expval < 100) && (expval > -100)) ? 4U : 5U;
|
||||
|
||||
// in "%g" mode, "prec" is the number of *significant figures* not decimals
|
||||
if (flags & FLAGS_ADAPT_EXP) {
|
||||
// do we want to fall-back to "%f" mode?
|
||||
if ((value >= 1e-4) && (value < 1e6)) {
|
||||
if ((int)prec > expval) {
|
||||
prec = (unsigned)((int)prec - expval - 1);
|
||||
}
|
||||
else {
|
||||
prec = 0;
|
||||
}
|
||||
flags |= FLAGS_PRECISION; // make sure _ftoa respects precision
|
||||
// no characters in exponent
|
||||
minwidth = 0U;
|
||||
expval = 0;
|
||||
}
|
||||
else {
|
||||
// we use one sigfig for the whole part
|
||||
if ((prec > 0) && (flags & FLAGS_PRECISION)) {
|
||||
--prec;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// will everything fit?
|
||||
unsigned int fwidth = width;
|
||||
if (width > minwidth) {
|
||||
// we didn't fall-back so subtract the characters required for the exponent
|
||||
fwidth -= minwidth;
|
||||
} else {
|
||||
// not enough characters, so go back to default sizing
|
||||
fwidth = 0U;
|
||||
}
|
||||
if ((flags & FLAGS_LEFT) && minwidth) {
|
||||
// if we're padding on the right, DON'T pad the floating part
|
||||
fwidth = 0U;
|
||||
}
|
||||
|
||||
// rescale the float value
|
||||
if (expval) {
|
||||
value /= conv.F;
|
||||
}
|
||||
|
||||
// output the floating part
|
||||
const size_t start_idx = idx;
|
||||
idx = _ftoa(out, buffer, idx, maxlen, negative ? -value : value, prec, fwidth, flags & ~FLAGS_ADAPT_EXP);
|
||||
|
||||
// output the exponent part
|
||||
if (minwidth) {
|
||||
// output the exponential symbol
|
||||
out((flags & FLAGS_UPPERCASE) ? 'E' : 'e', buffer, idx++, maxlen);
|
||||
// output the exponent value
|
||||
idx = _ntoa_long(out, buffer, idx, maxlen, (expval < 0) ? -expval : expval, expval < 0, 10, 0, minwidth-1, FLAGS_ZEROPAD | FLAGS_PLUS);
|
||||
// might need to right-pad spaces
|
||||
if (flags & FLAGS_LEFT) {
|
||||
while (idx - start_idx < width) out(' ', buffer, idx++, maxlen);
|
||||
}
|
||||
}
|
||||
return idx;
|
||||
}
|
||||
#endif // PRINTF_SUPPORT_EXPONENTIAL
|
||||
#endif // PRINTF_SUPPORT_FLOAT
|
||||
|
||||
|
||||
// internal vsnprintf
|
||||
int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const char* format, va_list va)
|
||||
{
|
||||
unsigned int flags, width, precision, n;
|
||||
size_t idx = 0U;
|
||||
|
||||
if (!buffer) {
|
||||
// use null output function
|
||||
out = _out_null;
|
||||
}
|
||||
|
||||
while (*format)
|
||||
{
|
||||
// format specifier? %[flags][width][.precision][length]
|
||||
if (*format != '%') {
|
||||
// no
|
||||
out(*format, buffer, idx++, maxlen);
|
||||
format++;
|
||||
continue;
|
||||
}
|
||||
else {
|
||||
// yes, evaluate it
|
||||
format++;
|
||||
}
|
||||
|
||||
// evaluate flags
|
||||
flags = 0U;
|
||||
do {
|
||||
switch (*format) {
|
||||
case '0': flags |= FLAGS_ZEROPAD; format++; n = 1U; break;
|
||||
case '-': flags |= FLAGS_LEFT; format++; n = 1U; break;
|
||||
case '+': flags |= FLAGS_PLUS; format++; n = 1U; break;
|
||||
case ' ': flags |= FLAGS_SPACE; format++; n = 1U; break;
|
||||
case '#': flags |= FLAGS_HASH; format++; n = 1U; break;
|
||||
default : n = 0U; break;
|
||||
}
|
||||
} while (n);
|
||||
|
||||
// evaluate width field
|
||||
width = 0U;
|
||||
if (_is_digit(*format)) {
|
||||
width = _atoi(&format);
|
||||
}
|
||||
else if (*format == '*') {
|
||||
const int w = va_arg(va, int);
|
||||
if (w < 0) {
|
||||
flags |= FLAGS_LEFT; // reverse padding
|
||||
width = (unsigned int)-w;
|
||||
}
|
||||
else {
|
||||
width = (unsigned int)w;
|
||||
}
|
||||
format++;
|
||||
}
|
||||
|
||||
// evaluate precision field
|
||||
precision = 0U;
|
||||
if (*format == '.') {
|
||||
flags |= FLAGS_PRECISION;
|
||||
format++;
|
||||
if (_is_digit(*format)) {
|
||||
precision = _atoi(&format);
|
||||
}
|
||||
else if (*format == '*') {
|
||||
const int prec = (int)va_arg(va, int);
|
||||
precision = prec > 0 ? (unsigned int)prec : 0U;
|
||||
format++;
|
||||
}
|
||||
}
|
||||
|
||||
// evaluate length field
|
||||
switch (*format) {
|
||||
case 'l' :
|
||||
flags |= FLAGS_LONG;
|
||||
format++;
|
||||
if (*format == 'l') {
|
||||
flags |= FLAGS_LONG_LONG;
|
||||
format++;
|
||||
}
|
||||
break;
|
||||
case 'h' :
|
||||
flags |= FLAGS_SHORT;
|
||||
format++;
|
||||
if (*format == 'h') {
|
||||
flags |= FLAGS_CHAR;
|
||||
format++;
|
||||
}
|
||||
break;
|
||||
#if defined(PRINTF_SUPPORT_PTRDIFF_T)
|
||||
case 't' :
|
||||
flags |= (sizeof(ptrdiff_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG);
|
||||
format++;
|
||||
break;
|
||||
#endif
|
||||
case 'j' :
|
||||
flags |= (sizeof(intmax_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG);
|
||||
format++;
|
||||
break;
|
||||
case 'z' :
|
||||
flags |= (sizeof(size_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG);
|
||||
format++;
|
||||
break;
|
||||
default :
|
||||
break;
|
||||
}
|
||||
|
||||
// evaluate specifier
|
||||
switch (*format) {
|
||||
case 'd' :
|
||||
case 'i' :
|
||||
case 'u' :
|
||||
case 'x' :
|
||||
case 'X' :
|
||||
case 'o' :
|
||||
case 'b' : {
|
||||
// set the base
|
||||
unsigned int base;
|
||||
if (*format == 'x' || *format == 'X') {
|
||||
base = 16U;
|
||||
}
|
||||
else if (*format == 'o') {
|
||||
base = 8U;
|
||||
}
|
||||
else if (*format == 'b') {
|
||||
base = 2U;
|
||||
}
|
||||
else {
|
||||
base = 10U;
|
||||
flags &= ~FLAGS_HASH; // no hash for dec format
|
||||
}
|
||||
// uppercase
|
||||
if (*format == 'X') {
|
||||
flags |= FLAGS_UPPERCASE;
|
||||
}
|
||||
|
||||
// no plus or space flag for u, x, X, o, b
|
||||
if ((*format != 'i') && (*format != 'd')) {
|
||||
flags &= ~(FLAGS_PLUS | FLAGS_SPACE);
|
||||
}
|
||||
|
||||
// ignore '0' flag when precision is given
|
||||
if (flags & FLAGS_PRECISION) {
|
||||
flags &= ~FLAGS_ZEROPAD;
|
||||
}
|
||||
|
||||
// convert the integer
|
||||
if ((*format == 'i') || (*format == 'd')) {
|
||||
// signed
|
||||
if (flags & FLAGS_LONG_LONG) {
|
||||
#if defined(PRINTF_SUPPORT_LONG_LONG)
|
||||
const long long value = va_arg(va, long long);
|
||||
idx = _ntoa_long_long(out, buffer, idx, maxlen, (unsigned long long)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags);
|
||||
#endif
|
||||
}
|
||||
else if (flags & FLAGS_LONG) {
|
||||
const long value = va_arg(va, long);
|
||||
idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned long)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags);
|
||||
}
|
||||
else {
|
||||
const int value = (flags & FLAGS_CHAR) ? (char)va_arg(va, int) : (flags & FLAGS_SHORT) ? (short int)va_arg(va, int) : va_arg(va, int);
|
||||
idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned int)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// unsigned
|
||||
if (flags & FLAGS_LONG_LONG) {
|
||||
#if defined(PRINTF_SUPPORT_LONG_LONG)
|
||||
idx = _ntoa_long_long(out, buffer, idx, maxlen, va_arg(va, unsigned long long), false, base, precision, width, flags);
|
||||
#endif
|
||||
}
|
||||
else if (flags & FLAGS_LONG) {
|
||||
idx = _ntoa_long(out, buffer, idx, maxlen, va_arg(va, unsigned long), false, base, precision, width, flags);
|
||||
}
|
||||
else {
|
||||
const unsigned int value = (flags & FLAGS_CHAR) ? (unsigned char)va_arg(va, unsigned int) : (flags & FLAGS_SHORT) ? (unsigned short int)va_arg(va, unsigned int) : va_arg(va, unsigned int);
|
||||
idx = _ntoa_long(out, buffer, idx, maxlen, value, false, base, precision, width, flags);
|
||||
}
|
||||
}
|
||||
format++;
|
||||
break;
|
||||
}
|
||||
#if defined(PRINTF_SUPPORT_FLOAT)
|
||||
case 'f' :
|
||||
case 'F' :
|
||||
if (*format == 'F') flags |= FLAGS_UPPERCASE;
|
||||
idx = _ftoa(out, buffer, idx, maxlen, va_arg(va, double), precision, width, flags);
|
||||
format++;
|
||||
break;
|
||||
#if defined(PRINTF_SUPPORT_EXPONENTIAL)
|
||||
case 'e':
|
||||
case 'E':
|
||||
case 'g':
|
||||
case 'G':
|
||||
if ((*format == 'g')||(*format == 'G')) flags |= FLAGS_ADAPT_EXP;
|
||||
if ((*format == 'E')||(*format == 'G')) flags |= FLAGS_UPPERCASE;
|
||||
idx = _etoa(out, buffer, idx, maxlen, va_arg(va, double), precision, width, flags);
|
||||
format++;
|
||||
break;
|
||||
#endif // PRINTF_SUPPORT_EXPONENTIAL
|
||||
#endif // PRINTF_SUPPORT_FLOAT
|
||||
case 'c' : {
|
||||
unsigned int l = 1U;
|
||||
// pre padding
|
||||
if (!(flags & FLAGS_LEFT)) {
|
||||
while (l++ < width) {
|
||||
out(' ', buffer, idx++, maxlen);
|
||||
}
|
||||
}
|
||||
// char output
|
||||
out((char)va_arg(va, int), buffer, idx++, maxlen);
|
||||
// post padding
|
||||
if (flags & FLAGS_LEFT) {
|
||||
while (l++ < width) {
|
||||
out(' ', buffer, idx++, maxlen);
|
||||
}
|
||||
}
|
||||
format++;
|
||||
break;
|
||||
}
|
||||
|
||||
case 's' : {
|
||||
const char* p = va_arg(va, char*);
|
||||
unsigned int l = _strnlen_s(p, precision ? precision : (size_t)-1);
|
||||
// pre padding
|
||||
if (flags & FLAGS_PRECISION) {
|
||||
l = (l < precision ? l : precision);
|
||||
}
|
||||
if (!(flags & FLAGS_LEFT)) {
|
||||
while (l++ < width) {
|
||||
out(' ', buffer, idx++, maxlen);
|
||||
}
|
||||
}
|
||||
// string output
|
||||
while ((*p != 0) && (!(flags & FLAGS_PRECISION) || precision--)) {
|
||||
out(*(p++), buffer, idx++, maxlen);
|
||||
}
|
||||
// post padding
|
||||
if (flags & FLAGS_LEFT) {
|
||||
while (l++ < width) {
|
||||
out(' ', buffer, idx++, maxlen);
|
||||
}
|
||||
}
|
||||
format++;
|
||||
break;
|
||||
}
|
||||
|
||||
case 'p' : {
|
||||
width = sizeof(void*) * 2U;
|
||||
flags |= FLAGS_ZEROPAD | FLAGS_UPPERCASE;
|
||||
#if defined(PRINTF_SUPPORT_LONG_LONG)
|
||||
const bool is_ll = sizeof(uintptr_t) == sizeof(long long);
|
||||
if (is_ll) {
|
||||
idx = _ntoa_long_long(out, buffer, idx, maxlen, (uintptr_t)va_arg(va, void*), false, 16U, precision, width, flags);
|
||||
}
|
||||
else {
|
||||
#endif
|
||||
idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned long)((uintptr_t)va_arg(va, void*)), false, 16U, precision, width, flags);
|
||||
#if defined(PRINTF_SUPPORT_LONG_LONG)
|
||||
}
|
||||
#endif
|
||||
format++;
|
||||
break;
|
||||
}
|
||||
|
||||
case '%' :
|
||||
out('%', buffer, idx++, maxlen);
|
||||
format++;
|
||||
break;
|
||||
|
||||
default :
|
||||
out(*format, buffer, idx++, maxlen);
|
||||
format++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// termination
|
||||
out((char)0, buffer, idx < maxlen ? idx : maxlen - 1U, maxlen);
|
||||
|
||||
// return written chars without terminating \0
|
||||
return (int)idx;
|
||||
}
|
@@ -0,0 +1,14 @@
|
||||
#include <stddef.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#pragma pack(push,1)
|
||||
typedef struct {
|
||||
void (*fct)(char character, void* arg);
|
||||
void* arg;
|
||||
} out_fct_wrap_type;
|
||||
#pragma pack(pop)
|
||||
|
||||
typedef void (*out_fct_type)(char character, void* buffer, size_t idx, size_t maxlen);
|
||||
void _out_buffer(char character, void* buffer, size_t idx, size_t maxlen);
|
||||
void _out_null(char character, void* buffer, size_t idx, size_t maxlen);
|
||||
int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const char* format, va_list va);
|
418
programs/develop/ktcc/trunk/libc.obj/source/stdio/format_scan.c
Normal file
418
programs/develop/ktcc/trunk/libc.obj/source/stdio/format_scan.c
Normal file
@@ -0,0 +1,418 @@
|
||||
/*
|
||||
function for format read from any source
|
||||
|
||||
Siemargl formats as http://www.cplusplus.com/reference/cstdio/scanf/, no wchar though
|
||||
http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap05.html is used too
|
||||
|
||||
todo:
|
||||
[characters], [^characters]
|
||||
-%n nothing scanned, filled only if good result
|
||||
-%d, i, u, o, x, p read similar - detect base by prefix 0 or 0x
|
||||
-%a
|
||||
-can overflow unsigned as signed
|
||||
-radix point always '.', no LOCALEs
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
#include <math.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
typedef int (*virtual_getc)(void *sp, const void *obj);
|
||||
typedef void (*virtual_ungetc)(void *sp, int c, const void *obj);
|
||||
|
||||
enum flags_t
|
||||
{
|
||||
flag_unsigned = 0x02,
|
||||
flag_register = 0x04,
|
||||
flag_plus = 0x08,
|
||||
flag_left_just = 0x10,
|
||||
flag_lead_zeros = 0x20,
|
||||
flag_space_plus = 0x40,
|
||||
flag_hash_sign = 0x80,
|
||||
flag_point = 0x100
|
||||
};
|
||||
|
||||
int try_parse_real(long double *real, int ch, const void *src, void *save, virtual_getc vgetc, virtual_ungetc vungetc)
|
||||
// returns 1 if OK, -1 == EOF, -2 parse broken
|
||||
{
|
||||
int sign = 1, have_digits = 0;
|
||||
long long div;
|
||||
|
||||
if (ch == '+')
|
||||
{
|
||||
ch = vgetc(save, src);
|
||||
if (ch == EOF) return EOF;
|
||||
} else
|
||||
if (ch == '-')
|
||||
{
|
||||
sign = -1;
|
||||
ch = vgetc(save, src);
|
||||
if (ch == EOF) return EOF;
|
||||
};
|
||||
*real = 0.0;
|
||||
for (;;) // mantissa before point
|
||||
{
|
||||
// test ch is valid
|
||||
if (isdigit(ch))
|
||||
{
|
||||
*real = *real * 10 + ch - '0';
|
||||
have_digits++;
|
||||
ch = vgetc(save, src);
|
||||
if (ch == EOF || isspace(ch)) break; // ok, just finish num
|
||||
} else
|
||||
if (ch == '.' || ch == 'E' || ch == 'e')
|
||||
{
|
||||
break; // ok
|
||||
}
|
||||
else
|
||||
{
|
||||
vungetc(save, ch, src);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ch != '.' && ch != 'E' && ch != 'e') // ok, just integer part
|
||||
{
|
||||
*real *= sign;
|
||||
if (have_digits)
|
||||
return 1;
|
||||
else
|
||||
return -2;
|
||||
}
|
||||
|
||||
if(ch == '.')
|
||||
{
|
||||
ch = vgetc(save, src);
|
||||
div = 10; // use as divisor
|
||||
for (;;) // mantissa after point
|
||||
{
|
||||
// test ch is valid
|
||||
if (isdigit(ch))
|
||||
{
|
||||
*real += (double)(ch - '0') / div;
|
||||
div *= 10;
|
||||
have_digits++;
|
||||
ch = vgetc(save, src);
|
||||
if (ch == EOF || isspace(ch)) break; // ok, just finish num
|
||||
} else
|
||||
if (ch == 'E' || ch == 'e')
|
||||
{
|
||||
break; // ok
|
||||
}
|
||||
else
|
||||
{
|
||||
vungetc(save, ch, src);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ch != 'E' && ch != 'e') // ok, real as XX.YY
|
||||
{
|
||||
*real *= sign;
|
||||
if (have_digits)
|
||||
return 1;
|
||||
else
|
||||
return -2;
|
||||
}
|
||||
}
|
||||
|
||||
ch = vgetc(save, src);
|
||||
*real *= sign;
|
||||
// exponent
|
||||
sign = 1;
|
||||
if (ch == '+')
|
||||
{
|
||||
ch = vgetc(save, src);
|
||||
if (ch == EOF) return EOF;
|
||||
} else
|
||||
if (ch == '-')
|
||||
{
|
||||
sign = -1;
|
||||
ch = vgetc(save, src);
|
||||
if (ch == EOF) return EOF;
|
||||
};
|
||||
div = 0;
|
||||
for (;;)
|
||||
{
|
||||
// test ch is valid
|
||||
if (isdigit(ch))
|
||||
{
|
||||
div = div * 10 + ch - '0';
|
||||
ch = vgetc(save, src);
|
||||
if (ch == EOF || isspace(ch)) break; // ok, just finish num
|
||||
}
|
||||
else
|
||||
{
|
||||
vungetc(save, ch, src);
|
||||
break;
|
||||
}
|
||||
}
|
||||
div *= sign;
|
||||
*real *= pow(10, div);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int try_parse_int(long long *digit, int ch, const void *src, void *save, virtual_getc vgetc, virtual_ungetc vungetc)
|
||||
{
|
||||
int sign = 1, base = 10, have_digits = 0;
|
||||
|
||||
if (ch == '+')
|
||||
{
|
||||
ch = vgetc(save, src);
|
||||
if (ch == EOF) return EOF;
|
||||
} else
|
||||
if (ch == '-')
|
||||
{
|
||||
sign = -1;
|
||||
ch = vgetc(save, src);
|
||||
if (ch == EOF) return EOF;
|
||||
};
|
||||
|
||||
if (ch == '0') // octal or hex, read next
|
||||
{
|
||||
base = 8;
|
||||
ch = vgetc(save, src);
|
||||
if (ch == EOF || isspace(ch))
|
||||
have_digits++;
|
||||
else
|
||||
if (ch == 'x' || ch == 'X')
|
||||
{
|
||||
base = 16;
|
||||
ch = vgetc(save, src);
|
||||
if (ch == EOF) return EOF;
|
||||
}
|
||||
}
|
||||
*digit = 0;
|
||||
for (;;)
|
||||
{
|
||||
// test ch is valid
|
||||
if ((isdigit(ch) && base == 10) ||
|
||||
(isdigit(ch) && base == 8 && ch < '8') ||
|
||||
(isxdigit(ch) && base == 16))
|
||||
{
|
||||
if (base == 16)
|
||||
{
|
||||
if (ch <= '9') ch-= '0';
|
||||
else
|
||||
if (ch <= 'F') ch = 10 + ch - 'A';
|
||||
else
|
||||
ch = 10 + ch - 'a';
|
||||
}
|
||||
else
|
||||
ch -= '0';
|
||||
*digit = *digit * base + ch;
|
||||
have_digits++;
|
||||
ch = vgetc(save, src);
|
||||
if (ch == EOF || isspace(ch)) break; // ok, just finish num
|
||||
}
|
||||
else if (ch == EOF || isspace(ch))
|
||||
break;
|
||||
else
|
||||
{
|
||||
vungetc(save, ch, src);
|
||||
break;
|
||||
}
|
||||
}
|
||||
*digit *= sign;
|
||||
if (have_digits)
|
||||
return 1;
|
||||
else
|
||||
return -2;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int format_scan(const void *src, const char *fmt, va_list argp, virtual_getc vgetc, virtual_ungetc vungetc)
|
||||
{
|
||||
int i;
|
||||
int length;
|
||||
int fmt1, fmt2; // width, precision
|
||||
size_t pos, posc;
|
||||
const char *fmtc; // first point to %, fmtc points to specifier
|
||||
int ch;
|
||||
int format_flag;
|
||||
int flag_long; // 2 = long double or long long int or wchar
|
||||
int *point_to_n = NULL, nread = 0;
|
||||
int flags; // parsed flags
|
||||
int save = 0;
|
||||
char *arg_str;
|
||||
int *arg_int;
|
||||
long *arg_long;
|
||||
long long *arg_longlong;
|
||||
float *arg_float;
|
||||
double *arg_double;
|
||||
long double *arg_longdouble;
|
||||
long long digit;
|
||||
long double real;
|
||||
|
||||
|
||||
pos = 0;
|
||||
while(*fmt)
|
||||
{
|
||||
while (*fmt && isspace(*fmt)) fmt++; // skip paces in format str
|
||||
|
||||
if (*fmt != '%') // usual char
|
||||
{
|
||||
ch = vgetc(&save, src);
|
||||
if (ch != *fmt++) // char not match format
|
||||
{
|
||||
vungetc(&save, ch, src);
|
||||
break;
|
||||
}
|
||||
pos++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (*(fmt + 1) == '%') // %%
|
||||
{
|
||||
ch = vgetc(&save, src);
|
||||
if (ch != '%') // char not match format
|
||||
{
|
||||
vungetc(&save, ch, src);
|
||||
break;
|
||||
}
|
||||
pos++;
|
||||
fmt += 2;
|
||||
continue;
|
||||
}
|
||||
//checking to containg format in the string
|
||||
fmtc = fmt;
|
||||
posc = pos;
|
||||
|
||||
flags = 0;
|
||||
format_flag = 0;
|
||||
flag_long = 0; // 2 = long double or long long int or wchar
|
||||
|
||||
while(*fmtc != '\0' && !format_flag) // searching end of format
|
||||
{
|
||||
fmtc++; posc++;
|
||||
switch( *fmtc )
|
||||
{
|
||||
case 'a':
|
||||
format_flag = 1;
|
||||
flags |= flag_unsigned;
|
||||
break;
|
||||
case 'c': case 'd': case 'i': case 'e': case 'f': case 'g': case 's': case 'n':
|
||||
format_flag = 1;
|
||||
break;
|
||||
case 'l':
|
||||
flag_long = flag_long ? 2 : 1; // ll.eq.L
|
||||
break;
|
||||
case 'L':
|
||||
flag_long = 2;
|
||||
break;
|
||||
case 'o': case 'u': case 'x': case 'p':
|
||||
format_flag = 1;
|
||||
flags |= flag_unsigned;
|
||||
break;
|
||||
case '*': case '.': // just skip
|
||||
break;
|
||||
default:
|
||||
if(isdigit(*fmtc)) break;
|
||||
goto exit_me; // non format char found - user error
|
||||
}
|
||||
}
|
||||
|
||||
if (format_flag == 0)
|
||||
{
|
||||
goto exit_me; // format char not found - user error
|
||||
}
|
||||
|
||||
fmt1 = 0;
|
||||
fmt2 = 0;
|
||||
if (posc - pos > 1) // try to read width, precision
|
||||
{
|
||||
fmt++;
|
||||
for(i = pos + 1; i < posc; i++)
|
||||
{
|
||||
switch(*fmt)
|
||||
{
|
||||
case '0':
|
||||
if(fmt1 == 0 && (flags & flag_point) == 0) flags |= flag_lead_zeros;
|
||||
case '1': case '2': case '3': case '4':
|
||||
case '5': case '6': case '7': case '8': case '9':
|
||||
if ((flags & flag_point) == 0)
|
||||
fmt1 = fmt1 * 10 + (*fmt -'0');
|
||||
else
|
||||
fmt2 = fmt2 * 10 + (*fmt -'0');
|
||||
break;
|
||||
case '*': // ignoring
|
||||
break;
|
||||
case '.':
|
||||
flags |= flag_point;
|
||||
break;
|
||||
case 'l': case 'L': // valid chars - skip
|
||||
break;
|
||||
default: // must be error
|
||||
goto exit_me; // format char not found - user error
|
||||
}
|
||||
fmt++;
|
||||
}
|
||||
}
|
||||
|
||||
// do real work - format arguments values
|
||||
// skip input spaces
|
||||
do {
|
||||
ch = vgetc(&save, src);
|
||||
if (ch == EOF) goto exit_me;
|
||||
} while (isspace(ch));
|
||||
|
||||
switch(*fmtc)
|
||||
{
|
||||
case 'n':
|
||||
point_to_n = va_arg(argp, int*);
|
||||
vungetc(&save, ch, src);
|
||||
break;
|
||||
case 'c': // read width chars, ever spaces
|
||||
arg_str = va_arg(argp, char*);
|
||||
if (fmt1 == 0) length = 1;
|
||||
else length = fmt1;
|
||||
for (i = 0; i < length;)
|
||||
{
|
||||
*arg_str++ = ch; i++;
|
||||
ch = vgetc(&save, src);
|
||||
if (ch == EOF) break;
|
||||
}
|
||||
if (i < length) goto exit_me; // not enough chars
|
||||
break;
|
||||
case 's':
|
||||
arg_str = va_arg(argp, char*);
|
||||
if (fmt1 == 0) length = 4095; // max string scan 4096
|
||||
else length = fmt1;
|
||||
for (i = 0; i < length; i++)
|
||||
{
|
||||
*arg_str++ = ch;
|
||||
ch = vgetc(&save, src);
|
||||
if (ch == EOF || isspace(ch)) break; // ok, just finish string
|
||||
}
|
||||
*arg_str++ = '\0';
|
||||
break;
|
||||
case 'd': case 'i': case 'u':
|
||||
case 'o': case 'p': case 'x':
|
||||
i = try_parse_int(&digit, ch, src, &save, vgetc, vungetc);
|
||||
if (i < 0) goto exit_me;
|
||||
|
||||
if (flag_long == 0) { arg_int = va_arg(argp, int*); *arg_int = (int)digit; } else
|
||||
if (flag_long == 1) { arg_long = va_arg(argp, long*); *arg_long = (long)digit; } else
|
||||
if (flag_long == 2) { arg_longlong = va_arg(argp, long long*); *arg_longlong = digit; }
|
||||
break;
|
||||
case 'a': case 'A': case 'f': case 'F':
|
||||
case 'e': case 'E':
|
||||
case 'g': case 'G':
|
||||
i = try_parse_real(&real, ch, src, &save, vgetc, vungetc);
|
||||
if (i < 0) goto exit_me;
|
||||
|
||||
if (flag_long == 0) { arg_float = va_arg(argp, float*); *arg_float = (float)real; } else
|
||||
if (flag_long == 1) { arg_double = va_arg(argp, double*); *arg_double = (double)real; } else
|
||||
if (flag_long == 2) { arg_longdouble = va_arg(argp, long double*); *arg_longdouble = real; }
|
||||
break;
|
||||
}
|
||||
|
||||
fmt = fmtc + 1;
|
||||
nread++;
|
||||
}
|
||||
exit_me:
|
||||
if (point_to_n) *point_to_n = nread;
|
||||
return nread;
|
||||
}
|
@@ -0,0 +1,20 @@
|
||||
#include <stdio.h>
|
||||
|
||||
typedef int (*virtual_getc)(void *sp, const void *obj);
|
||||
typedef void (*virtual_ungetc)(void *sp, int c, const void *obj);
|
||||
|
||||
enum flags_t
|
||||
{
|
||||
flag_unsigned = 0x02,
|
||||
flag_register = 0x04,
|
||||
flag_plus = 0x08,
|
||||
flag_left_just = 0x10,
|
||||
flag_lead_zeros = 0x20,
|
||||
flag_space_plus = 0x40,
|
||||
flag_hash_sign = 0x80,
|
||||
flag_point = 0x100
|
||||
};
|
||||
|
||||
char *__scanf_buffer;
|
||||
|
||||
extern int _format_scan(const void *src, const char *fmt, va_list argp, virtual_getc vgetc, virtual_ungetc vungetc);
|
14
programs/develop/ktcc/trunk/libc.obj/source/stdio/fprintf.c
Normal file
14
programs/develop/ktcc/trunk/libc.obj/source/stdio/fprintf.c
Normal file
@@ -0,0 +1,14 @@
|
||||
|
||||
#include "sys/ksys.h"
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
int fprintf(FILE *restrict f, const char *restrict fmt, ...)
|
||||
{
|
||||
int ret;
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
ret = vfprintf(f, fmt, ap);
|
||||
va_end(ap);
|
||||
return ret;
|
||||
}
|
12
programs/develop/ktcc/trunk/libc.obj/source/stdio/fputc.c
Normal file
12
programs/develop/ktcc/trunk/libc.obj/source/stdio/fputc.c
Normal file
@@ -0,0 +1,12 @@
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <sys/ksys.h>
|
||||
|
||||
int fputc(int c, FILE *stream)
|
||||
{
|
||||
if(fwrite(&c, sizeof(char), 1, stream)==1){
|
||||
return c;
|
||||
}else{
|
||||
return EOF;
|
||||
}
|
||||
}
|
11
programs/develop/ktcc/trunk/libc.obj/source/stdio/fputs.c
Normal file
11
programs/develop/ktcc/trunk/libc.obj/source/stdio/fputs.c
Normal file
@@ -0,0 +1,11 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int fputs(const char *str, FILE *stream){
|
||||
size_t str_len = strlen(str);
|
||||
if(str_len == fwrite(str, sizeof(char), str_len , stream)){
|
||||
return str[str_len-1];
|
||||
}else{
|
||||
return EOF;
|
||||
}
|
||||
}
|
38
programs/develop/ktcc/trunk/libc.obj/source/stdio/fread.c
Normal file
38
programs/develop/ktcc/trunk/libc.obj/source/stdio/fread.c
Normal file
@@ -0,0 +1,38 @@
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include "conio.h"
|
||||
#include "sys/ksys.h"
|
||||
|
||||
size_t fread(void *restrict ptr, size_t size, size_t nmemb, FILE *restrict stream) {
|
||||
unsigned bytes_read = 0;
|
||||
unsigned bytes_count = size * nmemb;
|
||||
|
||||
if(!stream){
|
||||
errno = EBADF;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(stream==stdin){
|
||||
con_init();
|
||||
con_gets((char*)ptr, bytes_count+1);
|
||||
return nmemb;
|
||||
}
|
||||
|
||||
else{
|
||||
if(stream->mode & _FILEMODE_R){
|
||||
if(!stream->__ungetc_emu_buff){
|
||||
((char*) ptr)[0]=(char)stream->__ungetc_emu_buff;
|
||||
//debug_printf("Ungetc: %x\n", ((char*) ptr)[0]);
|
||||
}
|
||||
unsigned status = _ksys_file_read_file(stream->name, stream->position, bytes_count, ptr , &bytes_read);
|
||||
if (status) {
|
||||
errno = EIO;
|
||||
stream->error = errno;
|
||||
return 0;
|
||||
}else {
|
||||
stream->position+=bytes_read;
|
||||
}
|
||||
}
|
||||
}
|
||||
return bytes_read;
|
||||
}
|
53
programs/develop/ktcc/trunk/libc.obj/source/stdio/freopen.c
Normal file
53
programs/develop/ktcc/trunk/libc.obj/source/stdio/freopen.c
Normal file
@@ -0,0 +1,53 @@
|
||||
#include "stddef.h"
|
||||
#include "sys/ksys.h"
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define CREATE_FILE() if(_ksys_file_create(_name)){ \
|
||||
errno= EIO; \
|
||||
free(out); \
|
||||
out = NULL; \
|
||||
}
|
||||
|
||||
FILE *freopen(const char *restrict _name, const char *restrict _mode, FILE *restrict out) {
|
||||
if(!_name || !_mode || !out){
|
||||
errno = EINVAL;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (strchr(_mode, 'r')) { out->mode = _FILEMODE_R; }
|
||||
if (strchr(_mode, 'a')) { out->mode = _FILEMODE_A; }
|
||||
if (strchr(_mode, 'w')) { out->mode = _FILEMODE_W; }
|
||||
|
||||
ksys_bdfe_t info;
|
||||
int no_file = _ksys_file_get_info(_name, &info);
|
||||
out->eof=0;
|
||||
out->error=0;
|
||||
out->position=0;
|
||||
out->name = strdup(_name);
|
||||
|
||||
switch (out->mode) {
|
||||
case _FILEMODE_A :
|
||||
if(no_file){
|
||||
CREATE_FILE();
|
||||
}
|
||||
out->position = info.size;
|
||||
break;
|
||||
case _FILEMODE_W :
|
||||
CREATE_FILE();
|
||||
break;
|
||||
case _FILEMODE_R :
|
||||
if(no_file){
|
||||
free(out);
|
||||
out = NULL;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
free(out);
|
||||
out = NULL;
|
||||
break;
|
||||
}
|
||||
return out;
|
||||
}
|
42
programs/develop/ktcc/trunk/libc.obj/source/stdio/fscanf.c
Normal file
42
programs/develop/ktcc/trunk/libc.obj/source/stdio/fscanf.c
Normal file
@@ -0,0 +1,42 @@
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int virtual_getc_file(void *sp, const void *obj)
|
||||
// get next chat from file obj, save point is ptr to string char ptr
|
||||
{
|
||||
FILE *f = (FILE *)obj;
|
||||
int ch = fgetc(f);
|
||||
|
||||
//printf("getc '%c'[%d];", ch, ch);
|
||||
|
||||
return ch;
|
||||
}
|
||||
|
||||
void virtual_ungetc_file(void *sp, int c, const void *obj)
|
||||
// if can, one step back savepoint in s
|
||||
{
|
||||
FILE *f = (FILE *)obj;
|
||||
|
||||
if (f) ungetc(c, f);
|
||||
}
|
||||
|
||||
|
||||
int vfscanf ( FILE * stream, const char * format, va_list arg )
|
||||
{
|
||||
return format_scan(stream, format, arg, &virtual_getc_file, &virtual_ungetc_file);
|
||||
};
|
||||
|
||||
int fscanf ( FILE * stream, const char * format, ...)
|
||||
{
|
||||
va_list arg;
|
||||
int n;
|
||||
va_start(arg, format);
|
||||
|
||||
n = vfscanf(stream, format, arg);
|
||||
|
||||
va_end(arg);
|
||||
return n;
|
||||
}
|
||||
|
||||
|
18
programs/develop/ktcc/trunk/libc.obj/source/stdio/fseek.c
Normal file
18
programs/develop/ktcc/trunk/libc.obj/source/stdio/fseek.c
Normal file
@@ -0,0 +1,18 @@
|
||||
#include <stdio.h>
|
||||
#include <sys/ksys.h>
|
||||
|
||||
int fseek(FILE *stream, long int offset, int whence) {
|
||||
if (whence == SEEK_SET) {
|
||||
stream->position = offset;
|
||||
} else if (whence == SEEK_CUR) {
|
||||
stream->position += offset;
|
||||
} else if (whence == SEEK_END) {
|
||||
ksys_bdfe_t info;
|
||||
if (_ksys_file_get_info(stream->name, &info)) {
|
||||
return -1;
|
||||
}
|
||||
stream->position = info.size + offset;
|
||||
}
|
||||
stream->eof = 0;
|
||||
return 0;
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int fsetpos(FILE *stream, const fpos_t *pos) {
|
||||
stream->position = *pos;
|
||||
stream->eof = 0;
|
||||
return 0;
|
||||
}
|
@@ -0,0 +1,5 @@
|
||||
#include <stdio.h>
|
||||
|
||||
long int ftell(FILE *stream) {
|
||||
return stream->position;
|
||||
}
|
39
programs/develop/ktcc/trunk/libc.obj/source/stdio/fwrite.c
Normal file
39
programs/develop/ktcc/trunk/libc.obj/source/stdio/fwrite.c
Normal file
@@ -0,0 +1,39 @@
|
||||
#include <stdio.h>
|
||||
#include "conio.h"
|
||||
#include <sys/ksys.h>
|
||||
#include <errno.h>
|
||||
|
||||
size_t fwrite(const void *restrict ptr, size_t size, size_t nmemb, FILE *restrict stream) {
|
||||
unsigned bytes_written = 0;
|
||||
unsigned bytes_count = size * nmemb;
|
||||
|
||||
if(!stream){
|
||||
errno = EBADF;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(stream==stdout){
|
||||
con_init();
|
||||
con_write_string((char*)ptr, bytes_count);
|
||||
return nmemb;
|
||||
}
|
||||
else if(stream==stderr){
|
||||
for (size_t i = 0; i < bytes_count; i++) {
|
||||
char c = *(char*)(ptr+i);
|
||||
_ksys_debug_putc(c);
|
||||
}
|
||||
}
|
||||
else{
|
||||
if(stream->mode != _FILEMODE_R){
|
||||
unsigned status = _ksys_file_write_file(stream->name, stream->position, bytes_count, ptr, &bytes_written);
|
||||
if (status != KSYS_FS_ERR_SUCCESS) {
|
||||
errno = EIO;
|
||||
stream->error = errno;
|
||||
return 0;
|
||||
}else {
|
||||
stream->position+=bytes_written;
|
||||
}
|
||||
}
|
||||
}
|
||||
return bytes_written;
|
||||
}
|
12
programs/develop/ktcc/trunk/libc.obj/source/stdio/getchar.c
Normal file
12
programs/develop/ktcc/trunk/libc.obj/source/stdio/getchar.c
Normal file
@@ -0,0 +1,12 @@
|
||||
#include <stdio.h>
|
||||
#include <conio.h>
|
||||
|
||||
int getchar(void) {
|
||||
con_init();
|
||||
char c = 0;
|
||||
con_gets(&c, 2);
|
||||
if (c == 0) {
|
||||
c = EOF;
|
||||
}
|
||||
return c;
|
||||
}
|
16
programs/develop/ktcc/trunk/libc.obj/source/stdio/gets.c
Normal file
16
programs/develop/ktcc/trunk/libc.obj/source/stdio/gets.c
Normal file
@@ -0,0 +1,16 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "conio.h"
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
|
||||
char *gets(char* str)
|
||||
{
|
||||
con_init();
|
||||
if(con_gets(str, STDIO_MAX_MEM)==NULL){
|
||||
errno = EIO;
|
||||
return NULL;
|
||||
}
|
||||
str[strlen(str)-1]='\0';
|
||||
return str;
|
||||
}
|
@@ -0,0 +1,5 @@
|
||||
#include <stdio.h>
|
||||
|
||||
void perror(const char *s) {
|
||||
debug_printf("%s: It's some error, maybe...\n", s);
|
||||
}
|
14
programs/develop/ktcc/trunk/libc.obj/source/stdio/printf.c
Normal file
14
programs/develop/ktcc/trunk/libc.obj/source/stdio/printf.c
Normal file
@@ -0,0 +1,14 @@
|
||||
/* Copyright (C) 2021 Logaev Maxim (turbocat2001), GPLv2 */
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
//#include "format_print.h"
|
||||
|
||||
int printf(const char *format, ...)
|
||||
{
|
||||
va_list arg;
|
||||
va_start(arg, format);
|
||||
return vprintf(format, arg);
|
||||
}
|
14
programs/develop/ktcc/trunk/libc.obj/source/stdio/puts.c
Normal file
14
programs/develop/ktcc/trunk/libc.obj/source/stdio/puts.c
Normal file
@@ -0,0 +1,14 @@
|
||||
/* Copyright (C) 2021 Logaev Maxim (turbocat2001), GPLv2 */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "conio.h"
|
||||
#include "sys/ksys.h"
|
||||
|
||||
int puts(const char *str)
|
||||
{
|
||||
con_init();
|
||||
con_write_asciiz(str);
|
||||
con_write_asciiz("\n");
|
||||
return strlen(str);
|
||||
}
|
@@ -0,0 +1,6 @@
|
||||
#include <stdio.h>
|
||||
#include <sys/ksys.h>
|
||||
|
||||
int remove(const char *name) {
|
||||
return _ksys_file_delete(name);
|
||||
}
|
@@ -0,0 +1,6 @@
|
||||
#include <stdio.h>
|
||||
#include <sys/ksys.h>
|
||||
|
||||
int rename(const char *name, const char *new_name) {
|
||||
return _ksys_file_rename(name, new_name);
|
||||
}
|
@@ -0,0 +1,5 @@
|
||||
#include <stdio.h>
|
||||
|
||||
void rewind(FILE *stream) {
|
||||
stream->position = 0;
|
||||
}
|
65
programs/develop/ktcc/trunk/libc.obj/source/stdio/scanf.c
Normal file
65
programs/develop/ktcc/trunk/libc.obj/source/stdio/scanf.c
Normal file
@@ -0,0 +1,65 @@
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
char *__scanf_buffer = 0;
|
||||
|
||||
int virtual_getc_con(void *sp, const void *obj)
|
||||
// get next chat from string obj, save point is ptr to string char ptr
|
||||
{
|
||||
int ch;
|
||||
const char**spc= (const char**)sp;
|
||||
if (!spc) return EOF; // error
|
||||
|
||||
if (!*spc) *spc = __scanf_buffer; // first call, init savepoint
|
||||
|
||||
while (!**spc) // need to read more
|
||||
{
|
||||
if(!gets(__scanf_buffer)) return EOF;
|
||||
*spc = __scanf_buffer;
|
||||
strcat(__scanf_buffer,"\n"); // imitate delimiter
|
||||
}
|
||||
if (**spc == 26 || **spc == 3) // ^C ^Z end of scan, clear buffer
|
||||
{
|
||||
*spc = __scanf_buffer;
|
||||
*__scanf_buffer = 0;
|
||||
return EOF; // ^C ^Z
|
||||
}
|
||||
|
||||
ch = **spc; (*spc)++ ;
|
||||
|
||||
//printf("getc '%c'[%d];", ch, ch);
|
||||
return ch;
|
||||
}
|
||||
|
||||
void virtual_ungetc_con(void *sp, int c, const void *obj)
|
||||
// if can, one step back savepoint in s
|
||||
{
|
||||
const char**spc= (const char**)sp;
|
||||
|
||||
if (spc && *spc > __scanf_buffer) (*spc)--;
|
||||
//printf("Ungetc '%c'[%d];", c, c);
|
||||
}
|
||||
|
||||
int vscanf ( const char * format, va_list arg )
|
||||
{
|
||||
return format_scan(NULL, format, arg, &virtual_getc_con, &virtual_ungetc_con);
|
||||
};
|
||||
|
||||
int scanf ( const char * format, ...)
|
||||
{
|
||||
va_list arg;
|
||||
int n;
|
||||
va_start(arg, format);
|
||||
|
||||
if(__scanf_buffer == NULL) __scanf_buffer = malloc(4096);
|
||||
if(__scanf_buffer == NULL) return -3;
|
||||
|
||||
*__scanf_buffer = 0;
|
||||
n = vscanf(format, arg);
|
||||
|
||||
va_end(arg);
|
||||
return n;
|
||||
}
|
||||
|
||||
|
@@ -0,0 +1,5 @@
|
||||
#include <stdio.h>
|
||||
|
||||
void setbuf(FILE *restrict stream, char *restrict buf) {
|
||||
return;
|
||||
}
|
@@ -0,0 +1,5 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int setvbuf(FILE *restrict stream, char *restrict buf, int mode, size_t size) {
|
||||
return 0;
|
||||
}
|
13
programs/develop/ktcc/trunk/libc.obj/source/stdio/snprintf.c
Normal file
13
programs/develop/ktcc/trunk/libc.obj/source/stdio/snprintf.c
Normal file
@@ -0,0 +1,13 @@
|
||||
/* Copyright (C) 2021 Logaev Maxim (turbocat2001), GPLv2 */
|
||||
|
||||
//#include "format_print.h"
|
||||
#include <stdio.h>
|
||||
|
||||
int snprintf(char* buffer, size_t count, const char* format, ...)
|
||||
{
|
||||
va_list va;
|
||||
va_start(va, format);
|
||||
const int ret = _vsnprintf(_out_buffer, buffer, count, format, va);
|
||||
va_end(va);
|
||||
return ret;
|
||||
}
|
13
programs/develop/ktcc/trunk/libc.obj/source/stdio/sprintf.c
Normal file
13
programs/develop/ktcc/trunk/libc.obj/source/stdio/sprintf.c
Normal file
@@ -0,0 +1,13 @@
|
||||
/* Copyright (C) 2021 Logaev Maxim (turbocat2001), GPLv2 */
|
||||
|
||||
//#include "format_print.h"
|
||||
#include <stdio.h>
|
||||
|
||||
int sprintf(char* buffer, const char* format, ...)
|
||||
{
|
||||
va_list va;
|
||||
va_start(va, format);
|
||||
const int ret = _vsnprintf(_out_buffer, buffer, (size_t)-1, format, va);
|
||||
va_end(va);
|
||||
return ret;
|
||||
}
|
46
programs/develop/ktcc/trunk/libc.obj/source/stdio/sscanf.c
Normal file
46
programs/develop/ktcc/trunk/libc.obj/source/stdio/sscanf.c
Normal file
@@ -0,0 +1,46 @@
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
int virtual_getc_str(void *sp, const void *obj)
|
||||
// get next chat from string obj, save point is ptr to string char ptr
|
||||
{
|
||||
int ch;
|
||||
const char *s = (const char *)obj;
|
||||
const char**spc= (const char**)sp;
|
||||
if (!s || !spc) return EOF; // error
|
||||
|
||||
if (!*spc) *spc = s; // first call, init savepoint
|
||||
|
||||
if (!**spc) return EOF; // EOS
|
||||
|
||||
ch = **spc; (*spc)++ ;
|
||||
|
||||
return ch;
|
||||
}
|
||||
|
||||
void virtual_ungetc_str(void *sp, int c, const void *obj)
|
||||
// if can, one step back savepoint in s
|
||||
{
|
||||
const char *s = (const char *)obj;
|
||||
const char**spc= (const char**)sp;
|
||||
|
||||
if (s && spc && *spc > s) (*spc)--;
|
||||
}
|
||||
|
||||
int vsscanf ( const char * s, const char * format, va_list arg )
|
||||
{
|
||||
return format_scan(s, format, arg, &virtual_getc_str, &virtual_ungetc_str);
|
||||
};
|
||||
|
||||
int sscanf ( const char * s, const char * format, ...)
|
||||
{
|
||||
va_list arg;
|
||||
int n;
|
||||
va_start(arg, format);
|
||||
|
||||
n = vsscanf(s, format, arg);
|
||||
|
||||
va_end(arg);
|
||||
return n;
|
||||
}
|
||||
|
11
programs/develop/ktcc/trunk/libc.obj/source/stdio/tmpfile.c
Normal file
11
programs/develop/ktcc/trunk/libc.obj/source/stdio/tmpfile.c
Normal file
@@ -0,0 +1,11 @@
|
||||
#include <stdio.h>
|
||||
|
||||
FILE *tmpfile(void) {
|
||||
char name[FILENAME_MAX + 1];
|
||||
|
||||
if (!tmpnam(name)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return fopen(name, "wb+");
|
||||
}
|
16
programs/develop/ktcc/trunk/libc.obj/source/stdio/tmpnam.c
Normal file
16
programs/develop/ktcc/trunk/libc.obj/source/stdio/tmpnam.c
Normal file
@@ -0,0 +1,16 @@
|
||||
#include <stdio.h>
|
||||
#include <sys/ksys.h>
|
||||
#include <string.h>
|
||||
|
||||
static char buf[FILENAME_MAX + 1];
|
||||
static int static_index = 0;
|
||||
|
||||
char *tmpnam(char *name) {
|
||||
ksys_proc_table_t table;
|
||||
_ksys_process_info(&table, -1);
|
||||
|
||||
char *out = name ? name : buf;
|
||||
// PID is also unique for each thread
|
||||
sprintf(out, "/tmp0/1/%x_%x.tmp", table.pid, static_index++);
|
||||
return out;
|
||||
}
|
28
programs/develop/ktcc/trunk/libc.obj/source/stdio/ungetc.c
Normal file
28
programs/develop/ktcc/trunk/libc.obj/source/stdio/ungetc.c
Normal file
@@ -0,0 +1,28 @@
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
// non standard realization - support for virtually change ONLY ONE char
|
||||
|
||||
int ungetc(int c, FILE* file)
|
||||
{
|
||||
int res;
|
||||
|
||||
if(!file){
|
||||
errno = EBADF;
|
||||
return EOF;
|
||||
}
|
||||
|
||||
if (file->mode != _FILEMODE_R){
|
||||
errno = EACCES;
|
||||
return EOF;
|
||||
}
|
||||
|
||||
if (file->position == 0 || c == EOF)
|
||||
{
|
||||
errno = EOF;
|
||||
return EOF;
|
||||
}
|
||||
|
||||
file->__ungetc_emu_buff = c;
|
||||
file->position--;
|
||||
return c;
|
||||
}
|
32
programs/develop/ktcc/trunk/libc.obj/source/stdio/vfprintf.c
Normal file
32
programs/develop/ktcc/trunk/libc.obj/source/stdio/vfprintf.c
Normal file
@@ -0,0 +1,32 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <limits.h>
|
||||
|
||||
int vfprintf(FILE * file, const char *format, va_list arg)
|
||||
{
|
||||
static char *buf=NULL;
|
||||
int printed=0, rc = 0;
|
||||
|
||||
if(!file){
|
||||
errno = EBADF;
|
||||
return errno;
|
||||
}
|
||||
if(!format){
|
||||
errno = EINVAL;
|
||||
return errno;
|
||||
}
|
||||
|
||||
buf = malloc(STDIO_MAX_MEM);
|
||||
|
||||
if(!buf){
|
||||
errno = ENOMEM;
|
||||
return errno;
|
||||
}
|
||||
|
||||
printed = vsnprintf(buf, STDIO_MAX_MEM, format,arg);
|
||||
rc = fwrite(buf, sizeof(char), printed, file);
|
||||
free(buf);
|
||||
return rc;
|
||||
}
|
26
programs/develop/ktcc/trunk/libc.obj/source/stdio/vprintf.c
Normal file
26
programs/develop/ktcc/trunk/libc.obj/source/stdio/vprintf.c
Normal file
@@ -0,0 +1,26 @@
|
||||
/* Copyright (C) 2021 Logaev Maxim (turbocat2001), GPLv2 */
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "conio.h"
|
||||
#include <sys/ksys.h>
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
//#include "format_print.h"
|
||||
|
||||
int vprintf ( const char * format, va_list arg )
|
||||
{
|
||||
int len = 0;
|
||||
char *s = malloc(STDIO_MAX_MEM);
|
||||
if(!s){
|
||||
errno = ENOMEM;
|
||||
return errno;
|
||||
}
|
||||
con_init();
|
||||
len = vsnprintf(s, STDIO_MAX_MEM, format, arg);
|
||||
con_write_string(s, len);
|
||||
free(s);
|
||||
return(len);
|
||||
}
|
@@ -0,0 +1,6 @@
|
||||
//#include "format_print.h"
|
||||
|
||||
int vsnprintf(char* buffer, size_t count, const char* format, va_list va)
|
||||
{
|
||||
return _vsnprintf(_out_buffer, buffer, count, format, va);
|
||||
}
|
@@ -0,0 +1 @@
|
||||
void ___chkstk_ms() {}
|
6
programs/develop/ktcc/trunk/libc.obj/source/stdlib/abs.c
Normal file
6
programs/develop/ktcc/trunk/libc.obj/source/stdlib/abs.c
Normal file
@@ -0,0 +1,6 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
int abs(int a)
|
||||
{
|
||||
return a>0 ? a : -a;
|
||||
}
|
21
programs/develop/ktcc/trunk/libc.obj/source/stdlib/atoi.c
Normal file
21
programs/develop/ktcc/trunk/libc.obj/source/stdlib/atoi.c
Normal file
@@ -0,0 +1,21 @@
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
|
||||
/*
|
||||
** atoi(s) - convert s to integer.
|
||||
*/
|
||||
int atoi(const char *s)
|
||||
{
|
||||
int sign, n;
|
||||
while(isspace(*s)) ++s;
|
||||
sign = 1;
|
||||
|
||||
switch(*s) {
|
||||
case '-': sign = -1;
|
||||
case '+': ++s;
|
||||
}
|
||||
|
||||
n = 0;
|
||||
while(isdigit(*s)) n = 10 * n + *s++ - '0';
|
||||
return (sign * n);
|
||||
}
|
17
programs/develop/ktcc/trunk/libc.obj/source/stdlib/atol.c
Normal file
17
programs/develop/ktcc/trunk/libc.obj/source/stdlib/atol.c
Normal file
@@ -0,0 +1,17 @@
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
|
||||
long atol(const char *s)
|
||||
{
|
||||
long n=0;
|
||||
int neg=0;
|
||||
while (isspace(*s)) s++;
|
||||
switch (*s) {
|
||||
case '-': neg=1;
|
||||
case '+': s++;
|
||||
}
|
||||
/* Compute n as a negative number to avoid overflow on LONG_MIN */
|
||||
while (isdigit(*s))
|
||||
n = 10*n - (*s++ - '0');
|
||||
return neg ? n : -n;
|
||||
}
|
17
programs/develop/ktcc/trunk/libc.obj/source/stdlib/atoll.c
Normal file
17
programs/develop/ktcc/trunk/libc.obj/source/stdlib/atoll.c
Normal file
@@ -0,0 +1,17 @@
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
|
||||
long long atoll(const char *s)
|
||||
{
|
||||
long long n=0;
|
||||
int neg=0;
|
||||
while (isspace(*s)) s++;
|
||||
switch (*s) {
|
||||
case '-': neg=1;
|
||||
case '+': s++;
|
||||
}
|
||||
/* Compute n as a negative number to avoid overflow on LLONG_MIN */
|
||||
while (isdigit(*s))
|
||||
n = 10*n - (*s++ - '0');
|
||||
return neg ? n : -n;
|
||||
}
|
@@ -0,0 +1,6 @@
|
||||
#include <stdlib.h>
|
||||
#include <sys/ksys.h>
|
||||
|
||||
void *calloc(size_t num, size_t size) {
|
||||
return _ksys_alloc(num*size);
|
||||
}
|
6
programs/develop/ktcc/trunk/libc.obj/source/stdlib/div.c
Normal file
6
programs/develop/ktcc/trunk/libc.obj/source/stdlib/div.c
Normal file
@@ -0,0 +1,6 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
div_t div(int num, int den)
|
||||
{
|
||||
return (div_t){ num/den, num%den };
|
||||
}
|
12
programs/develop/ktcc/trunk/libc.obj/source/stdlib/exit.c
Normal file
12
programs/develop/ktcc/trunk/libc.obj/source/stdlib/exit.c
Normal file
@@ -0,0 +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();
|
||||
}
|
@@ -0,0 +1,6 @@
|
||||
#include <stdlib.h>
|
||||
#include <sys/ksys.h>
|
||||
|
||||
void free(void *ptr) {
|
||||
_ksys_free(ptr);
|
||||
}
|
42
programs/develop/ktcc/trunk/libc.obj/source/stdlib/itoa.c
Normal file
42
programs/develop/ktcc/trunk/libc.obj/source/stdlib/itoa.c
Normal file
@@ -0,0 +1,42 @@
|
||||
#include <string.h>
|
||||
#include <sys/ksys.h>
|
||||
|
||||
char *__reverse(char *str)
|
||||
{
|
||||
char tmp, *src, *dst;
|
||||
size_t len;
|
||||
if (str != NULL){
|
||||
len = strlen (str);
|
||||
if (len > 1) {
|
||||
src = str;
|
||||
dst = src + len - 1;
|
||||
while (src < dst) {
|
||||
tmp = *src;
|
||||
*src++ = *dst;
|
||||
*dst-- = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
/* itoa from K&R */
|
||||
void itoa(int n, char s[])
|
||||
{
|
||||
int i, sign;
|
||||
|
||||
if ((sign = n) < 0) /* record sign */
|
||||
n = -n; /* make n positive */
|
||||
i = 0;
|
||||
|
||||
do { /* generate digits in reverse order */
|
||||
s[i++] = n % 10 + '0'; /* get next digit */
|
||||
} while ((n /= 10) > 0); /* delete it */
|
||||
|
||||
if (sign < 0)
|
||||
s[i++] = '-';
|
||||
|
||||
__reverse(s);
|
||||
s[i] = '\0';
|
||||
return;
|
||||
}
|
@@ -0,0 +1,6 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
long labs(long a)
|
||||
{
|
||||
return a>0 ? a : -a;
|
||||
}
|
@@ -0,0 +1,6 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
ldiv_t ldiv(long num, long den)
|
||||
{
|
||||
return (ldiv_t){ num/den, num%den };
|
||||
}
|
@@ -0,0 +1,6 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
long long llabs(long long a)
|
||||
{
|
||||
return a>0 ? a : -a;
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user