1111 * 2020-02-24 Meco Man fix bug of _isatty_r()
1212 */
1313
14+ #include <reent.h>
1415#include <rtthread.h>
1516#include <stddef.h>
17+ #include <unistd.h>
1618#include <sys/errno.h>
1719
1820#define DBG_TAG "newlib.syscalls"
@@ -90,6 +92,10 @@ void __libc_init_array(void)
9092#include <dlmodule.h>
9193#endif
9294
95+ #define DBG_TAG "newlib.syscalls"
96+ #define DBG_LVL DBG_INFO
97+ #include <rtdbg.h>
98+
9399/* Reentrant versions of system calls. */
94100
95101#ifndef _REENT_ONLY
@@ -219,7 +225,11 @@ _ssize_t _read_r(struct _reent *ptr, int fd, void *buf, size_t nbytes)
219225 return -1 ;
220226#else
221227 _ssize_t rc ;
222-
228+ if (libc_stdio_get_console () < 0 && fd == STDIN_FILENO )
229+ {
230+ LOG_W ("Do not invoke standard input before initializing libc" );
231+ return 0 ;
232+ }
223233 rc = read (fd , buf , nbytes );
224234 return rc ;
225235#endif
@@ -275,7 +285,7 @@ _ssize_t _write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes)
275285{
276286#ifndef RT_USING_DFS
277287#if defined(RT_USING_CONSOLE ) && defined(RT_USING_DEVICE )
278- if (fileno ( stdout ) == fd )
288+ if (STDOUT_FILENO == fd )
279289 {
280290 rt_device_t console ;
281291
@@ -291,7 +301,11 @@ _ssize_t _write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes)
291301#endif /*RT_USING_DEVICE*/
292302#else
293303 _ssize_t rc ;
294-
304+ if (libc_stdio_get_console () < 0 && fd == STDOUT_FILENO )
305+ {
306+ LOG_W ("Do not invoke standard output before initializing libc" );
307+ return 0 ;
308+ }
295309 rc = write (fd , buf , nbytes );
296310 return rc ;
297311#endif
0 commit comments