Freeing std streams when using _REENT_SMALL

Hans-Erik Floryd hans-erik.floryd@rt-labs.com
Fri Mar 27 17:30:00 GMT 2009


Hello,

The stdin, stdout and stderr streams are created in __sinit. In the case 
of _REENT_SMALL, they are not entered into the __sglue list:

#ifndef _REENT_SMALL
   s->__sglue._niobs = 3;
   s->__sglue._iobs = &s->__sf[0];
#else
   s->__sglue._niobs = 0;
   s->__sglue._iobs = NULL;
   s->_stdin = __sfp(s);
   s->_stdout = __sfp(s);
   s->_stderr = __sfp(s);
#endif

Because the std streams are not in __sglue, the __cleanup function will 
not call fclose on them. The following patch fixes this by calling 
fclose on the streams manually. That may not be the best way to fix it 
so any other suggestions are welcome.

Cheers,
  Hans-Erik Floryd

diff -Naur newlib-1.17.0/newlib/libc/reent/reent.c 
newlib-1.17.0-mod/newlib/libc/reent/reent.c
--- newlib-1.17.0/newlib/libc/reent/reent.c     2006-10-11 
10:04:50.000000000 +0200
+++ newlib-1.17.0-mod/newlib/libc/reent/reent.c 2009-03-27 
11:23:27.000000000 +0100
@@ -143,5 +143,14 @@
  #endif
    if (ptr->__cleanup)
      (*ptr->__cleanup) (ptr);
+
+#ifdef _REENT_SMALL
+  if (ptr->__sdidinit)
+    {
+      _fclose_r (ptr, ptr->_stdin);
+      _fclose_r (ptr, ptr->_stdout);
+      _fclose_r (ptr, ptr->_stderr);
+    }
+#endif
  }




More information about the Newlib mailing list