On Fri, Sep 23, 2016 at 12:08 PM, Holger Freyther <holger@freyther.de> wrote:


> On 23 Sep 2016, at 19:22, Eliot Miranda <eliot.miranda@gmail.com> wrote:
>
> Hi Holger,
>

Hey!




> And as the comment says:
>
>         /* Line buffering in fread can't be relied upon, at least on Mac OS X
>          * and mingw win32.  So do it the hard way.
>          */
>         bytesRead = 0;
>         do {
>             clearerr(file);
>             if (fread(dst, 1, 1, file) == 1) {
>                 bytesRead += 1;

                        bytesRead > 0 now

>                 if (dst[bytesRead-1] == '\n'
>                  || dst[bytesRead-1] == '\r')
>                     break;
>             }
>         }
>         while (bytesRead <= 0 && ferror(file) && errno == EINTR);

                        ^^^ false && ? && ?

While we haven't read anything and there's an error and the error is due to being interrupted.
So exit if we've read something (because we're done),
       or if we haven't and there was no error (because there was no input)
       or we haven't and there was an error other than being interrupted (because something wen't wrong and looping won't fix it)

So the condition under which to keep trying is having yet to read input and having been interrupted.  In all other circumstances we should exit.  Doesn't that make sense?



>
> That seems like a good idea.  So you're proposing
>
>             fgets(dst,1,file)
>
> right?  Much nicer.  To whoever implements this please test on Windows and Mac OS and Linux.


I had fgets(dst, count, file) in mind but after re-reading the manpage we don't need/want the NUL termination here. (and I don't think doing something like char buf[count+1]; fgets.; memcpy(dst, buf, size);

My new proposal then is to change

        while (bytesRead <= 0 && ferror(file) && errno == EINTR)

to:

        while (bytesRead <= count && ferror(file) && errno == EINTR)


cheers
        holger

But what happens if we read, say, half the requested input and then there's an error.  The VM shouldn't lock spinning trying to read input.  For example, what if the image requests reading 100 characters and t6he user types end-of-file as input?  We don't want to spin trying to read input which will never become available.

_,,,^..^,,,_
best, Eliot