/programs/develop/ktcc/trunk/libc.obj/source/stdlib/exit.c |
---|
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(); |
} |
/programs/develop/ktcc/trunk/libc.obj/source/stdlib/rand.c |
---|
0,0 → 1,15 |
#include <stdlib.h> |
#include <stdint.h> |
static uint64_t seed; |
void srand(unsigned s) |
{ |
seed = s-1; |
} |
int rand(void) |
{ |
seed = 6364136223846793005ULL*seed + 1; |
return seed>>33; |
} |
/programs/develop/ktcc/trunk/libc.obj/source/stdlib/___chkstk_ms.c |
---|
0,0 → 1,0 |
void ___chkstk_ms() {} |
/programs/develop/ktcc/trunk/libc.obj/source/stdlib/abs.c |
---|
0,0 → 1,6 |
#include <stdlib.h> |
int abs(int a) |
{ |
return a>0 ? a : -a; |
} |
/programs/develop/ktcc/trunk/libc.obj/source/stdlib/atoi.c |
---|
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); |
} |
/programs/develop/ktcc/trunk/libc.obj/source/stdlib/atol.c |
---|
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; |
} |
/programs/develop/ktcc/trunk/libc.obj/source/stdlib/atoll.c |
---|
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; |
} |
/programs/develop/ktcc/trunk/libc.obj/source/stdlib/calloc.c |
---|
0,0 → 1,6 |
#include <stdlib.h> |
#include <sys/ksys.h> |
void *calloc(size_t num, size_t size) { |
return _ksys_alloc(num*size); |
} |
/programs/develop/ktcc/trunk/libc.obj/source/stdlib/div.c |
---|
0,0 → 1,6 |
#include <stdlib.h> |
div_t div(int num, int den) |
{ |
return (div_t){ num/den, num%den }; |
} |
/programs/develop/ktcc/trunk/libc.obj/source/stdlib/free.c |
---|
0,0 → 1,6 |
#include <stdlib.h> |
#include <sys/ksys.h> |
void free(void *ptr) { |
_ksys_free(ptr); |
} |
/programs/develop/ktcc/trunk/libc.obj/source/stdlib/itoa.c |
---|
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; |
} |
/programs/develop/ktcc/trunk/libc.obj/source/stdlib/labs.c |
---|
0,0 → 1,6 |
#include <stdlib.h> |
long labs(long a) |
{ |
return a>0 ? a : -a; |
} |
/programs/develop/ktcc/trunk/libc.obj/source/stdlib/ldiv.c |
---|
0,0 → 1,6 |
#include <stdlib.h> |
ldiv_t ldiv(long num, long den) |
{ |
return (ldiv_t){ num/den, num%den }; |
} |
/programs/develop/ktcc/trunk/libc.obj/source/stdlib/llabs.c |
---|
0,0 → 1,6 |
#include <stdlib.h> |
long long llabs(long long a) |
{ |
return a>0 ? a : -a; |
} |
/programs/develop/ktcc/trunk/libc.obj/source/stdlib/lldiv.c |
---|
0,0 → 1,6 |
#include <stdlib.h> |
lldiv_t lldiv(long long num, long long den) |
{ |
return (lldiv_t){ num/den, num%den }; |
} |
/programs/develop/ktcc/trunk/libc.obj/source/stdlib/malloc.c |
---|
0,0 → 1,6 |
#include <stdlib.h> |
#include <sys/ksys.h> |
void *malloc(size_t size) { |
return _ksys_alloc(size); |
} |
/programs/develop/ktcc/trunk/libc.obj/source/stdlib/realloc.c |
---|
0,0 → 1,6 |
#include <stdlib.h> |
#include <sys/ksys.h> |
void *realloc(void *ptr, size_t newsize) { |
return _ksys_realloc(ptr, newsize); |
} |
/programs/develop/ktcc/trunk/libc.obj/source/stdlib/strtol.c |
---|
0,0 → 1,78 |
#include <stdlib.h> |
#include <stdio.h> |
#include <ctype.h> |
#define LONG_MIN (-2147483647L-1) |
#define LONG_MAX (2147483647L) |
#define ULONG_MAX (4294967295UL) |
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; |
return ch; |
} |
long int strtol (const char* str, char** endptr, int base) |
{ |
long int res = 0; |
int sign = 1; |
if (base > 36) |
{ |
errno = EINVAL; |
goto bye; |
} |
while (isspace(*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 && *str == '0') base = 8; |
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; |
} |
} |
bye: |
if (endptr) |
*endptr = (char*)str; |
return res * sign; |
} |