summaryrefslogtreecommitdiff
path: root/src/nonstd/__libc.c
blob: f596a3c87f0194977a0d21af59294cca557c555b (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
#include "sys/types.h"
#include <nonstd/internal.h>
#include "nonstd/locale.h"
#include "nonstd/io.h"

#include "_printf.h"
#include "_scanf.h"
#include "_locale.h"
#include "_syscall.h"

void *__libc(LIBC_INTERNAL variable)
{
	extern void *__libc_per_thread(LIBC_INTERNAL __variable);
	static struct __locale_t locale;
	static struct __FILE file_streams[FOPEN_MAX];

	void *r = (void*)0;

	switch (variable) {
	case ERRNO:
		return __libc_per_thread(ERRNO);

	case THREAD_LOCALE:
		r = __libc_per_thread(THREAD_LOCALE);
		if (r) {
			break;
		}
		/* fallthru */

	case GLOBAL_LOCALE:
		r = &locale;
		break;

	case CTYPE:
		r = __libc(THREAD_LOCALE);
		if (((struct __locale_t*)r)->ctype[0] == '\0') {
			r = &locale;
		}
		r = ((struct __locale_t*)r)->lc_ctype.ctattr;
		break;

	case TOLOWER:
		r = __libc(THREAD_LOCALE);
		if (((struct __locale_t*)r)->ctype[0] == '\0') {
			r = &locale;
		}
		r = ((struct __locale_t*)r)->lc_ctype.ctolower;
		break;

	case TOUPPER:
		r = __libc(THREAD_LOCALE);
		if (((struct __locale_t*)r)->ctype[0] == '\0') {
			r = &locale;
		}
		r = ((struct __locale_t*)r)->lc_ctype.ctoupper;
		break;

	case LCONV:
		r = __libc(THREAD_LOCALE);
		if (((struct __locale_t*)r)->numeric[0] == '\0') {
			r = &locale;
		}
		r = &(((struct __locale_t*)r)->mn);
		break;

	case SYSCALL_LOOKUP:
		r = (void*)__syscall_lookup;
		break;

	case PRINTF:
		r = (void*)(__printf);
		break;

	case FILE_STREAMS:
		r = file_streams;
		break;

	case LOAD_LOCALE:
		r = (void*)(__load_locale);
		break;

	default:
		break;
	}

	return r;
}