You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by ma...@apache.org on 2021/11/24 06:51:14 UTC

[incubator-nuttx] branch master updated: libc/stdoutstream: restore the output method to fputc()

This is an automated email from the ASF dual-hosted git repository.

masayuki pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new f3b019d  libc/stdoutstream: restore the output method to fputc()
f3b019d is described below

commit f3b019d1d25eb6f0979802728058c4f67d7589cd
Author: chao.an <an...@xiaomi.com>
AuthorDate: Wed Nov 24 13:09:31 2021 +0800

    libc/stdoutstream: restore the output method to fputc()
    
    keep the default behavior for stdoutstream since the character
    needs to flush every output
    
    Signed-off-by: chao.an <an...@xiaomi.com>
---
 libs/libc/stdio/lib_rawsostream.c  |  4 ++--
 libs/libc/stdio/lib_stdoutstream.c | 48 ++++++++++++++------------------------
 2 files changed, 19 insertions(+), 33 deletions(-)

diff --git a/libs/libc/stdio/lib_rawsostream.c b/libs/libc/stdio/lib_rawsostream.c
index 9af34ad..14940f2 100644
--- a/libs/libc/stdio/lib_rawsostream.c
+++ b/libs/libc/stdio/lib_rawsostream.c
@@ -46,9 +46,9 @@ static int rawoutstream_puts(FAR struct lib_outstream_s *this,
   FAR struct lib_rawoutstream_s *rthis =
                                 (FAR struct lib_rawoutstream_s *)this;
   int nwritten = 0;
-  int ret;
+  int ret = 0;
 
-  while (1)
+  while (nwritten != len)
     {
       ret = _NX_WRITE(rthis->fd, (FAR const char *)buf + nwritten,
                       len - nwritten);
diff --git a/libs/libc/stdio/lib_stdoutstream.c b/libs/libc/stdio/lib_stdoutstream.c
index 903acb2..beb4157 100644
--- a/libs/libc/stdio/lib_stdoutstream.c
+++ b/libs/libc/stdio/lib_stdoutstream.c
@@ -33,48 +33,35 @@
  ****************************************************************************/
 
 /****************************************************************************
- * Name: stdoutstream_puts
+ * Name: stdoutstream_putc
  ****************************************************************************/
 
-static int stdoutstream_puts(FAR struct lib_outstream_s *this,
-                             FAR const void *buf, int len)
+static void stdoutstream_putc(FAR struct lib_outstream_s *this, int ch)
 {
   FAR struct lib_stdoutstream_s *sthis =
                                (FAR struct lib_stdoutstream_s *)this;
-  int nwritten = 0;
-  int ret;
+  int result;
 
   DEBUGASSERT(this && sthis->stream);
 
-  while (1)
+  /* Loop until the character is successfully transferred or an irrecoverable
+   * error occurs.
+   */
+
+  do
     {
-      ret = fwrite((FAR char *)buf + nwritten,
-                    len - nwritten, 1, sthis->stream);
-      if (ret <= 0)
+      result = fputc(ch, sthis->stream);
+      if (result != EOF)
         {
-          if (_NX_GETERRNO(ret) == EINTR)
-            {
-              continue;
-            }
-
-          break;
+          this->nput++;
+          return;
         }
 
-      this->nput += ret;
-      nwritten   += ret;
+      /* EINTR (meaning that fputc was interrupted by a signal) is the only
+       * recoverable error.
+       */
     }
-
-  return nwritten > 0 ? nwritten : ret;
-}
-
-/****************************************************************************
- * Name: stdoutstream_putc
- ****************************************************************************/
-
-static void stdoutstream_putc(FAR struct lib_outstream_s *this, int ch)
-{
-  char tmp = ch;
-  (void)stdoutstream_puts(this, &tmp, 1);
+  while (get_errno() == EINTR);
 }
 
 /****************************************************************************
@@ -118,8 +105,7 @@ void lib_stdoutstream(FAR struct lib_stdoutstream_s *outstream,
 {
   /* Select the put operation */
 
-  outstream->public.put  = stdoutstream_putc;
-  outstream->public.puts = stdoutstream_puts;
+  outstream->public.put = stdoutstream_putc;
 
   /* Select the correct flush operation.  This flush is only called when
    * a newline is encountered in the output stream.  However, we do not