<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Sep 23, 2016 at 12:08 PM, Holger Freyther <span dir="ltr"><<a href="mailto:holger@freyther.de" target="_blank">holger@freyther.de</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
<br>
> On 23 Sep 2016, at 19:22, Eliot Miranda <<a href="mailto:eliot.miranda@gmail.com">eliot.miranda@gmail.com</a>> wrote:<br>
><br>
> Hi Holger,<br>
><br>
<br>
Hey!<br>
<span class=""><br>
<br>
<br>
<br>
> And as the comment says:<br>
><br>
> /* Line buffering in fread can't be relied upon, at least on Mac OS X<br>
> * and mingw win32. So do it the hard way.<br>
> */<br>
> bytesRead = 0;<br>
> do {<br>
> clearerr(file);<br>
> if (fread(dst, 1, 1, file) == 1) {<br>
> bytesRead += 1;<br>
<br>
</span> bytesRead > 0 now<br>
<span class=""><br>
> if (dst[bytesRead-1] == '\n'<br>
> || dst[bytesRead-1] == '\r')<br>
> break;<br>
> }<br>
> }<br>
> while (bytesRead <= 0 && ferror(file) && errno == EINTR);<br>
<br>
</span> ^^^ false && ? && ?<br></blockquote><div><br></div><div>While we haven't read anything and there's an error and the error is due to being interrupted.</div><div>So exit if we've read something (because we're done),</div><div> or if we haven't and there was no error (because there was no input)</div><div> or we haven't and there was an error other than being interrupted (because something wen't wrong and looping won't fix it)</div><div><br></div><div>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?</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<span class=""><br>
<br>
><br>
> That seems like a good idea. So you're proposing<br>
><br>
> fgets(dst,1,file)<br>
><br>
> right? Much nicer. To whoever implements this please test on Windows and Mac OS and Linux.<br>
<br>
<br>
</span>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);<br>
<br>
My new proposal then is to change<br>
<span class=""><br>
while (bytesRead <= 0 && ferror(file) && errno == EINTR)<br>
<br>
</span>to:<br>
<br>
while (bytesRead <= count && ferror(file) && errno == EINTR)<br>
<br>
<br>
cheers<br>
<span class="HOEnZb"><font color="#888888"> holger</font></span></blockquote></div><br>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.<br clear="all"><div><br></div><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><span style="font-size:small;border-collapse:separate"><div>_,,,^..^,,,_<br></div><div>best, Eliot</div></span></div></div></div>
</div></div>