Fix fwrite() to return 0 when size or nmemb are zero.

Right now nmemb is returned when size is 0. In newer versions of the
standards, it is explicitly required that fwrite() should return 0.

Submitted by:	Christoph Mallon
Approved by:	re (kib)
This commit is contained in:
Ed Schouten 2009-07-12 13:09:43 +00:00
parent 5861f96654
commit 77822acff7
2 changed files with 9 additions and 4 deletions
lib/libc/stdio

@ -67,9 +67,7 @@ __fread(void * __restrict buf, size_t size, size_t count, FILE * __restrict fp)
size_t total;
/*
* The ANSI standard requires a return value of 0 for a count
* or a size of 0. Peculiarily, it imposes no such requirements
* on fwrite; it only requires fread to be broken.
* ANSI and SUSv2 require a return value of 0 if size or count are 0.
*/
if ((resid = count * size) == 0)
return (0);

@ -57,8 +57,15 @@ fwrite(buf, size, count, fp)
struct __suio uio;
struct __siov iov;
/*
* ANSI and SUSv2 require a return value of 0 if size or count are 0.
*/
n = count * size;
if (n == 0)
return (0);
iov.iov_base = (void *)buf;
uio.uio_resid = iov.iov_len = n = count * size;
uio.uio_resid = iov.iov_len = n;
uio.uio_iov = &iov;
uio.uio_iovcnt = 1;