From 9bafc8aa7b6fcc1d3824da7fd8cd69f5a0803e46 Mon Sep 17 00:00:00 2001 From: siemargl Date: Sat, 30 Apr 2016 13:50:04 +0000 Subject: [PATCH] bugfixing git-svn-id: svn://kolibrios.org@6412 a494cfbc-eb01-0410-851d-a64ba20cac60 --- programs/develop/ktcc/trunk/libc/build.bat | 4 +- .../develop/ktcc/trunk/libc/include/float.h | 57 +++++++ .../develop/ktcc/trunk/libc/include/stdarg.h | 15 ++ .../develop/ktcc/trunk/libc/include/stdbool.h | 10 ++ .../develop/ktcc/trunk/libc/include/stddef.h | 21 +++ .../develop/ktcc/trunk/libc/include/stdio.h | 4 +- .../develop/ktcc/trunk/libc/include/stdlib.h | 4 +- .../develop/ktcc/trunk/libc/include/string.h | 1 + .../develop/ktcc/trunk/libc/include/varargs.h | 11 ++ .../ktcc/trunk/libc/memory/memalloc.asm | 4 +- .../develop/ktcc/trunk/libc/stdio/fopen.c | 2 +- .../ktcc/trunk/libc/stdio/format_print.c | 153 +++++++++--------- .../develop/ktcc/trunk/libc/stdio/fprintf.c | 1 + .../develop/ktcc/trunk/libc/stdio/fscanf.c | 5 +- .../develop/ktcc/trunk/libc/stdio/fseek.c | 4 +- .../develop/ktcc/trunk/libc/stdio/printf.c | 2 +- .../develop/ktcc/trunk/libc/stdlib/itoa.c | 11 +- .../develop/ktcc/trunk/libc/stdlib/itoab.c | 11 +- .../develop/ktcc/trunk/libc/string/strcoll.c | 2 + .../develop/ktcc/trunk/libc/string/strdup.c | 5 +- .../develop/ktcc/trunk/libc/string/strnbrk.c | 4 +- .../develop/ktcc/trunk/libc/string/strrev.c | 12 ++ .../develop/ktcc/trunk/libc/string/strstr.c | 5 +- 23 files changed, 242 insertions(+), 106 deletions(-) create mode 100644 programs/develop/ktcc/trunk/libc/include/float.h create mode 100644 programs/develop/ktcc/trunk/libc/include/stdarg.h create mode 100644 programs/develop/ktcc/trunk/libc/include/stdbool.h create mode 100644 programs/develop/ktcc/trunk/libc/include/stddef.h create mode 100644 programs/develop/ktcc/trunk/libc/include/varargs.h create mode 100644 programs/develop/ktcc/trunk/libc/string/strrev.c diff --git a/programs/develop/ktcc/trunk/libc/build.bat b/programs/develop/ktcc/trunk/libc/build.bat index 1547ff2427..9d5fc9a16f 100644 --- a/programs/develop/ktcc/trunk/libc/build.bat +++ b/programs/develop/ktcc/trunk/libc/build.bat @@ -6,8 +6,8 @@ echo #################################################### rem #### CONFIG SECTION #### set LIBNAME=libck.a set INCLUDE=include -set CC=kos32-tcc -set CFLAGS=-c -nostdinc -DGNUC -I"%cd%\%INCLUDE%" +set CC=kos32-tcc +set CFLAGS=-c -nostdinc -DGNUC -I"%cd%\%INCLUDE%" -Wall set AR=kos32-ar set ASM=fasm set dirs=stdio memory kolibrisys string stdlib diff --git a/programs/develop/ktcc/trunk/libc/include/float.h b/programs/develop/ktcc/trunk/libc/include/float.h new file mode 100644 index 0000000000..5f1c6f73c7 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/float.h @@ -0,0 +1,57 @@ +#ifndef _FLOAT_H_ +#define _FLOAT_H_ + +#define FLT_RADIX 2 + +/* IEEE float */ +#define FLT_MANT_DIG 24 +#define FLT_DIG 6 +#define FLT_ROUNDS 1 +#define FLT_EPSILON 1.19209290e-07F +#define FLT_MIN_EXP (-125) +#define FLT_MIN 1.17549435e-38F +#define FLT_MIN_10_EXP (-37) +#define FLT_MAX_EXP 128 +#define FLT_MAX 3.40282347e+38F +#define FLT_MAX_10_EXP 38 + +/* IEEE double */ +#define DBL_MANT_DIG 53 +#define DBL_DIG 15 +#define DBL_EPSILON 2.2204460492503131e-16 +#define DBL_MIN_EXP (-1021) +#define DBL_MIN 2.2250738585072014e-308 +#define DBL_MIN_10_EXP (-307) +#define DBL_MAX_EXP 1024 +#define DBL_MAX 1.7976931348623157e+308 +#define DBL_MAX_10_EXP 308 + +/* horrible intel long double */ +#ifdef __i386__ + +#define LDBL_MANT_DIG 64 +#define LDBL_DIG 18 +#define LDBL_EPSILON 1.08420217248550443401e-19L +#define LDBL_MIN_EXP (-16381) +#define LDBL_MIN 3.36210314311209350626e-4932L +#define LDBL_MIN_10_EXP (-4931) +#define LDBL_MAX_EXP 16384 +#define LDBL_MAX 1.18973149535723176502e+4932L +#define LDBL_MAX_10_EXP 4932 + +#else + +/* same as IEEE double */ +#define LDBL_MANT_DIG 53 +#define LDBL_DIG 15 +#define LDBL_EPSILON 2.2204460492503131e-16 +#define LDBL_MIN_EXP (-1021) +#define LDBL_MIN 2.2250738585072014e-308 +#define LDBL_MIN_10_EXP (-307) +#define LDBL_MAX_EXP 1024 +#define LDBL_MAX 1.7976931348623157e+308 +#define LDBL_MAX_10_EXP 308 + +#endif + +#endif /* _FLOAT_H_ */ diff --git a/programs/develop/ktcc/trunk/libc/include/stdarg.h b/programs/develop/ktcc/trunk/libc/include/stdarg.h new file mode 100644 index 0000000000..d562d6ddcc --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/stdarg.h @@ -0,0 +1,15 @@ +#ifndef _STDARG_H +#define _STDARG_H + +typedef char *va_list; + +/* only correct for i386 */ +#define va_start(ap,last) ap = ((char *)&(last)) + ((sizeof(last)+3)&~3) +#define va_arg(ap,type) (ap += (sizeof(type)+3)&~3, *(type *)(ap - ((sizeof(type)+3)&~3))) +#define va_end(ap) + +/* fix a buggy dependency on GCC in libio.h */ +typedef va_list __gnuc_va_list; +#define _VA_LIST_DEFINED + +#endif diff --git a/programs/develop/ktcc/trunk/libc/include/stdbool.h b/programs/develop/ktcc/trunk/libc/include/stdbool.h new file mode 100644 index 0000000000..6ed13a611a --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/stdbool.h @@ -0,0 +1,10 @@ +#ifndef _STDBOOL_H +#define _STDBOOL_H + +/* ISOC99 boolean */ + +#define bool _Bool +#define true 1 +#define false 0 + +#endif /* _STDBOOL_H */ diff --git a/programs/develop/ktcc/trunk/libc/include/stddef.h b/programs/develop/ktcc/trunk/libc/include/stddef.h new file mode 100644 index 0000000000..89bd7b0941 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/stddef.h @@ -0,0 +1,21 @@ +#ifndef _STDDEF_H +#define _STDDEF_H + +#define NULL ((void *)0) +typedef __SIZE_TYPE__ size_t; +typedef __WCHAR_TYPE__ wchar_t; +typedef __PTRDIFF_TYPE__ ptrdiff_t; +#define offsetof(type, field) ((size_t) &((type *)0)->field) + +/* need to do that because of glibc 2.1 bug (should have a way to test + presence of 'long long' without __GNUC__, or TCC should define + __GNUC__ ? */ +#if !defined(__int8_t_defined) && !defined(__dietlibc__) +#define __int8_t_defined +typedef char int8_t; +typedef short int int16_t; +typedef int int32_t; +typedef long long int int64_t; +#endif + +#endif diff --git a/programs/develop/ktcc/trunk/libc/include/stdio.h b/programs/develop/ktcc/trunk/libc/include/stdio.h index b267a09ccf..aa5e5b612f 100644 --- a/programs/develop/ktcc/trunk/libc/include/stdio.h +++ b/programs/develop/ktcc/trunk/libc/include/stdio.h @@ -10,7 +10,7 @@ typedef char *va_list; #define va_end(ap) (ap = (va_list)0) #define NULL ((void*)0) -//extern int stdcall format_print(char *dest, size_t maxlen, const char *fmt0, va_list argp); +int format_print(char *dest, size_t maxlen,const char *fmt0, va_list argp); typedef struct { char* buffer; @@ -54,4 +54,6 @@ extern int vsnprintf(char *dest, size_t size,const char *format,va_list ap); extern int cdecl snprintf(char *dest, size_t size, const char *format,...); extern int cdecl sprintf(char *dest,const char *format,...); +#define getc(a) fgetc(a) + #endif diff --git a/programs/develop/ktcc/trunk/libc/include/stdlib.h b/programs/develop/ktcc/trunk/libc/include/stdlib.h index 608113357d..d1d91f2579 100644 --- a/programs/develop/ktcc/trunk/libc/include/stdlib.h +++ b/programs/develop/ktcc/trunk/libc/include/stdlib.h @@ -11,8 +11,8 @@ extern int atoib(char *s,int b); extern int atoi(char *s); extern unsigned char tolower(unsigned char c); extern unsigned char toupper(unsigned char c); -extern void itoab(int n,char* s,int b); -extern void itoa(int n,char* s); +extern char *itoab(int n,char* s,int b); +extern char *itoa(int n,char* s); extern void* stdcall malloc(dword size); extern void stdcall free(void *pointer); diff --git a/programs/develop/ktcc/trunk/libc/include/string.h b/programs/develop/ktcc/trunk/libc/include/string.h index 07a49a1c5b..cf50c5872d 100644 --- a/programs/develop/ktcc/trunk/libc/include/string.h +++ b/programs/develop/ktcc/trunk/libc/include/string.h @@ -22,4 +22,5 @@ extern char* strstr(const char*,const char*); extern char* strtok(char*,const char*); extern int strxfrm(char*,const char*,int); extern char* strdup(const char*); +extern char* strrev(char *p); #endif diff --git a/programs/develop/ktcc/trunk/libc/include/varargs.h b/programs/develop/ktcc/trunk/libc/include/varargs.h new file mode 100644 index 0000000000..daee29e874 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/varargs.h @@ -0,0 +1,11 @@ +#ifndef _VARARGS_H +#define _VARARGS_H + +#include + +#define va_dcl +#define va_alist __va_alist +#undef va_start +#define va_start(ap) ap = __builtin_varargs_start + +#endif diff --git a/programs/develop/ktcc/trunk/libc/memory/memalloc.asm b/programs/develop/ktcc/trunk/libc/memory/memalloc.asm index a60cb0dfb0..ee64ef9d46 100644 --- a/programs/develop/ktcc/trunk/libc/memory/memalloc.asm +++ b/programs/develop/ktcc/trunk/libc/memory/memalloc.asm @@ -31,8 +31,8 @@ realloc: mov ebx,20 mov eax,68 - mov ecx,[esp+4] - mov edx,[esp+8] + mov edx,[esp+4] ; pointer + mov ecx,[esp+8] ; size int 0x40 ret 8 diff --git a/programs/develop/ktcc/trunk/libc/stdio/fopen.c b/programs/develop/ktcc/trunk/libc/stdio/fopen.c index 6697de3c6a..2b63bfea00 100644 --- a/programs/develop/ktcc/trunk/libc/stdio/fopen.c +++ b/programs/develop/ktcc/trunk/libc/stdio/fopen.c @@ -128,7 +128,7 @@ FILE* fopen(const char* filename, const char *mode) res->filesize=0; res->filepos=0; res->mode=imode; - res->filename=getfullpath(filename); + res->filename=(char*)getfullpath(filename); if ((imode==FILE_OPEN_READ) || (imode==FILE_OPEN_APPEND)) { diff --git a/programs/develop/ktcc/trunk/libc/stdio/format_print.c b/programs/develop/ktcc/trunk/libc/stdio/format_print.c index 7e78b13ecd..72c285d477 100644 --- a/programs/develop/ktcc/trunk/libc/stdio/format_print.c +++ b/programs/develop/ktcc/trunk/libc/stdio/format_print.c @@ -414,9 +414,11 @@ int format_print(char *dest, size_t maxlen,const char *fmt0, va_list argp) flag_unsigned=1; break; case 'x': + case 'p': format_flag=1; break; case 'X': + case 'P': flag_register=1; format_flag=1; break; @@ -451,7 +453,7 @@ int format_print(char *dest, size_t maxlen,const char *fmt0, va_list argp) { case 'c': case 'C': - if ((pos+1) maxlen) + length = maxlen - pos; + memcpy(s,str,length); + s=s+length;pos=pos+length; + break; case 'd': case 'D': case 'i': case 'I': - if (flag_long==0) {intdigit=va_arg(argp,int);} - if (flag_long==1) {intdigit=va_arg(argp,long);} - if (flag_long==2) {intdigit=va_arg(argp,long long);} - //intdigit=*((long*)argp); - //argp=argp+4; - if ((intdigit>0) && (flag_plus==1) && (pos+10) && (flag_plus==1) && (pos+1<=maxlen)) + { + *s='+'; + s++; + pos++; + } + length=formatted_long_to_string(intdigit,0,buf); + if (pos + length > maxlen) + length = maxlen - pos; + memcpy(s,buf,length); + s=s+length;pos=pos+length; + break; case 'o': - if (flag_long==0) {intdigit=va_arg(argp,int);} - if (flag_long==1) {intdigit=va_arg(argp,long);} - if (flag_long==2) {intdigit=va_arg(argp,long long);} - //intdigit=*((long int *)argp); - //argp=argp+4; + if (flag_long==0) {intdigit=va_arg(argp,int);} + if (flag_long==1) {intdigit=va_arg(argp,long);} + if (flag_long==2) {intdigit=va_arg(argp,long long);} + //intdigit=*((long int *)argp); + //argp=argp+4; - length=formatted_octa_to_string(intdigit,0,flag_register,buf); - if ((pos+length) maxlen) + length = maxlen - pos; + memcpy(s,buf,length); + s=s+length;pos=pos+length; + break; case 'u': case 'U': - if (flag_long==0) {intdigit=va_arg(argp,int);} - if (flag_long==1) {intdigit=va_arg(argp,long int);} - if (flag_long==2) {intdigit=va_arg(argp,long long);} + if (flag_long==0) {intdigit=va_arg(argp,int);} + if (flag_long==1) {intdigit=va_arg(argp,long int);} + if (flag_long==2) {intdigit=va_arg(argp,long long);} - if (flag_unsigned==1) { - if (intdigit<0) {intdigit=-intdigit;} - } + if (flag_unsigned==1) { + if (intdigit<0) {intdigit=-intdigit;} + } - length=formatted_long_to_string(intdigit,0,buf); - if ((pos+length) maxlen) + length = maxlen - pos; + memcpy(s,buf,length); + s=s+length;pos=pos+length; + break; + case 'p': + case 'P': case 'x': case 'X': - if (flag_long==0) {intdigit=va_arg(argp,int);} - if (flag_long==1) {intdigit=va_arg(argp,long);} - if (flag_long==2) {intdigit=va_arg(argp,long long);} - //intdigit=*((long int *)argp); - //argp=argp+4; + if (flag_long==0) {intdigit=va_arg(argp,int);} + if (flag_long==1) {intdigit=va_arg(argp,long);} + if (flag_long==2) {intdigit=va_arg(argp,long long);} + //intdigit=*((long int *)argp); + //argp=argp+4; - length=formatted_hex_to_string(intdigit,0,flag_register,buf); - if ((pos+length) maxlen) + length = maxlen - pos; + memcpy(s,buf,length); + s=s+length;pos=pos+length; + break; case 'z': case 'Z': - intdigit=va_arg(argp,size_t); + intdigit=va_arg(argp,size_t); - if (flag_unsigned==1) { - if (intdigit<0) {intdigit=-intdigit;} - } + if (flag_unsigned==1) { + if (intdigit<0) {intdigit=-intdigit;} + } - length=formatted_long_to_string(intdigit,0,buf); - if ((pos+length) maxlen) + length = maxlen - pos; + memcpy(s,buf,length); + s=s+length;pos=pos+length; + break; default:; } @@ -717,10 +715,7 @@ int format_print(char *dest, size_t maxlen,const char *fmt0, va_list argp) } else { - if (*fmt=='\0') {break;} - *s=*fmt; - fmt++; - s++; + if (!(*s++ = *fmt++)) break; pos++; } exit_check:; diff --git a/programs/develop/ktcc/trunk/libc/stdio/fprintf.c b/programs/develop/ktcc/trunk/libc/stdio/fprintf.c index a1129bbed5..5d42c1c31e 100644 --- a/programs/develop/ktcc/trunk/libc/stdio/fprintf.c +++ b/programs/develop/ktcc/trunk/libc/stdio/fprintf.c @@ -1,4 +1,5 @@ #include +#include int format_print(char *dest, size_t maxlen, const char *fmt,va_list argp); diff --git a/programs/develop/ktcc/trunk/libc/stdio/fscanf.c b/programs/develop/ktcc/trunk/libc/stdio/fscanf.c index f572f9bc00..9864a25dce 100644 --- a/programs/develop/ktcc/trunk/libc/stdio/fscanf.c +++ b/programs/develop/ktcc/trunk/libc/stdio/fscanf.c @@ -4,7 +4,7 @@ void skipspaces(FILE* file) int c; while(1) { - c=getc(file); + c=fgetc(file); if (c!=' ' && c!='\r' && c!='\n') { ungetc(c,file); @@ -12,6 +12,7 @@ void skipspaces(FILE* file) } } } + int fscanf(FILE* file,const char* format, ...) { int res; @@ -34,7 +35,7 @@ int fscanf(FILE* file,const char* format, ...) c=fgetc(file); if (c!=*format) { - fungetc(c,file); + ungetc(c,file); return -1; } format++; diff --git a/programs/develop/ktcc/trunk/libc/stdio/fseek.c b/programs/develop/ktcc/trunk/libc/stdio/fseek.c index 1686904b32..620b043403 100644 --- a/programs/develop/ktcc/trunk/libc/stdio/fseek.c +++ b/programs/develop/ktcc/trunk/libc/stdio/fseek.c @@ -1,11 +1,13 @@ #include int fseek(FILE* file,long offset,int origin) { + fpos_t pos; if (origin==SEEK_CUR) offset+=file->filepos; else if (origin==SEEK_END) offset+=file->filesize; else if (origin!=SEEK_SET) return EOF; - return fsetpos(file,offset); + pos = offset; + return fsetpos(file, &pos); } \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc/stdio/printf.c b/programs/develop/ktcc/trunk/libc/stdio/printf.c index da3037b86d..ed19a91f87 100644 --- a/programs/develop/ktcc/trunk/libc/stdio/printf.c +++ b/programs/develop/ktcc/trunk/libc/stdio/printf.c @@ -1,4 +1,4 @@ - +#include #include #include #include diff --git a/programs/develop/ktcc/trunk/libc/stdlib/itoa.c b/programs/develop/ktcc/trunk/libc/stdlib/itoa.c index 35b3bd455c..529243fcf4 100644 --- a/programs/develop/ktcc/trunk/libc/stdlib/itoa.c +++ b/programs/develop/ktcc/trunk/libc/stdlib/itoa.c @@ -1,11 +1,12 @@ -#include "stdio.h" -#include "stdlib.h" -#include "ctype.h" +#include +#include +#include +#include /* ** itoa(n,s) - Convert n to characters in s */ -void itoa(int n,char* s) +char* itoa(int n,char* s) { int sign; char *ptr; @@ -16,6 +17,6 @@ void itoa(int n,char* s) } while ((n = n / 10) > 0); if (sign < 0) *ptr++ = '-'; *ptr = '\0'; - reverse(s); + return strrev(s); } diff --git a/programs/develop/ktcc/trunk/libc/stdlib/itoab.c b/programs/develop/ktcc/trunk/libc/stdlib/itoab.c index ac5fb114e6..6e639058f4 100644 --- a/programs/develop/ktcc/trunk/libc/stdlib/itoab.c +++ b/programs/develop/ktcc/trunk/libc/stdlib/itoab.c @@ -1,12 +1,13 @@ -#include "stdio.h" -#include "stdlib.h" -#include "ctype.h" +#include +#include +#include +#include /* ** itoab(n,s,b) - Convert "unsigned" n to characters in s using base b. ** NOTE: This is a non-standard function. */ -void itoab(int n,char* s,int b) +char* itoab(int n,char* s,int b) { char *ptr; int lowbit; @@ -20,5 +21,5 @@ void itoab(int n,char* s,int b) ++ptr; } while(n /= b); *ptr = 0; - reverse (s); + return strrev(s); } diff --git a/programs/develop/ktcc/trunk/libc/string/strcoll.c b/programs/develop/ktcc/trunk/libc/string/strcoll.c index dcdbdbe0ab..6e96af7833 100644 --- a/programs/develop/ktcc/trunk/libc/string/strcoll.c +++ b/programs/develop/ktcc/trunk/libc/string/strcoll.c @@ -1,3 +1,5 @@ +#include + int strcoll(const char* string1,const char* string2) { return strcmp(string1,string2); diff --git a/programs/develop/ktcc/trunk/libc/string/strdup.c b/programs/develop/ktcc/trunk/libc/string/strdup.c index c0244f800e..6f53813407 100644 --- a/programs/develop/ktcc/trunk/libc/string/strdup.c +++ b/programs/develop/ktcc/trunk/libc/string/strdup.c @@ -1,4 +1,7 @@ -char* strdup(char* str) +#include +#include + +char* strdup(const char* str) { char* res; int len; diff --git a/programs/develop/ktcc/trunk/libc/string/strnbrk.c b/programs/develop/ktcc/trunk/libc/string/strnbrk.c index 1693228eeb..f77d3e8c11 100644 --- a/programs/develop/ktcc/trunk/libc/string/strnbrk.c +++ b/programs/develop/ktcc/trunk/libc/string/strnbrk.c @@ -1,13 +1,13 @@ char* strpbrk(const char* string, const char* strCharSet) { - char* temp; + const char* temp; while (*string!='\0') { temp=strCharSet; while (*temp!='\0') { if (*string==*temp) - return string; + return (char*)string; temp++; } string++; diff --git a/programs/develop/ktcc/trunk/libc/string/strrev.c b/programs/develop/ktcc/trunk/libc/string/strrev.c new file mode 100644 index 0000000000..8b9cdc86b8 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/string/strrev.c @@ -0,0 +1,12 @@ +char* strrev(char *p) +{ + char *q = p, *res = p, z; + while(q && *q) ++q; /* find eos */ + for(--q; p < q; ++p, --q) + { + z = *p; + *p = *q; + *q = z; + } + return res; +} \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc/string/strstr.c b/programs/develop/ktcc/trunk/libc/string/strstr.c index 15039bf544..45a8da2001 100644 --- a/programs/develop/ktcc/trunk/libc/string/strstr.c +++ b/programs/develop/ktcc/trunk/libc/string/strstr.c @@ -1,11 +1,12 @@ -extern int strncmp(char* s1,char* s2,int len); +#include + char* strstr(const char* s, const char* find) { int len; len=strlen(find); while (1) { - if (strncmp(s,find,len)==0) return s; + if (strncmp(s,find,len)==0) return (char*)s; if (*s=='\0') return (char*) 0; s++;