[gandr] split _PDCLIB_stdinit to better separate stdio and locale
authorOwen Shepherd <owen.shepherd@e43.eu>
Tue, 7 Oct 2014 22:24:52 +0000 (23:24 +0100)
committerOwen Shepherd <owen.shepherd@e43.eu>
Tue, 7 Oct 2014 22:24:52 +0000 (23:24 +0100)
platform/gandr/functions/_PDCLIB/_PDCLIB_stdinit.c
platform/gandr/functions/_PDCLIB/_PDCLIB_stdstreams.c [new file with mode: 0644]

index b6b378e3957c1891443c032c2f2be6be496fb18f..9504186659eb8c7333bf15b3c2a156cb723bf642 100644 (file)
@@ -9,71 +9,12 @@
 #include <limits.h>
 
 #ifndef REGTEST
-#include <_PDCLIB_io.h>
 #include <_PDCLIB_locale.h>
 #include <_PDCLIB_clocale.h>
 #include <threads.h>
 
-/* TODO: This is proof-of-concept, requires finetuning. */
-static char _PDCLIB_sin_buffer[BUFSIZ];
-static char _PDCLIB_sout_buffer[BUFSIZ];
-static char _PDCLIB_serr_buffer[BUFSIZ];
-
-static unsigned char _PDCLIB_sin_ungetbuf[_PDCLIB_UNGETCBUFSIZE];
-static unsigned char _PDCLIB_sout_ungetbuf[_PDCLIB_UNGETCBUFSIZE];
-static unsigned char _PDCLIB_serr_ungetbuf[_PDCLIB_UNGETCBUFSIZE];
-
-extern _PDCLIB_fileops_t _PDCLIB_fileops;
-
-static FILE _PDCLIB_serr = { 
-    .ops        = &_PDCLIB_fileops, 
-    .handle     = { .pointer = NULL }, 
-    .buffer     = _PDCLIB_serr_buffer, 
-    .bufsize    = BUFSIZ, 
-    .bufidx     = 0, 
-    .bufend     = 0, 
-    .ungetidx   = 0, 
-    .ungetbuf   = _PDCLIB_serr_ungetbuf, 
-    .status     = _IONBF | _PDCLIB_FWRITE | _PDCLIB_STATIC, 
-    .filename   = NULL, 
-    .next       = NULL,
-};
-static FILE _PDCLIB_sout = { 
-    .ops        = &_PDCLIB_fileops, 
-    .handle     = { .pointer = NULL },
-    .buffer     = _PDCLIB_sout_buffer, 
-    .bufsize    = BUFSIZ, 
-    .bufidx     = 0, 
-    .bufend     = 0, 
-    .ungetidx   = 0, 
-    .ungetbuf   = _PDCLIB_sout_ungetbuf, 
-    .status     = _IOLBF | _PDCLIB_FWRITE | _PDCLIB_STATIC, 
-    .filename   = NULL, 
-    .next       = &_PDCLIB_serr 
-};
-static FILE _PDCLIB_sin  = { 
-    .ops        = &_PDCLIB_fileops, 
-    .handle     = { .pointer = NULL }, 
-    .buffer     = _PDCLIB_sin_buffer, 
-    .bufsize    = BUFSIZ, 
-    .bufidx     = 0, 
-    .bufend     = 0, 
-    .ungetidx   = 0, 
-    .ungetbuf   = _PDCLIB_sin_ungetbuf, 
-    .status     = _IOLBF | _PDCLIB_FREAD | _PDCLIB_STATIC, 
-    .filename   = NULL, 
-    .next       = &_PDCLIB_sout 
-};
-
-FILE * stdin  = &_PDCLIB_sin;
-FILE * stdout = &_PDCLIB_sout;
-FILE * stderr = &_PDCLIB_serr;
-
 tss_t _PDCLIB_locale_tss;
 
-/* FIXME: This approach is a possible attack vector. */
-FILE * _PDCLIB_filelist = &_PDCLIB_sin;
-
 /* "C" locale - defaulting to ASCII-7.
    1 kByte (+ 4 byte) of <ctype.h> data.
    Each line: flags, lowercase, uppercase, collation.
@@ -341,7 +282,7 @@ static const _PDCLIB_ctype_t global_ctype[] = {
 extern const struct _PDCLIB_charcodec _PDCLIB_ascii_codec;
 struct _PDCLIB_locale _PDCLIB_global_locale = {
     ._Codec = &_PDCLIB_ascii_codec,
-    ._Conv  = { 
+    ._Conv  = {
         /* decimal_point      */ (char *)".",
         /* thousands_sep      */ (char *)"",
         /* grouping           */ (char *)"",
@@ -376,15 +317,6 @@ struct _PDCLIB_locale _PDCLIB_global_locale = {
     },
 };
 
