From 914ef57844ce77eb693bcbf2d6912146007fc75f Mon Sep 17 00:00:00 2001 From: obarthel Date: Sat, 24 Aug 2019 13:58:04 +0200 Subject: [PATCH] Changed how size=0 plays out Setting the buffer size to 0 had the effect of switching the buffering mode. This is a bad idea since it wasn't actually called for: this is what the 'bufmode' parameter is intended for. What happens now is that the default buffer size is used (BUFSIZ), which sort of follows what the BSD libc did: a zero buffer size delayed the allocation of a buffer until the first read/write access occured. --- library/stdio_setvbuf.c | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/library/stdio_setvbuf.c b/library/stdio_setvbuf.c index b3cb697..5120a25 100644 --- a/library/stdio_setvbuf.c +++ b/library/stdio_setvbuf.c @@ -1,10 +1,8 @@ /* - * $Id: stdio_setvbuf.c,v 1.11 2008-09-04 12:07:58 obarthel Exp $ - * * :ts=4 * * Portable ISO 'C' (1994) runtime library for the Amiga computer - * Copyright (c) 2002-2015 by Olaf Barthel + * Copyright (c) 2002-2019 by Olaf Barthel * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -114,20 +112,26 @@ setvbuf(FILE *stream,char *buf,int bufmode,size_t size) goto out; } - /* A buffer size of 0 bytes defaults to unbuffered operation. */ + /* A buffer size of 0 bytes will cause the default buffer size to be used. */ if(size == 0) - bufmode = IOBF_BUFFER_MODE_NONE; - - /* If a certain buffer size is requested but no buffer was provided, - allocate some memory for it. */ - if(size > 0 && buf == NULL) { - /* Allocate a little more memory than necessary. */ - new_buffer = malloc(size + (__cache_line_size-1)); - if(new_buffer == NULL) + size = BUFSIZ; + buf = NULL; + } + + if(bufmode != IOBF_BUFFER_MODE_NONE) + { + /* If a certain buffer size is requested but no buffer was provided, + allocate some memory for it. */ + if(size > 0 && buf == NULL) { - __set_errno(ENOBUFS); - goto out; + /* Allocate a little more memory than necessary. */ + new_buffer = malloc(size + (__cache_line_size-1)); + if(new_buffer == NULL) + { + __set_errno(ENOBUFS); + goto out; + } } }