more off

EAGAIN = errno ('EAGAIN')
EINVAL = errno ('EINVAL')

[in, out, pid] = popen2 ('echo_bug2.bat')
assert(pid > 0)

num_again = 0;
while (1)
  s = fgets (out);
  if (ischar (s))
    fputs (stdout, s);
  elseif (errno () == EINVAL)
    disp(sprintf('<waiting> (win32), s=%d, errno=%d', s, errno()))
    errno(0);
    fclear (out);
    sleep (0.25);
    num_again++;
  elseif (errno () == EAGAIN)
    disp(sprintf('<waiting> (unix), s=%d, errno=%d', s, errno()))
    fclear (out);
    sleep (0.25);
    num_again++;
  else
    disp(sprintf('<waiting> (somewhere), s=%d, errno=%d', s, errno()))
    sleep (0.25);
    num_again++;
  end
  if (num_again > 30)
    disp('no many waits, breaking out')
    break
  end
end

r = fclose (in);  assert(r == 0)
fclose (out);
waitpid (pid);

