Mail Archives: djgpp/1999/04/28/03:20:13
On Tue, 27 Apr 1999, Rob Kramer wrote:
> I noticed that open/write or fopen/fwrite seem to handle my disk running
> out of space as if nothting special is going on.
This depends on how did you call `write' and `fwrite'. There *is* a
way to get them to return an error, see below.
> I would expect them to bomb and set errno appropriately.
Sorry, you expect too much. When a filesystem fills, neither DOS nor
Windows 9X don't fail the call; they simply write as many bytes as
would fit, and return the count of what was actually written to the
caller. Since a library function cannot easily know why the OS wrote
only part of the buffer, it doesn't automatically set errno to ENOSPC,
unless it has some corroborating evidence: it only returns ENOSPC if
ZERO bytes were written.
So, to get what you want, do something like this (given a pointer to a
buffer in buf_ptr and its length in buf_len):
while (buf_len > 0)
{
int written = write (desc, buf_ptr, len);
if (written <= 0)
break;
buf_ptr += written;
len -= written;
}
This will set errno to ENOSPC if the disk fills. The trick is to call
`write' one more time after it fills the disk, so that it fails to
write anything.
- Raw text -