Browse Source

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

Meco Man 3 years ago
parent
commit
cb8d5c5d9d
1 changed files with 37 additions and 16 deletions
  1. 37 16
      components/libc/compilers/gcc/newlib/syscalls.c

+ 37 - 16
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() */