libc.obj:

- Formatted by clang-format (WebKit-style).
- Removed unnecessary errno linux. 
- Added KOS error codes. 
- String functions have been replaced with more optimal ones for x86. 
- Changed wrappers for 70 sysfunction.

git-svn-id: svn://kolibrios.org@9765 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
turbocat
2022-04-15 09:00:55 +00:00
parent dff8056cd6
commit cde4fa851d
68 changed files with 1628 additions and 1762 deletions

View File

@@ -1 +1 @@
void ___chkstk_ms() {}
void ___chkstk_ms() { }

View File

@@ -2,5 +2,5 @@
int abs(int a)
{
return a>0 ? a : -a;
return a > 0 ? a : -a;
}

View File

@@ -1,14 +1,13 @@
#include <stdio.h>
#include <stdlib.h>
#pragma GCC push_options
#pragma GCC optimize("O0")
void __assert_fail(const char *expr, const char *file, int line, const char *func)
void __assert_fail(const char* expr, const char* file, int line, const char* func)
{
fprintf(stdout, "Assertion failed: %s (%s: %s: %d)\n", expr, file, func, line);
exit(0);
fprintf(stdout, "Assertion failed: %s (%s: %s: %d)\n", expr, file, func, line);
exit(0);
}
#pragma GCC pop_options

View File

@@ -1,7 +1,7 @@
/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
#include <stdlib.h>
double atof(const char *ascii)
double atof(const char* ascii)
{
return strtod(ascii, 0);
return strtod(ascii, 0);
}

View File

@@ -1,21 +1,25 @@
#include <stdlib.h>
#include <ctype.h>
#include <stdlib.h>
/*
** atoi(s) - convert s to integer.
*/
int atoi(const char *s)
int atoi(const char* s)
{
int sign, n;
while(isspace(*s)) ++s;
while (isspace(*s))
++s;
sign = 1;
switch(*s) {
case '-': sign = -1;
case '+': ++s;
switch (*s) {
case '-':
sign = -1;
case '+':
++s;
}
n = 0;
while(isdigit(*s)) n = 10 * n + *s++ - '0';
while (isdigit(*s))
n = 10 * n + *s++ - '0';
return (sign * n);
}

View File

@@ -1,17 +1,20 @@
#include <stdlib.h>
#include <ctype.h>
#include <stdlib.h>
long atol(const char *s)
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;
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;
}

View File

@@ -1,17 +1,20 @@
#include <stdlib.h>
#include <ctype.h>
#include <stdlib.h>
long long atoll(const char *s)
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;
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;
}

View File

