<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">&lt;<a href="mailto:holger@freyther.de" target="_blank">holger@freyther.de</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
<br>
&gt; On 23 Sep 2016, at 19:22, Eliot Miranda &lt;<a href="mailto:eliot.miranda@gmail.com">eliot.miranda@gmail.com</a>&gt; wrote:<br>
&gt;<br>
&gt; Hi Holger,<br>
&gt;<br>
<br>
Hey!<br>
<span class=""><br>
<br>
<br>
<br>
&gt; And as the comment says:<br>
&gt;<br>
&gt;         /* Line buffering in fread can&#39;t be relied upon, at least on Mac OS X<br>
&gt;          * and mingw win32.  So do it the hard way.<br>
&gt;          */<br>
&gt;         bytesRead = 0;<br>
&gt;         do {<br>
&gt;             clearerr(file);<br>
&gt;             if (fread(dst, 1, 1, file) == 1) {<br>
&gt;                 bytesRead += 1;<br>
<br>
</span>                        bytesRead &gt; 0 now<br>
<span class=""><br>
&gt;                 if (dst[bytesRead-1] == &#39;\n&#39;<br>
&gt;                  || dst[bytesRead-1] == &#39;\r&#39;)<br>
&gt;                     break;<br>
&gt;             }<br>
&gt;         }<br>
&gt;         while (bytesRead &lt;= 0 &amp;&amp; ferror(file) &amp;&amp; errno == EINTR);<br>
<br>
</span>                        ^^^ false &amp;&amp; ? &amp;&amp; ?<br></blockquote><div><br></div><div>While we haven&#39;t read anything and there&#39;s an error and the error is due to being interrupted.</div><div>So exit if we&#39;ve read something (because we&#39;re done),</div><div>       or if we haven&#39;t and there was no error (because there was no input)</div><div>       or we haven&#39;t and there was an error other than being interrupted (because something wen&#39;t wrong and looping won&#39;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&#39;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>
&gt;<br>
&gt; That seems like a good idea.  So you&#39;re proposing<br>
&gt;<br>
&gt;             fgets(dst,1,file)<br>
&gt;<br>
&gt; 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&#39;t need/want the NUL termination here. (and I don&#39;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 &lt;= 0 &amp;&amp; ferror(file) &amp;&amp; errno == EINTR)<br>
<br>
</span>to:<br>
<br>
        while (bytesRead &lt;= count &amp;&amp; ferror(file) &amp;&amp; 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&#39;s an error.  The VM shouldn&#39;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&#39;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>