meta: move photon/libc to root
This commit is contained in:
16
libc/stdlib/abort.c
Normal file
16
libc/stdlib/abort.c
Normal file
@@ -0,0 +1,16 @@
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
|
||||
/* defined in dyld/main.c */
|
||||
extern const char *dyld_exec_path(void);
|
||||
|
||||
_Noreturn void abort(void)
|
||||
{
|
||||
/* yes we're faking the job number shut up */
|
||||
char header[64];
|
||||
snprintf(header, sizeof header, "[1] %6d abort", getpid());
|
||||
|
||||
fprintf(stderr, "%-20s %s\n", header, dyld_exec_path());
|
||||
exit(-1);
|
||||
}
|
||||
8
libc/stdlib/assert.c
Normal file
8
libc/stdlib/assert.c
Normal file
@@ -0,0 +1,8 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
extern void _Noreturn __assert_fail(const char *exp, const char *func, const char *file, int line)
|
||||
{
|
||||
printf("Assertion failed: (%s), function %s, file %s, line %d\n", exp, func, file, line);
|
||||
abort();
|
||||
}
|
||||
21
libc/stdlib/atexit.c
Normal file
21
libc/stdlib/atexit.c
Normal file
@@ -0,0 +1,21 @@
|
||||
#define MAX_FN 32
|
||||
|
||||
static void(*atexit_fn[MAX_FN])(void);
|
||||
static int atexit_fn_count = 0;
|
||||
|
||||
int atexit(void(*fn)(void))
|
||||
{
|
||||
if (atexit_fn_count == MAX_FN) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
atexit_fn[atexit_fn_count++] = fn;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void __crt_run_atexit()
|
||||
{
|
||||
for (int i = 0; i < atexit_fn_count; i++) {
|
||||
atexit_fn[i]();
|
||||
}
|
||||
}
|
||||
42
libc/stdlib/atof.c
Normal file
42
libc/stdlib/atof.c
Normal file
@@ -0,0 +1,42 @@
|
||||
#include <ctype.h>
|
||||
|
||||
double atof(const char *s)
|
||||
{
|
||||
double a = 0.0;
|
||||
int e = 0;
|
||||
int c;
|
||||
while ((c = *s++) != '\0' && isdigit(c)) {
|
||||
a = a*10.0 + (c - '0');
|
||||
}
|
||||
if (c == '.') {
|
||||
while ((c = *s++) != '\0' && isdigit(c)) {
|
||||
a = a*10.0 + (c - '0');
|
||||
e = e-1;
|
||||
}
|
||||
}
|
||||
if (c == 'e' || c == 'E') {
|
||||
int sign = 1;
|
||||
int i = 0;
|
||||
c = *s++;
|
||||
if (c == '+')
|
||||
c = *s++;
|
||||
else if (c == '-') {
|
||||
c = *s++;
|
||||
sign = -1;
|
||||
}
|
||||
while (isdigit(c)) {
|
||||
i = i*10 + (c - '0');
|
||||
c = *s++;
|
||||
}
|
||||
e += i*sign;
|
||||
}
|
||||
while (e > 0) {
|
||||
a *= 10.0;
|
||||
e--;
|
||||
}
|
||||
while (e < 0) {
|
||||
a *= 0.1;
|
||||
e++;
|
||||
}
|
||||
return a;
|
||||
}
|
||||
0
libc/stdlib/atoi.c
Normal file
0
libc/stdlib/atoi.c
Normal file
6
libc/stdlib/exit.c
Normal file
6
libc/stdlib/exit.c
Normal file
@@ -0,0 +1,6 @@
|
||||
#include <__system.h>
|
||||
|
||||
_Noreturn void exit(int code)
|
||||
{
|
||||
__exit(code);
|
||||
}
|
||||
6
libc/stdlib/getenv.c
Normal file
6
libc/stdlib/getenv.c
Normal file
@@ -0,0 +1,6 @@
|
||||
extern char *__crt_sys_getenv(const char *name);
|
||||
|
||||
char *getenv(const char *name)
|
||||
{
|
||||
return __crt_sys_getenv(name);
|
||||
}
|
||||
31
libc/stdlib/rand.c
Normal file
31
libc/stdlib/rand.c
Normal file
@@ -0,0 +1,31 @@
|
||||
#include <stddef.h>
|
||||
|
||||
static unsigned int seed = 0;
|
||||
|
||||
void srand(unsigned int _seed)
|
||||
{
|
||||
seed = _seed;
|
||||
}
|
||||
|
||||
int rand(void)
|
||||
{
|
||||
int next = seed;
|
||||
int result;
|
||||
|
||||
next *= 1103515245;
|
||||
next += 12345;
|
||||
result = (int)(next / 65536) % 2048;
|
||||
|
||||
next *= 1103515245;
|
||||
next += 12345;
|
||||
result <<= 10;
|
||||
result ^= (int)(next / 65536) % 1024;
|
||||
|
||||
next *= 1103515245;
|
||||
next += 12345;
|
||||
result <<= 10;
|
||||
result ^= (int)(next / 65536) % 1024;
|
||||
|
||||
seed = next;
|
||||
return result;
|
||||
}
|
||||
91
libc/stdlib/strtod.c
Normal file
91
libc/stdlib/strtod.c
Normal file
@@ -0,0 +1,91 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
double strtod(const char *str, char **ptr)
|
||||
{
|
||||
if (ptr == (char **)0) {
|
||||
return atof(str);
|
||||
}
|
||||
|
||||
size_t len = strlen(str);
|
||||
char rstr[len + 1];
|
||||
strcpy(rstr, str);
|
||||
|
||||
char *p = rstr;
|
||||
|
||||
while (isspace(*p)) {
|
||||
++p;
|
||||
}
|
||||
|
||||
if (*p == '+' || *p == '-') {
|
||||
++p;
|
||||
}
|
||||
|
||||
if ((p[0] == 'i' || p[0] == 'I')
|
||||
&& (p[1] == 'n' || p[1] == 'N')
|
||||
&& (p[2] == 'f' || p[2] == 'F')) {
|
||||
if ((p[3] == 'i' || p[3] == 'I')
|
||||
&& (p[4] == 'n' || p[4] == 'N')
|
||||
&& (p[5] == 'i' || p[5] == 'I')
|
||||
&& (p[6] == 't' || p[6] == 'T')
|
||||
&& (p[7] == 'y' || p[7] == 'Y')) {
|
||||
*ptr = p + 8;
|
||||
return atof(str);
|
||||
} else {
|
||||
*ptr = p + 3;
|
||||
return atof(str);
|
||||
}
|
||||
}
|
||||
|
||||
if ((p[0] == 'n' || p[0] == 'N')
|
||||
&& (p[1] == 'a' || p[1] == 'A')
|
||||
&& (p[2] == 'n' || p[2] == 'N')) {
|
||||
p += 3;
|
||||
if (*p == '(') {
|
||||
++p;
|
||||
while (*p != '\0' && *p != ')') {
|
||||
++p;
|
||||
}
|
||||
|
||||
if (*p == ')') {
|
||||
++p;
|
||||
}
|
||||
}
|
||||
|
||||
*ptr = p;
|
||||
return atof(str);
|
||||
}
|
||||
|
||||
if (isdigit(*p) || *p == '.') {
|
||||
int got_dot = 0;
|
||||
while (isdigit(*p) || (!got_dot && *p == '.')) {
|
||||
if (*p == '.')
|
||||
got_dot = 1;
|
||||
++p;
|
||||
}
|
||||
|
||||
if (*p == 'e' || *p == 'E') {
|
||||
int i;
|
||||
i = 1;
|
||||
if (p[i] == '+' || p[i] == '-') {
|
||||
++i;
|
||||
}
|
||||
|
||||
if (isdigit (p[i])) {
|
||||
while (isdigit (p[i])) {
|
||||
++i;
|
||||
}
|
||||
|
||||
*ptr = p + i;
|
||||
return atof(str);
|
||||
}
|
||||
}
|
||||
|
||||
*ptr = p;
|
||||
return atof(str);
|
||||
}
|
||||
|
||||
*ptr = (char *)str;
|
||||
return 0.0;
|
||||
}
|
||||
33
libc/stdlib/strtof.c
Normal file
33
libc/stdlib/strtof.c
Normal file
@@ -0,0 +1,33 @@
|
||||
#include <ctype.h>
|
||||
|
||||
float strtof(const char *str, char **endptr)
|
||||
{
|
||||
float res = 0.0F;
|
||||
char *ptr = (char*)str;
|
||||
int after_dot = 0;
|
||||
float div = 1;
|
||||
|
||||
while (*ptr != '\0') {
|
||||
if (isdigit(*ptr)) {
|
||||
if (!after_dot) {
|
||||
res *= 10;
|
||||
res += *ptr - '0';
|
||||
} else {
|
||||
div *= 10;
|
||||
res += (float)(*ptr - '0') / div;
|
||||
}
|
||||
} else if (*ptr == '.') {
|
||||
after_dot = 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
|
||||
ptr++;
|
||||
}
|
||||
|
||||
if (endptr) {
|
||||
*endptr = ptr;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
73
libc/stdlib/strtol.c
Normal file
73
libc/stdlib/strtol.c
Normal file
@@ -0,0 +1,73 @@
|
||||
#include <ctype.h>
|
||||
#include <stdint.h>
|
||||
#include <limits.h>
|
||||
|
||||
long strtol(const char *nptr, char **endptr, int base)
|
||||
{
|
||||
const char *s = nptr;
|
||||
unsigned long acc;
|
||||
int c;
|
||||
unsigned long cutoff;
|
||||
int neg = 0, any, cutlim;
|
||||
|
||||
do {
|
||||
c = *s++;
|
||||
} while (isspace(c));
|
||||
|
||||
if (c == '-') {
|
||||
neg = 1;
|
||||
c = *s++;
|
||||
} else if (c == '+') {
|
||||
c = *s++;
|
||||
} if ((base == 0 || base == 16) &&
|
||||
c == '0' && (*s == 'x' || *s == 'X')) {
|
||||
c = s[1];
|
||||
s += 2;
|
||||
base = 16;
|
||||
} else if ((base == 0 || base == 2) &&
|
||||
c == '0' && (*s == 'b' || *s == 'B')) {
|
||||
c = s[1];
|
||||
s += 2;
|
||||
base = 2;
|
||||
}
|
||||
|
||||
if (base == 0) {
|
||||
base = c == '0' ? 8 : 10;
|
||||
}
|
||||
|
||||
cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
|
||||
cutlim = cutoff % (unsigned long)base;
|
||||
cutoff /= (unsigned long)base;
|
||||
|
||||
for (acc = 0, any = 0;; c = *s++) {
|
||||
if (isdigit(c)) {
|
||||
c -= '0';
|
||||
} else if (isalpha(c)) {
|
||||
c -= isupper(c) ? 'A' - 10 : 'a' - 10;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
|
||||
if (c >= base) {
|
||||
break;
|
||||
} if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) {
|
||||
any = -1;
|
||||
} else {
|
||||
any = 1;
|
||||
acc *= base;
|
||||
acc += c;
|
||||
}
|
||||
}
|
||||
|
||||
if (any < 0) {
|
||||
acc = neg ? LONG_MIN : LONG_MAX;
|
||||
} else if (neg) {
|
||||
acc = -acc;
|
||||
}
|
||||
|
||||
if (endptr != 0) {
|
||||
*endptr = (char *)(any ? s - 1 : nptr);
|
||||
}
|
||||
|
||||
return acc;
|
||||
}
|
||||
74
libc/stdlib/strtoul.c
Normal file
74
libc/stdlib/strtoul.c
Normal file
@@ -0,0 +1,74 @@
|
||||
#include <ctype.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <limits.h>
|
||||
|
||||
unsigned long strtoul(const char *nptr, char **endptr, int base)
|
||||
{
|
||||
const char *s = nptr;
|
||||
unsigned long acc;
|
||||
int c;
|
||||
unsigned long cutoff;
|
||||
int neg = 0, any, cutlim;
|
||||
|
||||
do {
|
||||
c = *s++;
|
||||
} while (isspace(c));
|
||||
|
||||
if (c == '-') {
|
||||
neg = 1;
|
||||
c = *s++;
|
||||
} else if (c == '+') {
|
||||
c = *s++;
|
||||
} if ((base == 0 || base == 16) &&
|
||||
c == '0' && (*s == 'x' || *s == 'X')) {
|
||||
c = s[1];
|
||||
s += 2;
|
||||
base = 16;
|
||||
} else if ((base == 0 || base == 2) &&
|
||||
c == '0' && (*s == 'b' || *s == 'B')) {
|
||||
c = s[1];
|
||||
s += 2;
|
||||
base = 2;
|
||||
}
|
||||
|
||||
if (base == 0) {
|
||||
base = c == '0' ? 8 : 10;
|
||||
}
|
||||
|
||||
cutoff = ULONG_MAX;
|
||||
cutlim = cutoff % (unsigned long)base;
|
||||
cutoff /= (unsigned long)base;
|
||||
|
||||
for (acc = 0, any = 0;; c = *s++) {
|
||||
if (isdigit(c)) {
|
||||
c -= '0';
|
||||
} else if (isalpha(c)) {
|
||||
c -= isupper(c) ? 'A' - 10 : 'a' - 10;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
|
||||
if (c >= base) {
|
||||
break;
|
||||
} if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) {
|
||||
any = -1;
|
||||
} else {
|
||||
any = 1;
|
||||
acc *= base;
|
||||
acc += c;
|
||||
}
|
||||
}
|
||||
|
||||
if (any < 0) {
|
||||
acc = ULONG_MAX;
|
||||
} else if (neg) {
|
||||
acc = -acc;
|
||||
}
|
||||
|
||||
if (endptr != 0) {
|
||||
*endptr = (char *)(any ? s - 1 : nptr);
|
||||
}
|
||||
|
||||
return acc;
|
||||
}
|
||||
Reference in New Issue
Block a user