summaryrefslogtreecommitdiff
path: root/nonstd/libc.c
blob: dabbc44a0b053a54e46750c25707a862b780d4ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <stddef.h>
#include <locale.h>

#include "nonstd/types.h"

#include "nonstd/public/setjmp.h"

#include "nonstd/static/locale.h"
#include "nonstd/static/thread.h"
/*
#include "nonstd/static/wctype.h"
#include "nonstd/static/wctrans.h"
*/
#include "nonstd/static/printf.h"
#include "nonstd/static/scanf.h"
#include "nonstd/static/fopen.h"

#include "nonstd/syscall.h"

#include "__linux.h"

/*
static int __syscall_byname(const char *name, ...)
{
	int ret = -1;
	int sc = __libc.syscall_lookup(name);
	va_list ap;
	va_start(ap, name);
	ret = __libc.syscall_arglist(sc, ap);
	va_end(ap);
	return ret;
}

static int __syscall_bynum(int call, ...)
{
	int ret = -1;
	va_list ap;
	va_start(ap, call);
	ret = __libc.syscall_arglist(call, ap);
	va_end(ap);
	return ret;
}
*/

struct libc __libc = {
	.ctype = {
		.ctattr = 0,
		.ctolower = 1,
		.ctoupper = 2,
		.lower = 1 << 1,
		.punct = 1 << 2,
		.space = 1 << 3,
		.upper = 1 << 4,
		.xdigit = 1 << 5,
		.getmap = __getmap,
	},
	.stdio.printf = __common_printf,
	.stdio.scanf = __common_scanf,
	.stdio.fopen = __common_fopen,
	.stdlib.rand = 1,
	.stdlib.atexit_max = 32,
	/*
	.wctype.wctype =  __wctype,
	.wctype.nwctype = sizeof(__wctype) / sizeof(__wctype[0]),
	.wctype.wctrans = __wctrans,
	.wctype.nwctrans = sizeof(__wctrans) / sizeof(__wctrans[0]),
	.unistd.confstr = confstr,
	.unistd.nconfstr = sizeof(confstr) / sizeof(confstr[0]),
	*/
	.syscall_lookup = __syscall_lookup,
	.syscall = __syscall,
	/*
	.syscall_byname = __syscall_byname,
	.syscall_bynum = __syscall_bynum,
	.syscall_arglist = __syscall_arglist,
	*/
	.per_thread = per_thread,
};

extern int main();

void __libc_start(int argc, char **argv)
{
	struct __fopen_options fo = {0};
	fo.fd = 0;
	stdin = __libc.stdio.fopen(&fo);
	fo.fd = 1;
	stdout = __libc.stdio.fopen(&fo);
	fo.fd = 2;
	stderr = __libc.stdio.fopen(&fo);

	#if defined _POSIX_SOURCE || defined _POSIX_C_SOURCE || defined _XOPEN_SOURCE
	setlocale("POSIX");
	#else
	setlocale(LC_ALL, "C");
	#endif

	exit(main(argc, argv));
}


#include <stdio.h>
#include <stdlib.h>

/* TODO: i18n */

void __assert(const char *expr, const char *file, int line, const char *func)
{
	if (func) {
		fprintf(stderr, "Assertion failed: %s (%s:%d:%s())\n", expr,
			file, line, func);
	} else {
		fprintf(stderr, "Assertion failed: %s (%s:%d)\n", expr, file,
			line);
	}
	abort();
}

int *__errno(void)
{
	return &__libc.per_thread()->err;
}


FILE *stdin, *stdout, *stderr;