Issue 10344 Exiting _Dmain should flush all FILE*s and return nonzero on failure

Flushing all open streams on exit, throwing an Error with the errno string in case of failure.

TODO: test cases. But must get test suite to work first.