www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/04/28/03:20:13

Date: Wed, 28 Apr 1999 10:17:54 +0300 (IDT)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
X-Sender: eliz AT is
To: Rob Kramer <robk AT cyberway DOT com DOT sg>
cc: djgpp AT delorie DOT com
Subject: Re: open() / write() doesn't fail on disk full?
In-Reply-To: <MPG.118fd94ddcf59d33989685@news.cyberway.com.sg>
Message-ID: <Pine.SUN.3.91.990428101607.25283L-100000@is>
MIME-Version: 1.0
Reply-To: djgpp AT delorie DOT com
X-Mailing-List: djgpp AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019