From 98d072a1f703074545d33c83a912f49b8d3cfcf9 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Thu, 9 Feb 2017 15:39:33 -0600 Subject: [PATCH] Apparently setvbuf() size can be nonzero with _IONBF. That makes no sense, but is necessary if setbuf() is to work as it is defined at OpenGroup.org. --- libc/stdio/lib_setbuf.c | 23 +++++++++-------------- libc/stdio/lib_setvbuf.c | 22 +++++++++++----------- 2 files changed, 20 insertions(+), 25 deletions(-) diff --git a/libc/stdio/lib_setbuf.c b/libc/stdio/lib_setbuf.c index f5fb435192..1655747c3c 100644 --- a/libc/stdio/lib_setbuf.c +++ b/libc/stdio/lib_setbuf.c @@ -59,11 +59,7 @@ * * setvbuf(stream, buf, _IONBF, BUFSIZ) * - * if buf is a null pointer. - * - * EXCEPTION: Currently, the NuttX setvbuf() expects the size argument to - * be zero if the mode is __INOBF. That is a descrepancy! What would a - * non-zero size mean in that case? + * if buf is a null pointer. * * Input Parameters: * stream - The stream whose buffer will be modified @@ -77,16 +73,15 @@ void setbuf(FAR FILE *stream, FAR char *buf) { +#ifndef CONFIG_STDIO_DISABLE_BUFFERING + int mode; + DEBUGASSERT(stream != NULL); -#ifndef CONFIG_STDIO_DISABLE_BUFFERING - if (buf != NULL) - { - (void)setvbuf(stream, buf, _IOFBF, BUFSIZ); - } - else - { - (void)setvbuf(stream, NULL, _IONBF, 0); - } + mode = (buf != NULL) ? _IOFBF : _IONBF; + (void)setvbuf(stream, buf, mode, BUFSIZ); + +#else + DEBUGASSERT(stream != NULL); #endif } diff --git a/libc/stdio/lib_setvbuf.c b/libc/stdio/lib_setvbuf.c index 65786cdedd..102ffd1a63 100644 --- a/libc/stdio/lib_setvbuf.c +++ b/libc/stdio/lib_setvbuf.c @@ -113,16 +113,6 @@ int setvbuf(FAR FILE *stream, FAR char *buffer, int mode, size_t size) goto errout; } - /* A non-zero size (or a non-NULL buffer) with mode = _IONBF makes no - * sense. - */ - - if (mode == _IONBF && size > 0) - { - errcode = EINVAL; - goto errout; - } - /* My assumption is that if size is zero for modes {_IOFBF, _IOLBF} the * caller is only attempting to change the buffering mode. In this case, * the existing buffer should be re-used (if there is one). If there is no @@ -136,6 +126,17 @@ int setvbuf(FAR FILE *stream, FAR char *buffer, int mode, size_t size) size = BUFSIZ; } + /* A non-zero size (or a non-NULL buffer) with mode = _IONBF makes no + * sense but is, apparently, permissible. We simply force the buffer to + * NULL and size to zero in this case without complaining. + */ + + else if (mode == _IONBF) + { + buffer = NULL; + size = 0; + } + /* Make sure that we have exclusive access to the stream */ lib_take_semaphore(stream); @@ -229,7 +230,6 @@ int setvbuf(FAR FILE *stream, FAR char *buffer, int mode, size_t size) case _IONBF: /* No buffer needed... We must be performing unbuffered I/O */ - DEBUGASSERT(size == 0); newbuf = NULL; break;