ComputerNerd's fixes

This pull request consists on most of the fixes discussed here:
http://www.cemetech.net/forum/viewtopic.php?p=222860

I am the one doing the pull request because ComputerNerd's fork has many other changes related to libc, which are not related to these fixes.
The only thing not included is the fwrite/fwrite_term changes which we believe should be discussed separately, as they consist on a big change from the current behavior.

I see that GitHub says the branches can't be automatically merged, so if it is too much work to merge this, it may be easier to just follow the fix instructions step-by-step on the current head.