-/* Todo: Better solution than this! */
-__attribute__((constructor)) void init_stdio(void)
-{
-    _PDCLIB_initclocale( &_PDCLIB_global_locale );
-    mtx_init(&stdin->lock,  mtx_recursive);
-    mtx_init(&stdout->lock, mtx_recursive);
-    mtx_init(&stderr->lock, mtx_recursive);
-}
-
 #endif
 
 #ifdef TEST
@@ -392,7 +324,7 @@ __attribute__((constructor)) void init_stdio(void)
 
 int main( void )
 {
-    /* Testing covered by several other testdrivers using stdin / stdout / 
+    /* Testing covered by several other testdrivers using stdin / stdout /
        stderr.
     */
     return TEST_RESULTS;
diff --git a/platform/gandr/functions/_PDCLIB/_PDCLIB_stdstreams.c b/platform/gandr/functions/_PDCLIB/_PDCLIB_stdstreams.c
new file mode 100644 (file)
index 0000000..9ce1115
--- /dev/null
@@ -0,0 +1,93 @@
+/* _PDCLIB_stdstream
+
+   This file is part of the Public Domain C Library (PDCLib).
+   Permission is granted to use, modify, and / or redistribute at will.
+*/
+
+#include <stdio.h>
+#include <locale.h>
+#include <limits.h>
+
+#ifndef REGTEST
+#include <_PDCLIB_io.h>
+#include <threads.h>
+
+static char _PDCLIB_sin_buffer[BUFSIZ];
+static char _PDCLIB_sout_buffer[BUFSIZ];
+static char _PDCLIB_serr_buffer[BUFSIZ];
+
+static unsigned char _PDCLIB_sin_ungetbuf[_PDCLIB_UNGETCBUFSIZE];
+static unsigned char _PDCLIB_sout_ungetbuf[_PDCLIB_UNGETCBUFSIZE];
+static unsigned char _PDCLIB_serr_ungetbuf[_PDCLIB_UNGETCBUFSIZE];
+
+extern _PDCLIB_fileops_t _PDCLIB_fileops;
+
+static FILE _PDCLIB_serr = {
+    .ops        = &_PDCLIB_fileops,
+    .handle     = { .pointer = NULL },
+    .buffer     = _PDCLIB_serr_buffer,
+    .bufsize    = BUFSIZ,
+    .bufidx     = 0,
+    .bufend     = 0,
+    .ungetidx   = 0,
+    .ungetbuf   = _PDCLIB_serr_ungetbuf,
+    .status     = _IONBF | _PDCLIB_FWRITE | _PDCLIB_STATIC,
+    .filename   = NULL,
+    .next       = NULL,
+};
+static FILE _PDCLIB_sout = {
+    .ops        = &_PDCLIB_fileops,
+    .handle     = { .pointer = NULL },
+    .buffer     = _PDCLIB_sout_buffer,
+    .bufsize    = BUFSIZ,
+    .bufidx     = 0,
+    .bufend     = 0,
+    .ungetidx   = 0,
+    .ungetbuf   = _PDCLIB_sout_ungetbuf,
+    .status     = _IOLBF | _PDCLIB_FWRITE | _PDCLIB_STATIC,
+    .filename   = NULL,
+    .next       = &_PDCLIB_serr
+};
+static FILE _PDCLIB_sin  = {
+    .ops        = &_PDCLIB_fileops,
+    .handle     = { .pointer = NULL },
+    .buffer     = _PDCLIB_sin_buffer,
+    .bufsize    = BUFSIZ,
+    .bufidx     = 0,
+    .bufend     = 0,
+    .ungetidx   = 0,
+    .ungetbuf   = _PDCLIB_sin_ungetbuf,
+    .status     = _IOLBF | _PDCLIB_FREAD | _PDCLIB_STATIC,
+    .filename   = NULL,
+    .next       = &_PDCLIB_sout
+};
+
+FILE * stdin  = &_PDCLIB_sin;
+FILE * stdout = &_PDCLIB_sout;
+FILE * stderr = &_PDCLIB_serr;
+
+FILE * _PDCLIB_filelist = &_PDCLIB_sin;
+
+/* Todo: Better solution than this! */
+__attribute__((constructor)) static void init_stdio(void)
+{
+    _PDCLIB_initclocale( &_PDCLIB_global_locale );
+    mtx_init(&stdin->lock,  mtx_recursive);
+    mtx_init(&stdout->lock, mtx_recursive);
+    mtx_init(&stderr->lock, mtx_recursive);
+}
+
+#endif
+
+#ifdef TEST
+#include <_PDCLIB_test.h>
+
+int main( void )
+{
+    /* Testing covered by several other testdrivers using stdin / stdout /
+       stderr.
+    */
+    return TEST_RESULTS;
+}
+
+#endif