<div dir="ltr">Hi Holger,<div class="gmail_extra"><br><div class="gmail_quote">On Fri, Sep 23, 2016 at 12:33 AM, 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:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><br>
Good Morning,<br>
<br>
I am working on some primitives to allow to populate a SQFile* based on int fd/FILE* and tried to understand the semantics of isStdioStream. When opening a non-file (e.g. a file descriptor returned from inotify_init1) I noticed this gem:<br>
<br>
<br>
bytesRead = 0;<br>
do {<br>
clearerr(file);<br>
if (fread(dst, 1, 1, file) == 1) {<br>
bytesRead += 1;<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>
* >>#primRead:into:startingAt:<wbr>count: will ignore count and only fill one byte at max. After the first successful fread the while loop will be left. The if for checking for a \n or \r could be deleted. I assume the intention was to read until bytesRead == count or \n\r? So e.g. even count: 0 will be ignored and turned into a '1'.<br></blockquote><div><br></div><div>Right. I added code to make reads from stdin work. I was implementing the following arm for stdin:</div><div><br></div><div> else</div><div> do {</div><div> clearerr(file);</div><div> bytesRead = fread(dst, 1, count, file);</div><div> }</div><div> while (bytesRead <= 0 && ferror(file) && errno == EINTR);</div><div><br></div><div>i.e. the preexisting code insisted on only reading one character at a time.</div><div><br></div><div>And as the comment says:</div><div><br></div><div><div> /* Line buffering in fread can't be relied upon, at least on Mac OS X</div><div> * and mingw win32. So do it the hard way.</div><div> */</div><div> bytesRead = 0;</div><div> do {</div><div> clearerr(file);</div><div> if (fread(dst, 1, 1, file) == 1) {</div><div> bytesRead += 1;</div><div> if (dst[bytesRead-1] == '\n'</div><div> || dst[bytesRead-1] == '\r')</div><div> break;</div><div> }</div><div> }</div><div> while (bytesRead <= 0 && ferror(file) && errno == EINTR);</div></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<br>
* To me line buffering looks a bit like a workaround but if we want this semantic for stdio can't we use fgets?<br></blockquote><div><br></div><div>That seems like a good idea. So you're proposing</div><div><br></div><div> fgets(dst,1,file)<br></div><div><br></div><div>right? Much nicer. To whoever implements this please test on Windows and Mac OS and Linux.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">regards<br>
<span class="gmail-HOEnZb"><font color="#888888"> holger</font></span></blockquote></div><br><div class="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>