[Vm-dev] FilePlugin and reading from stdio

Holger Freyther holger at freyther.de
Fri Sep 23 19:08:44 UTC 2016


> On 23 Sep 2016, at 19:22, Eliot Miranda <eliot.miranda at 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 && ? && ?


> 
> 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


More information about the Vm-dev mailing list