From cb8d5c5d9d3fed61114ed1824b6f0615c31e86c3 Mon Sep 17 00:00:00 2001 From: Meco Man <920369182@qq.com> Date: Wed, 10 Nov 2021 18:33:43 -0500 Subject: [PATCH] =?UTF-8?q?[libc][gcc]=20=E9=87=8D=E6=96=B0=E6=A2=B3?= =?UTF-8?q?=E7=90=86fread=20fwrite=E6=A1=A9=E5=87=BD=E6=95=B0=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../libc/compilers/gcc/newlib/syscalls.c | 53 +++++++++++++------ 1 file changed, 37 insertions(+), 16 deletions(-) diff --git a/components/libc/compilers/gcc/newlib/syscalls.c b/components/libc/compilers/gcc/newlib/syscalls.c index e957aaf003..7c25ffac3f 100644 --- a/components/libc/compilers/gcc/newlib/syscalls.c +++ b/components/libc/compilers/gcc/newlib/syscalls.c @@ -216,13 +216,27 @@ int _open_r(struct _reent *ptr, const char *file, int flags, int mode) _ssize_t _read_r(struct _reent *ptr, int fd, void *buf, size_t nbytes) { -#ifdef RT_USING_POSIX_STDIO +#ifdef RT_USING_POSIX _ssize_t rc; - if (libc_stdio_get_console() < 0 && fd == STDIN_FILENO) + if (fd == STDIN_FILENO) { - LOG_W("Do not invoke standard input before initializing libc"); - return 0; +#ifdef RT_USING_POSIX_STDIO + if (libc_stdio_get_console() < 0) + { + LOG_W("Do not invoke standard input before initializing libc"); + return 0; + } +#else + ptr->_errno = ENOTSUP; + return -1; +#endif /* RT_USING_POSIX_STDIO */ } + else if (fd == STDOUT_FILENO || fd == STDERR_FILENO) + { + ptr->_errno = ENOTSUP; + return -1; + } + rc = read(fd, buf, nbytes); return rc; #else @@ -271,27 +285,34 @@ _ssize_t _write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes) { #ifdef RT_USING_POSIX _ssize_t rc; -#ifdef RT_USING_POSIX_STDIO - if (libc_stdio_get_console() < 0 && fd == STDOUT_FILENO) - { - LOG_W("Do not invoke standard output before initializing libc"); - return 0; - } -#endif /* RT_USING_POSIX_STDIO */ - rc = write(fd, buf, nbytes); - return rc; -#elif defined(RT_USING_CONSOLE) - if (STDOUT_FILENO == fd) +#endif /* RT_USING_POSIX */ + + if (fd == STDOUT_FILENO || fd == STDERR_FILENO) { +#ifdef RT_USING_CONSOLE rt_device_t console; console = rt_console_get_device(); if (console) return rt_device_write(console, -1, buf, nbytes); +#else + ptr->_errno = ENOTSUP; + return -1; +#endif /* RT_USING_CONSOLE */ } -#endif /* RT_USING_POSIX */ + else if (fd == STDIN_FILENO) + { + ptr->_errno = ENOTSUP; + return -1; + } + +#ifdef RT_USING_POSIX + rc = write(fd, buf, nbytes); + return rc; +#else ptr->_errno = ENOTSUP; return -1; +#endif /* RT_USING_POSIX */ } /* for exit() and abort() */