From b8e622ea5dcadd2c3f10007c69b4980c2ee7e157 Mon Sep 17 00:00:00 2001 From: yinshengkai Date: Fri, 3 Mar 2023 12:33:16 +0800 Subject: [PATCH] libc/stream: unify stream behavior Return the error code when all gets occur when an error is wrong and return immediately when obtaining any valid data Signed-off-by: yinshengkai --- libs/libc/stream/lib_rawoutstream.c | 30 ++++++++++++++--------------- libs/libc/stream/lib_rawsostream.c | 5 ++--- libs/libc/stream/lib_stdsistream.c | 4 ++++ 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/libs/libc/stream/lib_rawoutstream.c b/libs/libc/stream/lib_rawoutstream.c index e895067743..34d383f18d 100644 --- a/libs/libc/stream/lib_rawoutstream.c +++ b/libs/libc/stream/lib_rawoutstream.c @@ -46,27 +46,27 @@ 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 = 0; - while (nwritten != len) + do { - ret = _NX_WRITE(rthis->fd, (FAR const char *)buf + nwritten, - len - nwritten); - if (ret <= 0) + nwritten = _NX_WRITE(rthis->fd, buf, len); + if (nwritten >= 0) { - if (_NX_GETERRNO(ret) == EINTR) - { - continue; - } - - break; + this->nput += nwritten; + return nwritten; } - this->nput += ret; - nwritten += ret; - } + /* The only expected error is EINTR, meaning that the write operation + * was awakened by a signal. Zero would not be a valid return value + * from _NX_WRITE(). + */ - return nwritten > 0 ? nwritten : ret; + nwritten = _NX_GETERRVAL(nwritten); + DEBUGASSERT(nwritten < 0); + } + while (nwritten == -EINTR); + + return nwritten; } /**************************************************************************** diff --git a/libs/libc/stream/lib_rawsostream.c b/libs/libc/stream/lib_rawsostream.c index 2f286c386d..61a5d93d9a 100644 --- a/libs/libc/stream/lib_rawsostream.c +++ b/libs/libc/stream/lib_rawsostream.c @@ -45,7 +45,6 @@ static void rawsostream_putc(FAR struct lib_sostream_s *this, int ch) FAR struct lib_rawsostream_s *rthis = (FAR struct lib_rawsostream_s *)this; char buffer = ch; int nwritten; - int errcode; DEBUGASSERT(this && rthis->fd >= 0); @@ -67,10 +66,10 @@ static void rawsostream_putc(FAR struct lib_sostream_s *this, int ch) * from _NX_WRITE(). */ - errcode = _NX_GETERRNO(nwritten); + nwritten = _NX_GETERRVAL(nwritten); DEBUGASSERT(nwritten < 0); } - while (errcode == EINTR); + while (nwritten == -EINTR); } /**************************************************************************** diff --git a/libs/libc/stream/lib_stdsistream.c b/libs/libc/stream/lib_stdsistream.c index 9feb39ab8c..9732492539 100644 --- a/libs/libc/stream/lib_stdsistream.c +++ b/libs/libc/stream/lib_stdsistream.c @@ -71,6 +71,10 @@ static int stdsistream_gets(FAR struct lib_instream_s *this, { this->nget += nread; } + else + { + nread = _NX_GETERRVAL(nread); + } return nread; }