On Fri, Jul 11, 2003 at 12:41:55PM -0700, Remco Treffkorn wrote:
> On Friday 11 July 2003 01:59, Ethan Benson wrote:
> > On Thu, Jul 10, 2003 at 05:35:33PM -0700, Remco Treffkorn wrote:
> > > Fwrite is supposed to return the number of items written.
> >
> > number of items, NOT number of characters:
> >
>
> I write: "Fwrite is supposed to return the number of items written", you write
> "number of items, NOT number of characters". You are re-stating what I said.
> Am I missing the point?
no but your patch changed it to return number of items written, not
number of shorted items (on errors) as the docs seem to say should happen.
> It does not 'appear' to be broken, it is broken.
dont be an ass.
> The return value should be the number of items successfully written. There is
> no anbiguity here. Please re-consider, the patch appears to fix it properly.
no your patch is broken. on error it returns number of items written,
NOT number of short items as the documentation states:
If an error occurs, or the end-of-file is reached, the return value is
a short item count (or zero).
^^^^^^^^^^^^^^^^^^
> I can not test with glibc, since I have no easy way to make it fail on the
> second item. I can only look at the code and the spec/man-page. The trivial
> case test agrees with me. The return valu for a single item successfully
> written is 1, not -1 as in your case.
and that is fixed.
im talking about your changes to error condition return values, not
the success case.
> In the trivial error case you will return 'count-bwrote' or 0-0, which happens
> to be correct for this case. In the non-trivial case, where count is > 1, you
> are in a world of pain.
>
> Assume 'count=2'. The first item writes ok, you are trying to write the
> second, and it fails. 'count' is now 0 (twice evaluated with post-decrement)
> and bwrote is 1 (for one item written). You will return 'count-bwrote', which
> is -1 again, which is wrong again. Sure the variable name 'bwrote' is
> misleading, but I did not choose it.
>
> Have a look at todays version :
> size_t fwrite(const void *buf, size_t size, size_t count, FILE *fp)
> {
> ssize_t ret;
> size_t nwrote = 0; /* item count, eg: number of size chunks */
>
> while (count--) {
> ret = write(fileno(fp), buf, size);
> if (ret == -1) {
> fp->error |= _STDIO_ERROR;
> return count-nwrote;
> } else if ((size_t)ret != size) {
> fp->error |= _STDIO_EOF;
> return count-nwrote;
> } else {
> nwrote++;
> buf += ret;
> }
> }
> return nwrote;
> }
>
> This fixes the trivial, non-error case. The name 'nwrote' better reflects
> purpose. The return value in the error case is still wrong. For the 'two item
> case failing on the second' case you still return -1 instead of 1. You need
> to return 'nwrote', not 'count-nwrote'.
>
> Do you want me to re-submit a patch against todays version, or is the original
> from yesterday still usefull?
your patch is wrong. it returns number of SUCESSFUL writes, not the
number of SHORTED writes.
you may submit a new patch, which actually implements documented behavior.
--
Ethan Benson
http://www.alaska.net/~erbenson/
Attachment:
pgp00025.pgp
Description: PGP signature