kolibri-libc:

- Fixed errno
- Fixed time functions and added new
- Socket functions are now in libraries

git-svn-id: svn://kolibrios.org@8787 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
turbocat 2021-06-09 03:17:46 +00:00
parent 9396efe6cd
commit 050f71484f
25 changed files with 391 additions and 248 deletions

View File

@ -1,5 +1,5 @@
-D
_KOLIBRI_LIBC_OBJ
_BUILD_LIBC
-I
include

View File

@ -20,7 +20,7 @@
#define __UPPER 512
#define __XDIGIT 1024
#ifdef _KOLIBRI_LIBC_OBJ
#ifdef _BUILD_LIBC
extern unsigned short __is[129];
#else
extern unsigned short *__is;

View File

@ -7,7 +7,13 @@
extern "C" {
#endif
extern int errno;
#ifdef _BUILD_LIBC
extern int _errno;
#define errno _errno
#else
extern int* _errno;
#define errno *_errno
#endif
#define EPERM 1 /* Operation not permitted */
#define ENOENT 2 /* No such file or directory */

View File

@ -1,10 +0,0 @@
#ifndef _LIBC_VERSION_H_
#define _LIBC_VERSION_H_
#include <stddef.h>
#define _LIBC_VERSION 8745
extern unsigned _FUNC(_libc_get_version)();
#endif

View File

@ -3,6 +3,7 @@
#define INT_MAX 2147483647
#define INT_MIN -2147483648
#define UINT_MAX (INT_MAX * 2U + 1)

View File

@ -26,7 +26,7 @@ typedef uint64_t uintmax_t;
#define NULL ((void*)0)
#endif
#ifdef _KOLIBRI_LIBC_OBJ
#ifdef _BUILD_LIBC
#define _FUNC(func) func
#else
#define _FUNC(func) (*func)

View File

@ -76,169 +76,16 @@ typedef struct{
}optstruct;
#pragma pack(pop)
static inline
void _conv_socket_err(){
switch(errno){
case 1: errno = ENOBUFS; break;
case 2: errno = EINPROGRESS; break;
case 4: errno = EOPNOTSUPP; break;
case 6: errno = EWOULDBLOCK; break;
case 9: errno = ENOTCONN; break;
case 10: errno = EALREADY; break;
case 11: errno = EINVAL; break;
case 12: errno = EMSGSIZE; break;
case 18: errno = ENOMEM; break;
case 20: errno = EADDRINUSE; break;
case 61: errno = ECONNREFUSED; break;
case 52: errno = ECONNRESET; break;
case 56: errno = EISCONN; break;
case 60: errno = ETIMEDOUT; break;
case 54: errno = ECONNABORTED; break;
default: errno = 0; break;
}
}
static inline
int socket(int domain, int type, int protocol)
{
int socket;
asm_inline(
"int $0x40"
:"=b"(errno), "=a"(socket)
:"a"(75), "b"(0), "c"(domain), "d"(type), "S"(protocol)
);
_conv_socket_err();
return socket;
}
static inline
int close(int socket)
{
int status;
asm_inline(
"int $0x40"
:"=b"(errno), "=a"(status)
:"a"(75), "b"(1), "c"(socket)
);
_conv_socket_err();
return status;
}
static inline
int bind(int socket, const struct sockaddr *addres, int addres_len)
{
int status;
asm_inline(
"int $0x40"
:"=b"(errno), "=a"(status)
:"a"(75), "b"(2), "c"(socket), "d"(addres), "S"(addres_len)
);
_conv_socket_err();
return status;
}
static inline
int listen(int socket, int backlog)
{
int status;
asm_inline(
"int $0x40"
:"=b"(errno), "=a"(status)
:"a"(75), "b"(3), "c"(socket), "d"(backlog)
);
_conv_socket_err();
return status;
}
static inline
int connect(int socket, const struct sockaddr* address, int socket_len)
{
int status;
asm_inline(
"int $0x40"
:"=b"(errno), "=a"(status)
:"a"(75), "b"(4), "c"(socket), "d"(address), "S"(socket_len)
);
_conv_socket_err();
return status;
}
static inline int
accept(int socket, const struct sockaddr *address, int address_len)
{
int new_socket;
asm_inline(
"int $0x40"
:"=b"(errno), "=a"(new_socket)
:"a"(75), "b"(5), "c"(socket), "d"(address), "S"(address_len)
);
_conv_socket_err();
return new_socket;
}
static inline
int send(int socket, const void *message, size_t msg_len, int flag)
{
int status;
asm_inline(
"int $0x40"
:"=b"(errno), "=a"(status)
:"a"(75), "b"(6), "c"(socket), "d"(message), "S"(msg_len), "D"(flag)
);
_conv_socket_err();
return status;
}
static inline
int recv(int socket, void *buffer, size_t buff_len, int flag)
{
int status;
asm_inline(
"int $0x40"
:"=b"(errno), "=a"(status)
:"a"(75), "b"(7), "c"(socket), "d"(buffer), "S"(buff_len), "D"(flag)
);
_conv_socket_err();
return status;
}
static inline
int setsockopt(int socket,const optstruct* opt)
{
int status;
asm_inline(
"int $0x40"
:"=b"(errno), "=a"(status)
:"a"(75), "b"(8), "c"(socket),"d"(opt)
);
_conv_socket_err();
return status;
}
static inline
int getsockopt(int socket, optstruct* opt)
{
int status;
asm_inline(
"int $0x40"
:"=b"(errno), "=a"(status)
:"a"(75), "b"(9), "c"(socket),"d"(opt)
);
_conv_socket_err();
return status;
}
static inline
int socketpair(int *socket1, int *socket2)
{
asm_inline(
"int $0x40"
:"=b"(*socket2), "=a"(*socket1)
:"a"(75), "b"(10)
);
errno=*socket2;
_conv_socket_err();
return *socket1;
}
extern int _FUNC(socket)(int domain, int type, int protocol);
extern int _FUNC(close)(int socket);
extern int _FUNC(bind)(int socket, const struct sockaddr *addres, int addres_len);
extern int _FUNC(listen)(int socket, int backlog);
extern int _FUNC(connect)(int socket, const struct sockaddr* address, int socket_len);
extern int _FUNC(accept)(int socket, const struct sockaddr *address, int address_len);
extern int _FUNC(send)(int socket, const void *message, size_t msg_len, int flag);
extern int _FUNC(recv)(int socket, void *buffer, size_t buff_len, int flag);
extern int _FUNC(setsockopt)(int socket,const optstruct* opt);
extern int _FUNC(getsockopt)(int socket, optstruct* opt);
extern int _FUNC(socketpair)(int *socket1, int *socket2);
#endif //_SOCKET_H_

View File

@ -8,6 +8,7 @@ typedef unsigned long int time_t;
#define clock() _ksys_get_clock()
#define CLOCKS_PER_SEC 100
#pragma pack(push, 1)
struct tm {
int tm_sec; /* seconds after the minute 0-61*/
int tm_min; /* minutes after the hour 0-59 */
@ -19,12 +20,12 @@ struct tm {
int tm_yday; /* days since January 1 0-365 */
int tm_isdst; /* Daylight Saving Time flag */
};
#pragma pack(pop)
extern time_t _FUNC(mktime)(struct tm * timeptr);
extern time_t _FUNC(time)(time_t* timer);
extern struct tm * _FUNC(localtime)(const time_t * timer); /* non-standard! ignore parameter and return just time now, not generate tm_isdst, tm_yday, tm_wday == -1 */
extern struct tm * _FUNC(localtime)(const time_t * timer);
extern double _FUNC(difftime)(time_t end, time_t beginning);
extern struct tm buffertime;
extern char* _FUNC(asctime)(const struct tm *tm);
#endif

View File

@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define READ_MAX 255
@ -28,6 +29,7 @@ int main(int argc, char **argv)
fputs(test_str1+i,f);
char null_term = '\0';
fwrite(&null_term, sizeof(char), 1, f);
printf("Error: %s\n",strerror(errno));
fclose(f);
//copy from testfile.txt to copyfile.txt

View File

@ -13,6 +13,7 @@ int errno;
#include <stdlib.h>
#include <sys/socket.h>
#include <clayer/network.h>
#include <conio.h>
FILE *out=stdout;
@ -61,7 +62,8 @@ int main(int argc , char *argv[])
exit(0);
}
if(out==stdout){
con_set_title("Whois");
con_init();
(*con_set_title)("Whois");
}
get_whois_data(domain , &data);
exit(0);

View File

@ -5,7 +5,7 @@ endif
KPACK=kpack
FASM=fasm
CFLAGS = -c -nostdinc -I../include -DGNUC -D_KOLIBRI_LIBC_OBJ -Os -fno-common -fno-builtin -fno-leading-underscore -fno-pie
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

View File

@ -2,7 +2,7 @@ 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_KOLIBRI_LIBC_OBJ -Os -fno-common -fno-builtin -fno-leading-underscore -fno-pie"
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")

View File

@ -1,9 +1,3 @@
#include <libc_version.h>
unsigned _libc_get_version(){
return _LIBC_VERSION;
}
#include "ctype/is.c"
#include "ctype/tolower.c"
#include "ctype/toupper.c"
@ -15,6 +9,7 @@ unsigned _libc_get_version(){
#include "sys/telldir.c"
#include "sys/closedir.c"
#include "sys/dir.c"
#include "sys/socket.c"
#include "stdio/clearerr.c"
#include "stdio/gets.c"
@ -93,18 +88,14 @@ unsigned _libc_get_version(){
#include "stdlib/free.c"
#include "stdlib/llabs.c"
#include "stdlib/exit.c"
#include "stdlib/mktime.c"
#include "stdlib/atoi.c"
#include "stdlib/localtime.c"
#include "stdlib/labs.c"
#include "stdlib/difftime.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/time.c"
#include "stdlib/strtol.c"
#include "stdlib/rand.c"
@ -118,6 +109,12 @@ unsigned _libc_get_version(){
#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"
@ -135,6 +132,7 @@ __asm__(
".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__(
@ -142,5 +140,6 @@ __asm__(
".include \"setjmp/setjmp.s\""
);
#include "libtcc/libtcc1.c"
#include "stdlib/___chkstk_ms.c"
#include "exports/exports.c"

View File

@ -30,7 +30,7 @@ doit:
fpatan
ret
isanan:
movl $1, errno
movl $1, _errno
fstp %st(0)
fstp %st(0)
fldl nan

View File

@ -1,32 +0,0 @@
#include <time.h>
#include <sys/ksys.h>
struct tm buffertime;
struct tm * localtime (const time_t * timer)
/* non-standard! ignore parameter and return just time now */
{
int kos_date, kos_time;
kos_date = _ksys_get_date();
kos_time = _ksys_get_clock();
int bcd_day = (kos_date >> 16);
int bcd_mon = ((kos_date & 0xFF00) >> 8);
int bcd_year = (kos_date & 0xFF);
buffertime.tm_mday = ((bcd_day & 0xF0)>>4)*10 + (bcd_day & 0x0F);
buffertime.tm_mon = ((bcd_mon & 0xF0)>>4)*10 + (bcd_mon & 0x0F) - 1;
buffertime.tm_year = ((bcd_year & 0xF0)>>4)*10 + (bcd_year & 0x0F);
buffertime.tm_wday = buffertime.tm_yday = buffertime.tm_isdst = -1; /* temporary */
int bcd_sec = (kos_time >> 16);
int bcd_min = ((kos_time & 0xFF00) >> 8);
int bcd_hour = (kos_time & 0xFF);
buffertime.tm_sec = ((bcd_sec & 0xF0)>>4)*10 + (bcd_sec & 0x0F);
buffertime.tm_min = ((bcd_min & 0xF0)>>4)*10 + (bcd_min & 0x0F);
buffertime.tm_hour = ((bcd_hour & 0xF0)>>4)*10 + (bcd_hour & 0x0F);
return &buffertime;
}

View File

@ -1,9 +0,0 @@
#include <time.h>
#include <sys/ksys.h>
time_t time (time_t* timer)
{
time_t t = mktime(localtime(0));
if (timer) *timer = t;
return t;
}

View File

@ -3,7 +3,7 @@
#include <string.h>
#include <errno.h>
int errno;
int _errno;
char* strerror(int err)
{

View File

@ -47,7 +47,6 @@ atoi
atol
atoll
calloc
difftime
div
exit
free
@ -56,20 +55,17 @@ labs
ldiv
llabs
lldiv
localtime
malloc
mktime
realloc
strtol
time
srand
rand
!____STRING____
memcpy
!memcpy
memchr
memcmp
memmove
memset
!memmove
!memset
strncat
strchr
strcat
@ -89,6 +85,7 @@ strspn
strstr
strtok
strxfrm
_errno
!____SYS____
closedir
opendir
@ -101,6 +98,17 @@ mkdir
rmdir
setcwd
getcwd
!____SOCKET____
socket
close
bind
listen
connect
accept
send
recv
setsockopt
socketpair
!____UNISTD____
!____MATH____
acosh
@ -156,5 +164,9 @@ con_set_cursor_height
con_cls
con_get_cursor_pos
con_set_cursor_pos
!___LIBC_VERSION___
_libc_get_version
!____TIME____
mktime
time
localtime
asctime
difftime

View File

@ -0,0 +1,155 @@
#include <sys/socket.h>
static void _conv_socket_err(){
switch(errno){
case 1: errno = ENOBUFS; break;
case 2: errno = EINPROGRESS; break;
case 4: errno = EOPNOTSUPP; break;
case 6: errno = EWOULDBLOCK; break;
case 9: errno = ENOTCONN; break;
case 10: errno = EALREADY; break;
case 11: errno = EINVAL; break;
case 12: errno = EMSGSIZE; break;
case 18: errno = ENOMEM; break;
case 20: errno = EADDRINUSE; break;
case 61: errno = ECONNREFUSED; break;
case 52: errno = ECONNRESET; break;
case 56: errno = EISCONN; break;
case 60: errno = ETIMEDOUT; break;
case 54: errno = ECONNABORTED; break;
default: errno = 0; break;
}
}
int socket(int domain, int type, int protocol)
{
int socket;
asm_inline(
"int $0x40"
:"=b"(errno), "=a"(socket)
:"a"(75), "b"(0), "c"(domain), "d"(type), "S"(protocol)
);
_conv_socket_err();
return socket;
}
int close(int socket)
{
int status;
asm_inline(
"int $0x40"
:"=b"(errno), "=a"(status)
:"a"(75), "b"(1), "c"(socket)
);
_conv_socket_err();
return status;
}
int bind(int socket, const struct sockaddr *addres, int addres_len)
{
int status;
asm_inline(
"int $0x40"
:"=b"(errno), "=a"(status)
:"a"(75), "b"(2), "c"(socket), "d"(addres), "S"(addres_len)
);
_conv_socket_err();
return status;
}
int listen(int socket, int backlog)
{
int status;
asm_inline(
"int $0x40"
:"=b"(errno), "=a"(status)
:"a"(75), "b"(3), "c"(socket), "d"(backlog)
);
_conv_socket_err();
return status;
}
int connect(int socket, const struct sockaddr* address, int socket_len)
{
int status;
asm_inline(
"int $0x40"
:"=b"(errno), "=a"(status)
:"a"(75), "b"(4), "c"(socket), "d"(address), "S"(socket_len)
);
_conv_socket_err();
return status;
}
int accept(int socket, const struct sockaddr *address, int address_len)
{
int new_socket;
asm_inline(
"int $0x40"
:"=b"(errno), "=a"(new_socket)
:"a"(75), "b"(5), "c"(socket), "d"(address), "S"(address_len)
);
_conv_socket_err();
return new_socket;
}
int send(int socket, const void *message, size_t msg_len, int flag)
{
int status;
asm_inline(
"int $0x40"
:"=b"(errno), "=a"(status)
:"a"(75), "b"(6), "c"(socket), "d"(message), "S"(msg_len), "D"(flag)
);
_conv_socket_err();
return status;
}
int recv(int socket, void *buffer, size_t buff_len, int flag)
{
int status;
asm_inline(
"int $0x40"
:"=b"(errno), "=a"(status)
:"a"(75), "b"(7), "c"(socket), "d"(buffer), "S"(buff_len), "D"(flag)
);
_conv_socket_err();
return status;
}
int setsockopt(int socket,const optstruct* opt)
{
int status;
asm_inline(
"int $0x40"
:"=b"(errno), "=a"(status)
:"a"(75), "b"(8), "c"(socket),"d"(opt)
);
_conv_socket_err();
return status;
}
int getsockopt(int socket, optstruct* opt)
{
int status;
asm_inline(
"int $0x40"
:"=b"(errno), "=a"(status)
:"a"(75), "b"(9), "c"(socket),"d"(opt)
);
_conv_socket_err();
return status;
}
int socketpair(int *socket1, int *socket2)
{
asm_inline(
"int $0x40"
:"=b"(*socket2), "=a"(*socket1)
:"a"(75), "b"(10)
);
errno=*socket2;
_conv_socket_err();
return *socket1;
}

View File

@ -0,0 +1,27 @@
#include <errno.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
const char *wday_str[7]={"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
const char *mon_str[12]={"Jan", "Feb", "Mar", "Ap", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
#pragma GCC push_options
#pragma GCC optimize("O0")
char *asctime(const struct tm *tm){
static char time_str[30];
if(tm->tm_wday>7 || tm->tm_wday<1 || tm->tm_mon<1 || tm->tm_mon>12){
errno = EINVAL;
return NULL;
}
snprintf(time_str, 26, "%.3s %.3s%3d %2d:%2d:%2d %d\n",
wday_str[tm->tm_wday-1],
mon_str[tm->tm_mon-1],
tm->tm_mday, tm->tm_hour,
tm->tm_min, tm->tm_sec,
1900 + tm->tm_year
);
return time_str;
}
#pragma GCC pop_options

View File

@ -0,0 +1,108 @@
#include <time.h>
#include <sys/ksys.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#define LEAPOCH (946684800LL + 86400*(31+29))
#define DAYS_PER_400Y (365*400 + 97)
#define DAYS_PER_100Y (365*100 + 24)
#define DAYS_PER_4Y (365*4 + 1)
static int __secs_to_tm(long long t, struct tm *tm)
{
long long days, secs, years;
int remdays, remsecs, remyears;
int qc_cycles, c_cycles, q_cycles;
int months;
int wday, yday, leap;
static const char days_in_month[] = {31,30,31,30,31,31,30,31,30,31,31,29};
// Reject time_t values whose year would overflow int
if (t < INT_MIN * 31622400LL || t > INT_MAX * 31622400LL)
return -1;
secs = t - LEAPOCH;
days = secs / 86400;
remsecs = secs % 86400;
if (remsecs < 0) {
remsecs += 86400;
days--;
}
wday = (3+days)%7;
if (wday < 0) wday += 7;
qc_cycles = days / DAYS_PER_400Y;
remdays = days % DAYS_PER_400Y;
if (remdays < 0) {
remdays += DAYS_PER_400Y;
qc_cycles--;
}
c_cycles = remdays / DAYS_PER_100Y;
if (c_cycles == 4) c_cycles--;
remdays -= c_cycles * DAYS_PER_100Y;
q_cycles = remdays / DAYS_PER_4Y;
if (q_cycles == 25) q_cycles--;
remdays -= q_cycles * DAYS_PER_4Y;
remyears = remdays / 365;
if (remyears == 4) remyears--;
remdays -= remyears * 365;
leap = !remyears && (q_cycles || !c_cycles);
yday = remdays + 31 + 28 + leap;
if (yday >= 365+leap) yday -= 365+leap;
years = remyears + 4*q_cycles + 100*c_cycles + 400LL*qc_cycles;
for (months=0; days_in_month[months] <= remdays; months++)
remdays -= days_in_month[months];
if (months >= 10) {
months -= 12;
years++;
}
if (years+100 > INT_MAX || years+100 < INT_MIN)
return -1;
tm->tm_year = years + 100;
tm->tm_mon = months + 2;
tm->tm_mday = remdays + 1;
tm->tm_wday = wday;
tm->tm_yday = yday;
tm->tm_hour = remsecs / 3600;
tm->tm_min = remsecs / 60 % 60;
tm->tm_sec = remsecs % 60;
return 0;
}
struct tm *__localtime_r(const time_t *restrict t, struct tm *restrict tm)
{
// Reject time_t values whose year would overflow int because
// __secs_to_zone cannot safely handle them.
if (*t < INT_MIN * 31622400LL || *t > INT_MAX * 31622400LL) {
errno = EOVERFLOW;
return NULL;
}
//__secs_to_zone(*t, 0, &tm->tm_isdst, &tm->__tm_gmtoff, 0, &tm->__tm_zone);
if (__secs_to_tm((long long)*t,tm) < 0) {
errno = EOVERFLOW;
return NULL;
}
return tm;
}
struct tm * localtime (const time_t * timer)
{
static struct tm tm;
return __localtime_r(timer, &tm);
}

View File

@ -0,0 +1,34 @@
#include <time.h>
#include <sys/ksys.h>
static struct tm __buffertime;
time_t time(time_t *timer){
int kos_date, kos_time;
kos_date = _ksys_get_date();
kos_time = _ksys_get_clock();
int bcd_day = (kos_date >> 16);
int bcd_mon = ((kos_date & 0xFF00) >> 8);
int bcd_year = (kos_date & 0xFF);
__buffertime.tm_mday = ((bcd_day & 0xF0)>>4)*10 + (bcd_day & 0x0F);
__buffertime.tm_mon = ((bcd_mon & 0xF0)>>4)*10 + (bcd_mon & 0x0F) - 1;
__buffertime.tm_year = ((bcd_year & 0xF0)>>4)*10 + (bcd_year & 0x0F) + 100;
__buffertime.tm_wday = __buffertime.tm_yday = __buffertime.tm_isdst = -1; /* temporary */
int bcd_sec = (kos_time >> 16);
int bcd_min = ((kos_time & 0xFF00) >> 8);
int bcd_hour = (kos_time & 0xFF);
__buffertime.tm_sec = ((bcd_sec & 0xF0)>>4)*10 + (bcd_sec & 0x0F);
__buffertime.tm_min = ((bcd_min & 0xF0)>>4)*10 + (bcd_min & 0x0F);
__buffertime.tm_hour = ((bcd_hour & 0xF0)>>4)*10 + (bcd_hour & 0x0F);
time_t ret = mktime(&__buffertime);
if(timer){
*timer=ret;
}
return ret;
}