diff --git a/newlib/libc/include/sys/reent.h b/newlib/libc/include/sys/reent.h index 8b67889ac..c045ca549 100644 --- a/newlib/libc/include/sys/reent.h +++ b/newlib/libc/include/sys/reent.h @@ -644,14 +644,23 @@ struct _reent of the above members (on the off chance that future binary compatibility would be broken otherwise). */ struct _glue __sglue; /* root of glue chain */ +# ifndef _REENT_GLOBAL_STDIO_STREAMS __FILE __sf[3]; /* first three file descriptors */ +# endif }; +#ifdef _REENT_GLOBAL_STDIO_STREAMS +extern __FILE __sf[3]; +#define _REENT_STDIO_STREAM(var, index) &__sf[index] +#else +#define _REENT_STDIO_STREAM(var, index) &(var)->__sf[index] +#endif + #define _REENT_INIT(var) \ { 0, \ - &(var).__sf[0], \ - &(var).__sf[1], \ - &(var).__sf[2], \ + _REENT_STDIO_STREAM(&(var), 0), \ + _REENT_STDIO_STREAM(&(var), 1), \ + _REENT_STDIO_STREAM(&(var), 2), \ 0, \ "", \ 0, \ @@ -696,9 +705,9 @@ struct _reent } #define _REENT_INIT_PTR_ZEROED(var) \ - { (var)->_stdin = &(var)->__sf[0]; \ - (var)->_stdout = &(var)->__sf[1]; \ - (var)->_stderr = &(var)->__sf[2]; \ + { (var)->_stdin = _REENT_STDIO_STREAM(var, 0); \ + (var)->_stdout = _REENT_STDIO_STREAM(var, 1); \ + (var)->_stderr = _REENT_STDIO_STREAM(var, 2); \ (var)->_new._reent._rand_next = 1; \ (var)->_new._reent._r48._seed[0] = _RAND48_SEED_0; \ (var)->_new._reent._r48._seed[1] = _RAND48_SEED_1; \ diff --git a/newlib/libc/stdio/findfp.c b/newlib/libc/stdio/findfp.c index b40aa9240..737bde102 100644 --- a/newlib/libc/stdio/findfp.c +++ b/newlib/libc/stdio/findfp.c @@ -35,6 +35,10 @@ const struct __sFILE_fake __sf_fake_stderr = {_NULL, 0, 0, 0, 0, {_NULL, 0}, 0, _NULL}; #endif +#ifdef _REENT_GLOBAL_STDIO_STREAMS +__FILE __sf[3]; +#endif + #if (defined (__OPTIMIZE_SIZE__) || defined (PREFER_SIZE_OVER_SPEED)) _NOINLINE_STATIC _VOID #else @@ -217,6 +221,14 @@ _DEFUN(_cleanup_r, (ptr), #else cleanup_func = _fclose_r; #endif +#endif +#ifdef _REENT_GLOBAL_STDIO_STREAMS + if (ptr->_stdin != &__sf[0]) + (*cleanup_func) (ptr, ptr->_stdin); + if (ptr->_stdout != &__sf[1]) + (*cleanup_func) (ptr, ptr->_stdout); + if (ptr->_stderr != &__sf[2]) + (*cleanup_func) (ptr, ptr->_stderr); #endif _CAST_VOID _fwalk_reent (ptr, cleanup_func); } @@ -250,8 +262,10 @@ _DEFUN(__sinit, (s), s->__sglue._next = NULL; #ifndef _REENT_SMALL +# ifndef _REENT_GLOBAL_STDIO_STREAMS s->__sglue._niobs = 3; s->__sglue._iobs = &s->__sf[0]; +# endif #else s->__sglue._niobs = 0; s->__sglue._iobs = NULL; @@ -265,9 +279,19 @@ _DEFUN(__sinit, (s), s->_stderr = __sfp(s); #endif +#ifdef _REENT_GLOBAL_STDIO_STREAMS + if (__sf[0]._cookie == NULL) { + _GLOBAL_REENT->__sglue._niobs = 3; + _GLOBAL_REENT->__sglue._iobs = &__sf[0]; + stdin_init (&__sf[0]); + stdout_init (&__sf[1]); + stderr_init (&__sf[2]); + } +#else stdin_init (s->_stdin); stdout_init (s->_stdout); stderr_init (s->_stderr); +#endif s->__sdidinit = 1;