This is the mail archive of the
newlib@sourceware.org
mailing list for the newlib project.
Freeing std streams when using _REENT_SMALL
- From: Hans-Erik Floryd <hans-erik dot floryd at rt-labs dot com>
- To: newlib at sources dot redhat dot com
- Date: Fri, 27 Mar 2009 11:37:47 +0100
- Subject: Freeing std streams when using _REENT_SMALL
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
}