]> pd.if.org Git - pdclib/blob - platform/gandr/functions/_PDCLIB/_PDCLIB_stdinit.c
PDCLIB-26 #resolve fixed don't leak setvbuf allocated buffers
[pdclib] / platform / gandr / functions / _PDCLIB / _PDCLIB_stdinit.c
1 /* _PDCLIB_stdinit
2
3    This file is part of the Public Domain C Library (PDCLib).
4    Permission is granted to use, modify, and / or redistribute at will.
5 */
6
7 #include <stdio.h>
8 #include <locale.h>
9 #include <limits.h>
10
11 #ifndef REGTEST
12 #include <_PDCLIB_io.h>
13 #include <_PDCLIB_locale.h>
14 #include <_PDCLIB_clocale.h>
15 #include <threads.h>
16
17 /* TODO: This is proof-of-concept, requires finetuning. */
18 static char _PDCLIB_sin_buffer[BUFSIZ];
19 static char _PDCLIB_sout_buffer[BUFSIZ];
20 static char _PDCLIB_serr_buffer[BUFSIZ];
21
22 static unsigned char _PDCLIB_sin_ungetbuf[_PDCLIB_UNGETCBUFSIZE];
23 static unsigned char _PDCLIB_sout_ungetbuf[_PDCLIB_UNGETCBUFSIZE];
24 static unsigned char _PDCLIB_serr_ungetbuf[_PDCLIB_UNGETCBUFSIZE];
25
26 extern _PDCLIB_fileops_t _PDCLIB_fileops;
27
28 static FILE _PDCLIB_serr = { 
29     .ops        = &_PDCLIB_fileops, 
30     .handle     = { .pointer = NULL }, 
31     .buffer     = _PDCLIB_serr_buffer, 
32     .bufsize    = BUFSIZ, 
33     .bufidx     = 0, 
34     .bufend     = 0, 
35     .ungetidx   = 0, 
36     .ungetbuf   = _PDCLIB_serr_ungetbuf, 
37     .status     = _IONBF | _PDCLIB_FWRITE | _PDCLIB_STATIC, 
38     .filename   = NULL, 
39     .next       = NULL,
40 };
41 static FILE _PDCLIB_sout = { 
42     .ops        = &_PDCLIB_fileops, 
43     .handle     = { .pointer = NULL },
44     .buffer     = _PDCLIB_sout_buffer, 
45     .bufsize    = BUFSIZ, 
46     .bufidx     = 0, 
47     .bufend     = 0, 
48     .ungetidx   = 0, 
49     .ungetbuf   = _PDCLIB_sout_ungetbuf, 
50     .status     = _IOLBF | _PDCLIB_FWRITE | _PDCLIB_STATIC, 
51     .filename   = NULL, 
52     .next       = &_PDCLIB_serr 
53 };
54 static FILE _PDCLIB_sin  = { 
55     .ops        = &_PDCLIB_fileops, 
56     .handle     = { .pointer = NULL }, 
57     .buffer     = _PDCLIB_sin_buffer, 
58     .bufsize    = BUFSIZ, 
59     .bufidx     = 0, 
60     .bufend     = 0, 
61     .ungetidx   = 0, 
62     .ungetbuf   = _PDCLIB_sin_ungetbuf, 
63     .status     = _IOLBF | _PDCLIB_FREAD | _PDCLIB_STATIC, 
64     .filename   = NULL, 
65     .next       = &_PDCLIB_sout 
66 };
67
68 FILE * stdin  = &_PDCLIB_sin;
69 FILE * stdout = &_PDCLIB_sout;
70 FILE * stderr = &_PDCLIB_serr;
71
72 tss_t _PDCLIB_locale_tss;
73
74 /* FIXME: This approach is a possible attack vector. */
75 FILE * _PDCLIB_filelist = &_PDCLIB_sin;
76
77 /* "C" locale - defaulting to ASCII-7.
78    1 kByte (+ 4 byte) of <ctype.h> data.
79    Each line: flags, lowercase, uppercase, collation.
80 */
81 static const _PDCLIB_ctype_t global_ctype[] = {
82     { /* EOF */    0,    0,    0,    0 },
83     { /* NUL */ _PDCLIB_CTYPE_CNTRL,                                             0x00, 0x00, 0x00 },
84     { /* SOH */ _PDCLIB_CTYPE_CNTRL,                                             0x01, 0x01, 0x01 },
85     { /* STX */ _PDCLIB_CTYPE_CNTRL,                                             0x02, 0x02, 0x02 },
86     { /* ETX */ _PDCLIB_CTYPE_CNTRL,                                             0x03, 0x03, 0x03 },
87     { /* EOT */ _PDCLIB_CTYPE_CNTRL,                                             0x04, 0x04, 0x04 },
88     { /* ENQ */ _PDCLIB_CTYPE_CNTRL,                                             0x05, 0x05, 0x05 },
89     { /* ACK */ _PDCLIB_CTYPE_CNTRL,                                             0x06, 0x06, 0x06 },
90     { /* BEL */ _PDCLIB_CTYPE_CNTRL,                                             0x07, 0x07, 0x07 },
91     { /*  BS */ _PDCLIB_CTYPE_CNTRL,                                             0x08, 0x08, 0x08 },
92     { /*  HT */ _PDCLIB_CTYPE_CNTRL | _PDCLIB_CTYPE_BLANK | _PDCLIB_CTYPE_SPACE, 0x09, 0x09, 0x09 },
93     { /*  LF */ _PDCLIB_CTYPE_CNTRL | _PDCLIB_CTYPE_SPACE,                       0x0A, 0x0A, 0x0A },
94     { /*  VT */ _PDCLIB_CTYPE_CNTRL | _PDCLIB_CTYPE_SPACE,                       0x0B, 0x0B, 0x0B },
95     { /*  FF */ _PDCLIB_CTYPE_CNTRL | _PDCLIB_CTYPE_SPACE,                       0x0C, 0x0C, 0x0C },
96     { /*  CR */ _PDCLIB_CTYPE_CNTRL | _PDCLIB_CTYPE_SPACE,                       0x0D, 0x0D, 0x0D },
97     { /*  SO */ _PDCLIB_CTYPE_CNTRL,                                             0x0E, 0x0E, 0x0E },
98     { /*  SI */ _PDCLIB_CTYPE_CNTRL,                                             0x0F, 0x0F, 0x0F },
99     { /* DLE */ _PDCLIB_CTYPE_CNTRL,                                             0x10, 0x10, 0x10 },
100     { /* DC1 */ _PDCLIB_CTYPE_CNTRL,                                             0x11, 0x11, 0x11 },
101     { /* DC2 */ _PDCLIB_CTYPE_CNTRL,                                             0x12, 0x12, 0x12 },
102     { /* DC3 */ _PDCLIB_CTYPE_CNTRL,                                             0x13, 0x13, 0x13 },
103     { /* DC4 */ _PDCLIB_CTYPE_CNTRL,                                             0x14, 0x14, 0x14 },
104     { /* NAK */ _PDCLIB_CTYPE_CNTRL,                                             0x15, 0x15, 0x15 },
105     { /* SYN */ _PDCLIB_CTYPE_CNTRL,                                             0x16, 0x16, 0x16 },
106     { /* ETB */ _PDCLIB_CTYPE_CNTRL,                                             0x17, 0x17, 0x17 },
107     { /* CAN */ _PDCLIB_CTYPE_CNTRL,                                             0x18, 0x18, 0x18 },
108     { /*  EM */ _PDCLIB_CTYPE_CNTRL,                                             0x19, 0x19, 0x19 },
109     { /* SUB */ _PDCLIB_CTYPE_CNTRL,                                             0x1A, 0x1A, 0x1A },
110     { /* ESC */ _PDCLIB_CTYPE_CNTRL,                                             0x1B, 0x1B, 0x1B },
111     { /*  FS */ _PDCLIB_CTYPE_CNTRL,                                             0x1C, 0x1C, 0x1C },
112     { /*  GS */ _PDCLIB_CTYPE_CNTRL,                                             0x1D, 0x1D, 0x1D },
113     { /*  RS */ _PDCLIB_CTYPE_CNTRL,                                             0x1E, 0x1E, 0x1E },
114     { /*  US */ _PDCLIB_CTYPE_CNTRL,                                             0x1F, 0x1F, 0x1F },
115     { /*  SP */ _PDCLIB_CTYPE_BLANK | _PDCLIB_CTYPE_SPACE,                       0x20, 0x20, 0x20 },
116     { /* '!' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_PUNCT,                       0x21, 0x21, 0x21 },
117     { /* '"' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_PUNCT,                       0x22, 0x22, 0x22 },
118     { /* '#' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_PUNCT,                       0x23, 0x23, 0x23 },
119     { /* '$' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_PUNCT,                       0x24, 0x24, 0x24 },
120     { /* '%' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_PUNCT,                       0x25, 0x25, 0x25 },
121     { /* '&' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_PUNCT,                       0x26, 0x26, 0x26 },
122     { /* ''' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_PUNCT,                       0x27, 0x27, 0x27 },
123     { /* '(' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_PUNCT,                       0x28, 0x28, 0x28 },
124     { /* ')' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_PUNCT,                       0x29, 0x29, 0x29 },
125     { /* '*' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_PUNCT,                       0x2A, 0x2A, 0x2A },
126     { /* '+' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_PUNCT,                       0x2B, 0x2B, 0x2B },
127     { /* ',' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_PUNCT,                       0x2C, 0x2C, 0x2C },
128     { /* '-' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_PUNCT,                       0x2D, 0x2D, 0x2D },
129     { /* '.' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_PUNCT,                       0x2E, 0x2E, 0x2E },
130     { /* '/' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_PUNCT,                       0x2F, 0x2F, 0x2F },
131     { /* '0' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_DIGIT | _PDCLIB_CTYPE_XDIGT, 0x30, 0x30, 0x30 },
132     { /* '1' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_DIGIT | _PDCLIB_CTYPE_XDIGT, 0x31, 0x31, 0x31 },
133     { /* '2' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_DIGIT | _PDCLIB_CTYPE_XDIGT, 0x32, 0x32, 0x32 },
134     { /* '3' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_DIGIT | _PDCLIB_CTYPE_XDIGT, 0x33, 0x33, 0x33 },
135     { /* '4' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_DIGIT | _PDCLIB_CTYPE_XDIGT, 0x34, 0x34, 0x34 },
136     { /* '5' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_DIGIT | _PDCLIB_CTYPE_XDIGT, 0x35, 0x35, 0x35 },
137     { /* '6' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_DIGIT | _PDCLIB_CTYPE_XDIGT, 0x36, 0x36, 0x36 },
138     { /* '7' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_DIGIT | _PDCLIB_CTYPE_XDIGT, 0x37, 0x37, 0x37 },
139     { /* '8' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_DIGIT | _PDCLIB_CTYPE_XDIGT, 0x38, 0x38, 0x38 },
140     { /* '9' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_DIGIT | _PDCLIB_CTYPE_XDIGT, 0x39, 0x39, 0x39 },
141     { /* ':' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_PUNCT,                       0x3A, 0x3A, 0x3A },
142     { /* ';' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_PUNCT,                       0x3B, 0x3B, 0x3B },
143     { /* '<' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_PUNCT,                       0x3C, 0x3C, 0x3C },
144     { /* '=' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_PUNCT,                       0x3D, 0x3D, 0x3D },
145     { /* '>' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_PUNCT,                       0x3E, 0x3E, 0x3E },
146     { /* '?' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_PUNCT,                       0x3F, 0x3F, 0x3F },
147     { /* '@' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_PUNCT,                       0x40, 0x40, 0x40 },
148     { /* 'A' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_UPPER | _PDCLIB_CTYPE_XDIGT, 0x41, 0x61, 0x41 },
149     { /* 'B' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_UPPER | _PDCLIB_CTYPE_XDIGT, 0x42, 0x62, 0x42 },
150     { /* 'C' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_UPPER | _PDCLIB_CTYPE_XDIGT, 0x43, 0x63, 0x43 },
151     { /* 'D' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_UPPER | _PDCLIB_CTYPE_XDIGT, 0x44, 0x64, 0x44 },
152     { /* 'E' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_UPPER | _PDCLIB_CTYPE_XDIGT, 0x45, 0x65, 0x45 },
153     { /* 'F' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_UPPER | _PDCLIB_CTYPE_XDIGT, 0x46, 0x66, 0x46 },
154     { /* 'G' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_UPPER, 0x47, 0x67, 0x47 },
155     { /* 'H' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_UPPER, 0x48, 0x68, 0x48 },
156     { /* 'I' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_UPPER, 0x49, 0x69, 0x49 },
157     { /* 'J' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_UPPER, 0x4A, 0x6A, 0x4A },
158     { /* 'K' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_UPPER, 0x4B, 0x6B, 0x4B },
159     { /* 'L' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_UPPER, 0x4C, 0x6C, 0x4C },
160     { /* 'M' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_UPPER, 0x4D, 0x6D, 0x4D },
161     { /* 'N' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_UPPER, 0x4E, 0x6E, 0x4E },
162     { /* 'O' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_UPPER, 0x4F, 0x6F, 0x4F },
163     { /* 'P' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_UPPER, 0x50, 0x70, 0x50 },
164     { /* 'Q' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_UPPER, 0x51, 0x71, 0x51 },
165     { /* 'R' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_UPPER, 0x52, 0x72, 0x52 },
166     { /* 'S' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_UPPER, 0x53, 0x73, 0x53 },
167     { /* 'T' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_UPPER, 0x54, 0x74, 0x54 },
168     { /* 'U' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_UPPER, 0x55, 0x75, 0x55 },
169     { /* 'V' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_UPPER, 0x56, 0x76, 0x56 },
170     { /* 'W' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_UPPER, 0x57, 0x77, 0x57 },
171     { /* 'X' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_UPPER, 0x58, 0x78, 0x58 },
172     { /* 'Y' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_UPPER, 0x59, 0x79, 0x59 },
173     { /* 'Z' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_UPPER, 0x5A, 0x7A, 0x5A },
174     { /* '[' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_PUNCT,                       0x5B, 0x5B, 0x5B },
175     { /* '\' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_PUNCT,                       0x5C, 0x5C, 0x5C },
176     { /* ']' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_PUNCT,                       0x5D, 0x5D, 0x5D },
177     { /* '^' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_PUNCT,                       0x5E, 0x5E, 0x5E },
178     { /* '_' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_PUNCT,                       0x5F, 0x5F, 0x5F },
179     { /* '`' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_PUNCT,                       0x60, 0x60, 0x60 },
180     { /* 'a' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_LOWER | _PDCLIB_CTYPE_XDIGT, 0x41, 0x61, 0x61 },
181     { /* 'b' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_LOWER | _PDCLIB_CTYPE_XDIGT, 0x42, 0x62, 0x62 },
182     { /* 'c' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_LOWER | _PDCLIB_CTYPE_XDIGT, 0x43, 0x63, 0x63 },
183     { /* 'd' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_LOWER | _PDCLIB_CTYPE_XDIGT, 0x44, 0x64, 0x64 },
184     { /* 'e' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_LOWER | _PDCLIB_CTYPE_XDIGT, 0x45, 0x65, 0x65 },
185     { /* 'f' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_LOWER | _PDCLIB_CTYPE_XDIGT, 0x46, 0x66, 0x66 },
186     { /* 'g' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_LOWER, 0x47, 0x67, 0x67 },
187     { /* 'h' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_LOWER, 0x48, 0x68, 0x68 },
188     { /* 'i' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_LOWER, 0x49, 0x69, 0x69 },
189     { /* 'j' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_LOWER, 0x4A, 0x6A, 0x6A },
190     { /* 'k' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_LOWER, 0x4B, 0x6B, 0x6B },
191     { /* 'l' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_LOWER, 0x4C, 0x6C, 0x6C },
192     { /* 'm' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_LOWER, 0x4D, 0x6D, 0x6D },
193     { /* 'n' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_LOWER, 0x4E, 0x6E, 0x6E },
194     { /* 'o' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_LOWER, 0x4F, 0x6F, 0x6F },
195     { /* 'p' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_LOWER, 0x50, 0x70, 0x70 },
196     { /* 'q' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_LOWER, 0x51, 0x71, 0x71 },
197     { /* 'r' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_LOWER, 0x52, 0x72, 0x72 },
198     { /* 's' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_LOWER, 0x53, 0x73, 0x73 },
199     { /* 't' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_LOWER, 0x54, 0x74, 0x74 },
200     { /* 'u' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_LOWER, 0x55, 0x75, 0x75 },
201     { /* 'v' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_LOWER, 0x56, 0x76, 0x76 },
202     { /* 'w' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_LOWER, 0x57, 0x77, 0x77 },
203     { /* 'x' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_LOWER, 0x58, 0x78, 0x78 },
204     { /* 'y' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_LOWER, 0x59, 0x79, 0x79 },
205     { /* 'z' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_ALPHA | _PDCLIB_CTYPE_LOWER, 0x5A, 0x7A, 0x7A },
206     { /* '{' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_PUNCT,                       0x7B, 0x7B, 0x7B },
207     { /* '|' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_PUNCT,                       0x7C, 0x7C, 0x7C },
208     { /* '}' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_PUNCT,                       0x7D, 0x7D, 0x7D },
209     { /* '~' */ _PDCLIB_CTYPE_GRAPH | _PDCLIB_CTYPE_PUNCT,                       0x7E, 0x7E, 0x7E },
210     { /* DEL */ _PDCLIB_CTYPE_CNTRL,                                             0x7F, 0x7F, 0x7F },
211     { 0x00, 0x80, 0x80, 0x80 },
212     { 0x00, 0x81, 0x81, 0x81 },
213     { 0x00, 0x82, 0x82, 0x82 },
214     { 0x00, 0x83, 0x83, 0x83 },
215     { 0x00, 0x84, 0x84, 0x84 },
216     { 0x00, 0x85, 0x85, 0x85 },
217     { 0x00, 0x86, 0x86, 0x86 },
218     { 0x00, 0x87, 0x87, 0x87 },
219     { 0x00, 0x88, 0x88, 0x88 },
220     { 0x00, 0x89, 0x89, 0x89 },
221     { 0x00, 0x8A, 0x8A, 0x8A },
222     { 0x00, 0x8B, 0x8B, 0x8B },
223     { 0x00, 0x8C, 0x8C, 0x8C },
224     { 0x00, 0x8D, 0x8D, 0x8D },
225     { 0x00, 0x8E, 0x8E, 0x8E },
226     { 0x00, 0x8F, 0x8F, 0x8F },
227     { 0x00, 0x90, 0x90, 0x90 },
228     { 0x00, 0x91, 0x91, 0x91 },
229     { 0x00, 0x92, 0x92, 0x92 },
230     { 0x00, 0x93, 0x93, 0x93 },
231     { 0x00, 0x94, 0x94, 0x94 },
232     { 0x00, 0x95, 0x95, 0x95 },
233     { 0x00, 0x96, 0x96, 0x96 },
234     { 0x00, 0x97, 0x97, 0x97 },
235     { 0x00, 0x98, 0x98, 0x98 },
236     { 0x00, 0x99, 0x99, 0x99 },
237     { 0x00, 0x9A, 0x9A, 0x9A },
238     { 0x00, 0x9B, 0x9B, 0x9B },
239     { 0x00, 0x9C, 0x9C, 0x9C },
240     { 0x00, 0x9D, 0x9D, 0x9D },
241     { 0x00, 0x9E, 0x9E, 0x9E },
242     { 0x00, 0x9F, 0x9F, 0x9F },
243     { 0x00, 0xA0, 0xA0, 0xA0 },
244     { 0x00, 0xA1, 0xA1, 0xA1 },
245     { 0x00, 0xA2, 0xA2, 0xA2 },
246     { 0x00, 0xA3, 0xA3, 0xA3 },
247     { 0x00, 0xA4, 0xA4, 0xA4 },
248     { 0x00, 0xA5, 0xA5, 0xA5 },
249     { 0x00, 0xA6, 0xA6, 0xA6 },
250     { 0x00, 0xA7, 0xA7, 0xA7 },
251     { 0x00, 0xA8, 0xA8, 0xA8 },
252     { 0x00, 0xA9, 0xA9, 0xA9 },
253     { 0x00, 0xAA, 0xAA, 0xAA },
254     { 0x00, 0xAB, 0xAB, 0xAB },
255     { 0x00, 0xAC, 0xAC, 0xAC },
256     { 0x00, 0xAD, 0xAD, 0xAD },
257     { 0x00, 0xAE, 0xAE, 0xAE },
258     { 0x00, 0xAF, 0xAF, 0xAF },
259     { 0x00, 0xB0, 0xB0, 0xB0 },
260     { 0x00, 0xB1, 0xB1, 0xB1 },
261     { 0x00, 0xB2, 0xB2, 0xB2 },
262     { 0x00, 0xB3, 0xB3, 0xB3 },
263     { 0x00, 0xB4, 0xB4, 0xB4 },
264     { 0x00, 0xB5, 0xB5, 0xB5 },
265     { 0x00, 0xB6, 0xB6, 0xB6 },
266     { 0x00, 0xB7, 0xB7, 0xB7 },
267     { 0x00, 0xB8, 0xB8, 0xB8 },
268     { 0x00, 0xB9, 0xB9, 0xB9 },
269     { 0x00, 0xBA, 0xBA, 0xBA },
270     { 0x00, 0xBB, 0xBB, 0xBB },
271     { 0x00, 0xBC, 0xBC, 0xBC },
272     { 0x00, 0xBD, 0xBD, 0xBD },
273     { 0x00, 0xBE, 0xBE, 0xBE },
274     { 0x00, 0xBF, 0xBF, 0xBF },
275     { 0x00, 0xC0, 0xC0, 0xC0 },
276     { 0x00, 0xC1, 0xC1, 0xC1 },
277     { 0x00, 0xC2, 0xC2, 0xC2 },
278     { 0x00, 0xC3, 0xC3, 0xC3 },
279     { 0x00, 0xC4, 0xC4, 0xC4 },
280     { 0x00, 0xC5, 0xC5, 0xC5 },
281     { 0x00, 0xC6, 0xC6, 0xC6 },
282     { 0x00, 0xC7, 0xC7, 0xC7 },
283     { 0x00, 0xC8, 0xC8, 0xC8 },
284     { 0x00, 0xC9, 0xC9, 0xC9 },
285     { 0x00, 0xCA, 0xCA, 0xCA },
286     { 0x00, 0xCB, 0xCB, 0xCB },
287     { 0x00, 0xCC, 0xCC, 0xCC },
288     { 0x00, 0xCD, 0xCD, 0xCD },
289     { 0x00, 0xCE, 0xCE, 0xCE },
290     { 0x00, 0xCF, 0xCF, 0xCF },
291     { 0x00, 0xD0, 0xD0, 0xD0 },
292     { 0x00, 0xD1, 0xD1, 0xD1 },
293     { 0x00, 0xD2, 0xD2, 0xD2 },
294     { 0x00, 0xD3, 0xD3, 0xD3 },
295     { 0x00, 0xD4, 0xD4, 0xD4 },
296     { 0x00, 0xD5, 0xD5, 0xD5 },
297     { 0x00, 0xD6, 0xD6, 0xD6 },
298     { 0x00, 0xD7, 0xD7, 0xD7 },
299     { 0x00, 0xD8, 0xD8, 0xD8 },
300     { 0x00, 0xD9, 0xD9, 0xD9 },
301     { 0x00, 0xDA, 0xDA, 0xDA },
302     { 0x00, 0xDB, 0xDB, 0xDB },
303     { 0x00, 0xDC, 0xDC, 0xDC },
304     { 0x00, 0xDD, 0xDD, 0xDD },
305     { 0x00, 0xDE, 0xDE, 0xDE },
306     { 0x00, 0xDF, 0xDF, 0xDF },
307     { 0x00, 0xE0, 0xE0, 0xE0 },
308     { 0x00, 0xE1, 0xE1, 0xE1 },
309     { 0x00, 0xE2, 0xE2, 0xE2 },
310     { 0x00, 0xE3, 0xE3, 0xE3 },
311     { 0x00, 0xE4, 0xE4, 0xE4 },
312     { 0x00, 0xE5, 0xE5, 0xE5 },
313     { 0x00, 0xE6, 0xE6, 0xE6 },
314     { 0x00, 0xE7, 0xE7, 0xE7 },
315     { 0x00, 0xE8, 0xE8, 0xE8 },
316     { 0x00, 0xE9, 0xE9, 0xE9 },
317     { 0x00, 0xEA, 0xEA, 0xEA },
318     { 0x00, 0xEB, 0xEB, 0xEB },
319     { 0x00, 0xEC, 0xEC, 0xEC },
320     { 0x00, 0xED, 0xED, 0xED },
321     { 0x00, 0xEE, 0xEE, 0xEE },
322     { 0x00, 0xEF, 0xEF, 0xEF },
323     { 0x00, 0xF0, 0xF0, 0xF0 },
324     { 0x00, 0xF1, 0xF1, 0xF1 },
325     { 0x00, 0xF2, 0xF2, 0xF2 },
326     { 0x00, 0xF3, 0xF3, 0xF3 },
327     { 0x00, 0xF4, 0xF4, 0xF4 },
328     { 0x00, 0xF5, 0xF5, 0xF5 },
329     { 0x00, 0xF6, 0xF6, 0xF6 },
330     { 0x00, 0xF7, 0xF7, 0xF7 },
331     { 0x00, 0xF8, 0xF8, 0xF8 },
332     { 0x00, 0xF9, 0xF9, 0xF9 },
333     { 0x00, 0xFA, 0xFA, 0xFA },
334     { 0x00, 0xFB, 0xFB, 0xFB },
335     { 0x00, 0xFC, 0xFC, 0xFC },
336     { 0x00, 0xFD, 0xFD, 0xFD },
337     { 0x00, 0xFE, 0xFE, 0xFE },
338     { 0x00, 0xFF, 0xFF, 0xFF }
339 };
340
341 extern const struct _PDCLIB_charcodec _PDCLIB_ascii_codec;
342 struct _PDCLIB_locale _PDCLIB_global_locale = {
343     ._Codec = &_PDCLIB_ascii_codec,
344     ._Conv  = { 
345         /* decimal_point      */ (char *)".",
346         /* thousands_sep      */ (char *)"",
347         /* grouping           */ (char *)"",
348         /* mon_decimal_point  */ (char *)"",
349         /* mon_thousands_sep  */ (char *)"",
350         /* mon_grouping       */ (char *)"",
351         /* positive_sign      */ (char *)"",
352         /* negative_sign      */ (char *)"",
353         /* currency_symbol    */ (char *)"",
354         /* int_curr_symbol    */ (char *)"",
355         /* frac_digits        */ CHAR_MAX,
356         /* p_cs_precedes      */ CHAR_MAX,
357         /* n_cs_precedes      */ CHAR_MAX,
358         /* p_sep_by_space     */ CHAR_MAX,
359         /* n_sep_by_space     */ CHAR_MAX,
360         /* p_sign_posn        */ CHAR_MAX,
361         /* n_sign_posn        */ CHAR_MAX,
362         /* int_frac_digits    */ CHAR_MAX,
363         /* int_p_cs_precedes  */ CHAR_MAX,
364         /* int_n_cs_precedes  */ CHAR_MAX,
365         /* int_p_sep_by_space */ CHAR_MAX,
366         /* int_n_sep_by_space */ CHAR_MAX,
367         /* int_p_sign_posn    */ CHAR_MAX,
368         /* int_n_sign_posn    */ CHAR_MAX,
369     },
370     ._CType = &global_ctype[1],
371     ._ErrnoStr = {
372         /* no error */ (char *)"",
373         /* ERANGE   */ (char *)"ERANGE (Range error)",
374         /* EDOM     */ (char *)"EDOM (Domain error)",
375         /* EILSEQ   */ (char *)"EILSEQ (Illegal sequence)"
376     },
377 };
378
379 /* Todo: Better solution than this! */
380 __attribute__((constructor)) void init_stdio(void)
381 {
382     _PDCLIB_initclocale( &_PDCLIB_global_locale );
383     mtx_init(&stdin->lock,  mtx_recursive);
384     mtx_init(&stdout->lock, mtx_recursive);
385     mtx_init(&stderr->lock, mtx_recursive);
386 }
387
388 #endif
389
390 #ifdef TEST
391 #include <_PDCLIB_test.h>
392
393 int main( void )
394 {
395     /* Testing covered by several other testdrivers using stdin / stdout / 
396        stderr.
397     */
398     return TEST_RESULTS;
399 }
400
401 #endif