[Vm-dev] FilePlugin and reading from stdio

Holger Freyther holger at freyther.de
Sat Sep 24 12:37:38 UTC 2016


> On 24 Sep 2016, at 00:05, Eliot Miranda <eliot.miranda at gmail.com> wrote:
> 
> 

Good Morning!

> 
> On Fri, Sep 23, 2016 at 12:08 PM, Holger Freyther <holger at freyther.de> wrote:
> 
> 
> 
> >         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)

Please remember that this is the stdin case:

a.) If on each primitive call we want to read 0 or 1 character let's remove the check if a \r or \n was read (as bytesRead == 1 and the loop will be terminated anyway).

b.) Let's try to read closer to count? If we assume line buffering and we have read one character it is likely we can read another until either count or \r\n is reached?


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

true, boolean logic is hard ;)


	while (continueToRead(file, count, bytesRead))

static inline bool
continueToRead(FILE* file, size_t count, size_t bytesRead)
{
	if (ferror(file) && errno != EINTR)
		return 0;
	return bytesRead < count;
}





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

right on first error we should bail out, I think the above is doing that?

holger



More information about the Vm-dev mailing list