[libc][gcc] 重新梳理fread fwrite桩函数实现

This commit is contained in:
Meco Man 2021-11-10 18:33:43 -05:00
parent e6ae01e332
commit cb8d5c5d9d
1 changed files with 37 additions and 16 deletions

View File

@ -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) _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; _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"); #ifdef RT_USING_POSIX_STDIO
return 0; 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); rc = read(fd, buf, nbytes);
return rc; return rc;
#else #else
@ -271,27 +285,34 @@ _ssize_t _write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes)
{ {
#ifdef RT_USING_POSIX #ifdef RT_USING_POSIX
_ssize_t rc; _ssize_t rc;
#ifdef RT_USING_POSIX_STDIO #endif /* RT_USING_POSIX */
if (libc_stdio_get_console() < 0 && fd == STDOUT_FILENO)
{ if (fd == STDOUT_FILENO || fd == STDERR_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)
{ {
#ifdef RT_USING_CONSOLE
rt_device_t console; rt_device_t console;
console = rt_console_get_device(); console = rt_console_get_device();
if (console) if (console)
return rt_device_write(console, -1, buf, nbytes); 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; ptr->_errno = ENOTSUP;
return -1; return -1;
#endif /* RT_USING_POSIX */
} }
/* for exit() and abort() */ /* for exit() and abort() */