@@ -1,13 +1,14 @@
#include <stdlib.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/ksys.h>
void *calloc(size_t num, size_t size) {
void *ptr = _ksys_alloc(num*size);
if(!ptr){
errno = ENOMEM;
void* calloc(size_t num, size_t size)
{
void* ptr = _ksys_alloc(num * size);
if (!ptr) {
__errno = ENOMEM;
return NULL;
}
memset(ptr, 0, num*size);
memset(ptr, 0, num * size);
return ptr;
}

View File

@@ -5,7 +5,7 @@
void exit(int status)
{
if(__con_is_load){
if (__con_is_load) {
con_exit(status);
}
_ksys_exit();

View File

@@ -1,6 +1,7 @@
#include <stdlib.h>
#include <sys/ksys.h>
void free(void *ptr) {
void free(void* ptr)
{
_ksys_free(ptr);
}

View File

@@ -1,23 +1,23 @@
#include <string.h>
#include <sys/ksys.h>
char *__reverse(char *str)
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;
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 */
@@ -25,13 +25,13 @@ void itoa(int n, char s[])
{
int i, sign;
if ((sign = n) < 0) /* record sign */
n = -n; /* make n positive */
if ((sign = n) < 0) /* record sign */
n = -n; /* make n positive */
i = 0;
do { /* generate digits in reverse order */
do { /* generate digits in reverse order */
s[i++] = n % 10 + '0'; /* get next digit */
} while ((n /= 10) > 0); /* delete it */
} while ((n /= 10) > 0); /* delete it */
if (sign < 0)
s[i++] = '-';

View File

@@ -2,5 +2,5 @@
long labs(long a)
{
return a>0 ? a : -a;
return a > 0 ? a : -a;
}

View File

@@ -2,5 +2,5 @@
long long llabs(long long a)
{
return a>0 ? a : -a;
return a > 0 ? a : -a;
}

View File

@@ -1,6 +1,7 @@
#include <stdlib.h>
#include <sys/ksys.h>
void *malloc(size_t size) {
void* malloc(size_t size)
{
return _ksys_alloc(size);
}

View File

@@ -29,13 +29,13 @@
* The MTHREShold is where we stop finding a better median.
*/
#define THRESH 4 /* threshold for insertion */
#define MTHRESH 6 /* threshold for median */
#define THRESH 4 /* threshold for insertion */
#define MTHRESH 6 /* threshold for median */
static int (*qcmp)(const void *, const void *); /* the comparison routine */
static int qsz; /* size of each record */
static int thresh; /* THRESHold in chars */
static int mthresh; /* MTHRESHold in chars */
static int (*qcmp)(const void*, const void*); /* the comparison routine */
static int qsz; /* size of each record */
static int thresh; /* THRESHold in chars */
static int mthresh; /* MTHRESHold in chars */
/*
* qst:
@@ -52,117 +52,101 @@ static int mthresh; /* MTHRESHold in chars */
* (And there are only three places where this is done).
*/
static void
qst(char *base, char *max)
static void qst(char* base, char* max)
{
char c, *i, *j, *jj;
int ii;
char *mid, *tmp;
int lo, hi;
char c, *i, *j, *jj;
int ii;
char *mid, *tmp;
int lo, hi;
/*
* At the top here, lo is the number of characters of elements in the
* current partition. (Which should be max - base).
* Find the median of the first, last, and middle element and make
* that the middle element. Set j to largest of first and middle.
* If max is larger than that guy, then it's that guy, else compare
* max with loser of first and take larger. Things are set up to
* prefer the middle, then the first in case of ties.
*/
lo = max - base; /* number of elements as chars */
do {
mid = i = base + qsz * ((lo / qsz) >> 1);
if (lo >= mthresh)
{
j = (qcmp((jj = base), i) > 0 ? jj : i);
if (qcmp(j, (tmp = max - qsz)) > 0)
{
/* switch to first loser */
j = (j == jj ? i : jj);
if (qcmp(j, tmp) < 0)
j = tmp;
}
if (j != i)
{
ii = qsz;
do {
c = *i;
*i++ = *j;
*j++ = c;
} while (--ii);
}
}
/*
* Semi-standard quicksort partitioning/swapping
* At the top here, lo is the number of characters of elements in the
* current partition. (Which should be max - base).
* Find the median of the first, last, and middle element and make
* that the middle element. Set j to largest of first and middle.
* If max is larger than that guy, then it's that guy, else compare
* max with loser of first and take larger. Things are set up to
* prefer the middle, then the first in case of ties.
*/
for (i = base, j = max - qsz; ; )
{
while (i < mid && qcmp(i, mid) <= 0)
i += qsz;
while (j > mid)
{
if (qcmp(mid, j) <= 0)
{
j -= qsz;
continue;
}
tmp = i + qsz; /* value of i after swap */
if (i == mid)
{
/* j <-> mid, new mid is j */
mid = jj = j;
}
else
{
/* i <-> j */
jj = j;
j -= qsz;
}
goto swap;
}
if (i == mid)
{
break;
}
else
{
/* i <-> mid, new mid is i */
jj = mid;
tmp = mid = i; /* value of i after swap */
j -= qsz;
}
swap:
ii = qsz;
do {
c = *i;
*i++ = *jj;
*jj++ = c;
} while (--ii);
i = tmp;
}
/*
* Look at sizes of the two partitions, do the smaller
* one first by recursion, then do the larger one by
* making sure lo is its size, base and max are update
* correctly, and branching back. But only repeat
* (recursively or by branching) if the partition is
* of at least size THRESH.
*/
i = (j = mid) + qsz;
if ((lo = j - base) <= (hi = max - i))
{
if (lo >= thresh)
qst(base, j);
base = i;
lo = hi;
}
else
{
if (hi >= thresh)
qst(i, max);
max = j;
}
} while (lo >= thresh);
lo = max - base; /* number of elements as chars */
do {
mid = i = base + qsz * ((lo / qsz) >> 1);
if (lo >= mthresh) {
j = (qcmp((jj = base), i) > 0 ? jj : i);
if (qcmp(j, (tmp = max - qsz)) > 0) {
/* switch to first loser */
j = (j == jj ? i : jj);
if (qcmp(j, tmp) < 0)
j = tmp;
}
if (j != i) {
ii = qsz;
do {
c = *i;
*i++ = *j;
*j++ = c;
} while (--ii);
}
}
/*
* Semi-standard quicksort partitioning/swapping
*/
for (i = base, j = max - qsz;;) {
while (i < mid && qcmp(i, mid) <= 0)
i += qsz;
while (j > mid) {
if (qcmp(mid, j) <= 0) {
j -= qsz;
continue;
}
tmp = i + qsz; /* value of i after swap */
if (i == mid) {
/* j <-> mid, new mid is j */
mid = jj = j;
} else {
/* i <-> j */
jj = j;
j -= qsz;
}
goto swap;
}
if (i == mid) {
break;
} else {
/* i <-> mid, new mid is i */
jj = mid;
tmp = mid = i; /* value of i after swap */
j -= qsz;
}
swap:
ii = qsz;
do {
c = *i;
*i++ = *jj;
*jj++ = c;
} while (--ii);
i = tmp;
}
/*
* Look at sizes of the two partitions, do the smaller
* one first by recursion, then do the larger one by
* making sure lo is its size, base and max are update
* correctly, and branching back. But only repeat
* (recursively or by branching) if the partition is
* of at least size THRESH.
*/
i = (j = mid) + qsz;
if ((lo = j - base) <= (hi = max - i)) {
if (lo >= thresh)
qst(base, j);
base = i;
lo = hi;
} else {
if (hi >= thresh)
qst(i, max);
max = j;
}
} while (lo >= thresh);
}
/*
@@ -172,67 +156,59 @@ qst(char *base, char *max)
* It's not...
*/
void
qsort(void *base0, size_t n, size_t size, int (*compar)(const void *, const void *))
void qsort(void* base0, size_t n, size_t size, int (*compar)(const void*, const void*))
{
char *base = (char *)base0;
char c, *i, *j, *lo, *hi;
char *min, *max;
char* base = (char*)base0;
char c, *i, *j, *lo, *hi;
char *min, *max;
if (n <= 1)
return;
qsz = size;
qcmp = compar;
thresh = qsz * THRESH;
mthresh = qsz * MTHRESH;
max = base + n * qsz;
if (n >= THRESH)
{
qst(base, max);
hi = base + thresh;
}
else
{
hi = max;
}
/*
* First put smallest element, which must be in the first THRESH, in
* the first position as a sentinel. This is done just by searching
* the first THRESH elements (or the first n if n < THRESH), finding
* the min, and swapping it into the first position.
*/
for (j = lo = base; (lo += qsz) < hi; )
if (qcmp(j, lo) > 0)
j = lo;
if (j != base)
{
/* swap j into place */
for (i = base, hi = base + qsz; i < hi; )
{
c = *j;
*j++ = *i;
*i++ = c;
if (n <= 1)
return;
qsz = size;
qcmp = compar;
thresh = qsz * THRESH;
mthresh = qsz * MTHRESH;
max = base + n * qsz;
if (n >= THRESH) {
qst(base, max);
hi = base + thresh;
} else {
hi = max;
}
}
/*
* With our sentinel in place, we now run the following hyper-fast
* insertion sort. For each remaining element, min, from [1] to [n-1],
* set hi to the index of the element AFTER which this one goes.
* Then, do the standard insertion sort shift on a character at a time
* basis for each element in the frob.
*/
for (min = base; (hi = min += qsz) < max; )
{
while (qcmp(hi -= qsz, min) > 0)
/* void */;
if ((hi += qsz) != min) {
for (lo = min + qsz; --lo >= min; )
{
c = *lo;
for (i = j = lo; (j -= qsz) >= hi; i = j)
*i = *j;
*i = c;
}
/*
* First put smallest element, which must be in the first THRESH, in
* the first position as a sentinel. This is done just by searching
* the first THRESH elements (or the first n if n < THRESH), finding
* the min, and swapping it into the first position.
*/
for (j = lo = base; (lo += qsz) < hi;)
if (qcmp(j, lo) > 0)
j = lo;
if (j != base) {
/* swap j into place */
for (i = base, hi = base + qsz; i < hi;) {
c = *j;
*j++ = *i;
*i++ = c;
}
}
/*
* With our sentinel in place, we now run the following hyper-fast
* insertion sort. For each remaining element, min, from [1] to [n-1],
* set hi to the index of the element AFTER which this one goes.
* Then, do the standard insertion sort shift on a character at a time
* basis for each element in the frob.
*/
for (min = base; (hi = min += qsz) < max;) {
while (qcmp(hi -= qsz, min) > 0)
/* void */;
if ((hi += qsz) != min) {
for (lo = min + qsz; --lo >= min;) {
c = *lo;
for (i = j = lo; (j -= qsz) >= hi; i = j)
*i = *j;
*i = c;
}
}
}
}
}

View File

@@ -1,15 +1,15 @@
#include <stdlib.h>
#include <stdint.h>
#include <stdlib.h>
static uint64_t seed;
void srand(unsigned s)
{
seed = s-1;
seed = s - 1;
}
int rand(void)
{
seed = 6364136223846793005ULL*seed + 1;
return seed>>33;
seed = 6364136223846793005ULL * seed + 1;
return seed >> 33;
}

View File

@@ -1,6 +1,7 @@
#include <stdlib.h>
#include <sys/ksys.h>
void *realloc(void *ptr, size_t newsize) {
void* realloc(void* ptr, size_t newsize)
{
return _ksys_realloc(ptr, newsize);
}

View File

@@ -1,17 +1,19 @@
/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
#include <math.h>
#define unconst(__v, __t) __extension__ ({union { const __t __cp; __t __p; } __q; __q.__cp = __v; __q.__p;})
#ifndef unconst
#define unconst(__v, __t) __extension__({union { const __t __cp; __t __p; } __q; __q.__cp = __v; __q.__p; })
#endif
double strtod(const char *s, char **sret)
double strtod(const char* s, char** sret)
{
long double r; /* result */
int e; /* exponent */
long double d; /* scale */
int sign; /* +- 1.0 */
long double r; /* result */
int e; /* exponent */
long double d; /* scale */
int sign; /* +- 1.0 */
int esign;
int i;
int flags=0;
int flags = 0;
r = 0.0;
sign = 1.0;
@@ -20,30 +22,26 @@ double strtod(const char *s, char **sret)
while ((*s == ' ') || (*s == '\t'))
s++;
if (*s == '+')
s++;
else if (*s == '-')
{
else if (*s == '-') {
sign = -1;
s++;
}
while ((*s >= '0') && (*s <= '9'))
{
while ((*s >= '0') && (*s <= '9')) {
flags |= 1;
r *= 10.0;
r += *s - '0';
s++;
}
if (*s == '.')
{
if (*s == '.') {
d = 0.1L;
s++;
while ((*s >= '0') && (*s <= '9'))
{
while ((*s >= '0') && (*s <= '9')) {
flags |= 2;
r += d * (*s - '0');
s++;
@@ -51,33 +49,28 @@ double strtod(const char *s, char **sret)
}
}
if (flags == 0)
{
if (flags == 0) {
if (sret)
*sret = unconst(s, char *);
*sret = unconst(s, char*);
return 0;
}
if ((*s == 'e') || (*s == 'E'))
{
if ((*s == 'e') || (*s == 'E')) {
s++;
if (*s == '+')
s++;
else if (*s == '-')
{
else if (*s == '-') {
s++;
esign = -1;
}
if ((*s < '0') || (*s > '9'))
{
if ((*s < '0') || (*s > '9')) {
if (sret)
*sret = unconst(s, char *);
*sret = unconst(s, char*);
return r;
}
while ((*s >= '0') && (*s <= '9'))
{
while ((*s >= '0') && (*s <= '9')) {
e *= 10;
e += *s - '0';
s++;
@@ -91,6 +84,6 @@ double strtod(const char *s, char **sret)
r *= 10.0;
if (sret)
*sret = unconst(s, char *);
*sret = unconst(s, char*);
return r * sign;
}
}

View File

@@ -1,73 +1,72 @@
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
int getdigit(char ch, int base)
{
if (isdigit(ch)) ch-= '0';
else
if (isalpha(ch) && ch <= 'Z') ch = 10 + ch - 'A';
else
if (isalpha(ch)) ch = 10 + ch - 'a';
else
return -1;
if (ch / base != 0) return -1;
if (isdigit(ch))
ch -= '0';
else if (isalpha(ch) && ch <= 'Z')
ch = 10 + ch - 'A';
else if (isalpha(ch))
ch = 10 + ch - 'a';
else
return -1;
return ch;
if (ch / base != 0)
return -1;
return ch;
}
long int strtol (const char* str, char** endptr, int base)
long int strtol(const char* str, char** endptr, int base)
{
long int res = 0;
int sign = 1;
int sign = 1;
if (base > 36)
{
errno = EINVAL;
goto bye;
}
if (base > 36) {
errno = EINVAL;
goto bye;
}
while (isspace(*str)) str++;
while (isspace(*str))
str++;
if (*str == '-') { sign = -1; str++; }
else
if (*str == '+') str++;
if (*str == '-') {
sign = -1;
str++;
} else if (*str == '+')
str++;
if (base == 0 || base == 16)
{
if (*str == '0' && (str[1] == 'x' || str[1] == 'X'))
{
base = 16;
str += 2;
}
}
if (base == 0 || base == 16) {
if (*str == '0' && (str[1] == 'x' || str[1] == 'X')) {
base = 16;
str += 2;
}
}
if (base == 0 && *str == '0') base = 8;
if (base == 0 && *str == '0')
base = 8;
if (base == 0) base = 10;
if (base == 0)
base = 10;
int digit;
while ((digit = getdigit(*str, base)) >= 0)
{
res = base * res + digit;
str++;
if (res < 0)
{
errno = ERANGE;
if (sign > 0)
res = LONG_MAX;
else
res = LONG_MIN;
}
}
int digit;
while ((digit = getdigit(*str, base)) >= 0) {
res = base * res + digit;
str++;
if (res < 0) {
errno = ERANGE;
if (sign > 0)
res = LONG_MAX;
else
res = LONG_MIN;
}
}
bye:
if (endptr)
*endptr = (char*)str;
if (endptr)
*endptr = (char*)str;
return res * sign;
return res * sign;
}